How do I create a 1D CNN

340 views (last 30 days)
Hi there, I'm relatively new to CNNs and currently exploring the application of CNNs to 1 dimensional data sets and would greatly appreciate some assistance with an error relating to the trainNetwork function. Despite having checked and rechecked the label array repeatedly, everything appears to be in order. I'm now at the point of using trial and error. Can anyone help?
The training dataset contains 662 samples each consisting of a 1-by-800 vector. I have reshaped this to a 4D array as specified in the help documentation (see also: https://au.mathworks.com/matlabcentral/answers/331164-convolutional-1d-net )
The training label array exists in the form of a 3-by-662 array of doubles.
See below:
height = 1;
width = 800;
channels = 1;
sampleSize = 662;
CNN_TrainingData = reshape(Training_ReductWindows_G,[height, width, channels, sampleSize]);
CNN_TrainingLabels = Training_Labels_Bin_G;;
% Build the CNN layers
InputLayer = imageInputLayer([height,width,channels]); %'DataAugmentation', 'none'); %'Normalization', 'none');
c1 = convolution2dLayer([1 5], 16),'stride',[1 10]); %Filter window size = [1 5], No of filters = 16, stride = [1 10].
% We use a max pooling layer as Downsampling layer. An alternative might be
% to use an average pooling layer e.g. AveragePooling2dLayer or a reluLayer
r1 = reluLayer();
p1 = maxPooling2dLayer([1 20],'stride',[1 10]); %PoolSize = [1 20], Stride = [1 10]
f1 = fullyConnectedLayer(3); %Reduce to three output classes
s1 = softmaxLayer();
outputLayer=classificationLayer();
convnet = [InputLayer; c1; r1; p1; f1; s1; outputLayer]
opts = trainingOptions('sgdm'); %Optimise using stochastic gradient descent with momentum
convnet = trainNetwork(CNN_TrainingData, CNN_TrainingLabels, convnet, opts);
However this consistently returns the following error:
Error using trainNetwork>iAssertCategoricalResponseVector (line 598)
Y must be a vector of categorical responses.
Error in trainNetwork>iAssertValidResponseForTheNetwork (line 589)
iAssertCategoricalResponseVector(x);
Error in trainNetwork>iParseInput (line 335)
iAssertValidResponseForTheNetwork( Y, layers );
Error in trainNetwork (line 68)
[layers, opts, X, Y] = iParseInput(varargin{:});
Error in CNN (line 38)
convnet = trainNetwork(CNN_TrainingData, CNN_TrainingLabels, convnet, opts);
Can anyone point out my error?
Thank you kindly
  3 Comments
aybike pirol
aybike pirol on 31 May 2020
Did you find the solution for your problem?
target=categorical(target_trans(1:500)'); returns the following error:
Error using categorical (line 431)
Unable to create default category names. Specify category names using the CATEGORYNAMES input argument.
but if its size is 365 and smaller, no problem.but my target data is larger than 365.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 28 Aug 2017
Try
CNN_TrainingLabels = categorical(Training_Labels_Bin_G);
  5 Comments
naglaa fathy
naglaa fathy on 17 Sep 2021
trainingFeatures=[];
trainingLabel=categorical.empty();
trainingFeatures(featureCount,:) = Features;
trainingLabel(featureCount,:) = TrainingimgSets(i).Description;

Sign in to comment.

More Answers (1)

David Willingham
David Willingham on 23 Sep 2021
Edited: David Willingham on 23 Sep 2021
Hi, as of R201b, you can create and train deep learning networks with 1-D convolution and pooling layers for sequence and time series data.
Create networks using the following layers:
The dimension that the layers convolve or pool over depends on the layer input:
  • For time series and vector sequence input (data with three dimensions corresponding to the channels, observations, and time steps, respectively), the layer convolves or pools over the time dimension.
  • For 1-D image input (data with three dimensions corresponding to the spatial pixels, channels, and observations, respectively), the layer convolves or pools over the spatial dimension.
  • For 1-D image sequence input (data with four dimensions corresponding to the spatial pixels, channels, observations, and time steps, respectively), the layer convolves or pools over the spatial dimension.
For an example showing how to train a sequence-to-label classification network using 1-D convolutions, see Sequence Classification Using 1-D Convolutions.
For an example showing how to train a sequence-to-sequence classification network using 1-D convolutions, see Sequence-to-Sequence Classification Using 1-D Convolutions.
Regards,

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by