Error creating a CNN-LSTM network with sequences.
5 views (last 30 days)
Show older comments
Hello:
I'm writting here because I have the following issue:
I want to train a CNN-LSTM deep network in order to classify sequences into 2 labels.
I have 252 sequences of 45 seconds of 1891 channels, 80% of then (201) are used as training, 10% (25) are used in validation and the other 10% (26) are used in testing.
I created the neural network in Deep Network Designer as follows:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1688956/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1688961/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1688966/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1688971/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1688976/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1688981/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1688986/image.png)
When I try to train the network, this error message is shown:
Error using trainnet (line 46)
Number of observations in predictors (201) and targets (1) must match. Check that the data and network are
consistent.
Te workspace generated is this:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1689047/image.png)
Could somebody help me find where is the problem please?
Thank you so much.
This is the code:
rng('shuffle')
load('datos.mat');
data_ordenado = cell(252,1);
mylabels_ordenado = cell(252,1);
data_desordenado = cell(252,1);
mylabels_desordenado = cell(252,1);
for i = 1:252
if (i<=131)
data_ordenado{i} = cxy1(:,:,i)';
mylabels_ordenado{i} = 'Pre_experiment';
else
data_ordenado{i} = cxy17(:,:,i-131)';
mylabels_ordenado{i} = 'Post_experiment';
end
end
idx = randperm(252);
for i = 1:252
data_desordenado{i} = data_ordenado{idx(i)};
mylabels_desordenado{i} = mylabels_ordenado{idx(i)};
end
labels = categorical(mylabels_desordenado);
classNames = categories(labels);
numObservations = numel(data_desordenado);
[idxTrain,idxValidation,idxTest] = trainingPartitions(numObservations,[0.8 0.1 0.1]);
XTrain = data_desordenado(idxTrain);
TTrain = labels(idxTrain);
XValidation = data_desordenado(idxValidation);
TValidation = labels(idxValidation);
XTest = data_desordenado(idxTest);
TTest = labels(idxTest);
options = trainingOptions("adam", ...
MaxEpochs=500, ...
InitialLearnRate=0.0005, ...
GradientThreshold=1, ...
ValidationData={XValidation,TValidation}, ...
Shuffle = "every-epoch", ...
Plots="training-progress", ...
Metrics="accuracy", ...
Verbose=false);
net = trainnet(XTrain,TTrain',net_1,"crossentropy",options);
scores = minibatchpredict(net,XTest);
YTest = scores2label(scores,classNames);
acc = mean(YTest == TTest)
figure
confusionchart(TTest,YTest)
0 Comments
Answers (1)
Alex Wu
on 13 May 2024
Edited: Alex Wu
on 13 May 2024
Number of observations in predictors (201) and targets (1) must match.
This means during training, your input data X and target data T have different number of observations.
net = trainnet(XTrain,TTrain',net_1,"crossentropy",options);
Your TTrain matrix has dimensions 201x1. However, transposing it results in dimensions of 1x201, which is interpreted as a single observation. Try removing the transpose operation here.
0 Comments
See Also
Categories
Find more on Image Data Workflows in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!