Invalid training data. Predictors and responses must have the same number of observations.

18 次查看(过去 30 天)
I wan to train a LSTM.
But I get Error:
Error using trainNetwork (line 191)
Invalid training data. Predictors and responses must have the same number of observations.
layers = [ ...
sequenceInputLayer(6)
lstmLayer(120,'OutputMode','last')
fullyConnectedLayer(2)
softmaxLayer
classificationLayer];
options = trainingOptions('adam', ...
'MaxEpochs',20, ...
'MiniBatchSize',32, ...
'GradientThreshold',1, ...
'InitialLearnRate',0.005, ...
'Shuffle','every-epoch', ...
'Verbose',0, ...
'Plots','training-progress');
net = trainNetwork(XTrain, YTrain, layers, options);

采纳的回答

Matt J
Matt J 2025-8-28,19:41
编辑:Matt J 2025-8-28,19:55
Your XTrain shouldn't be a 100x6 cell. It should be a 100x1 cell where each XTrain{i} is a matrix with 6 rows. Example,
layers = [ ...
sequenceInputLayer(6)
lstmLayer(120,'OutputMode','last')
fullyConnectedLayer(2)
softmaxLayer
classificationLayer];
for i=1:100
XTrain{i,1} = rand(6,randi(20));
end
YTrain = categorical(randi([0,1],100,1));
whos YTrain
Name Size Bytes Class Attributes YTrain 100x1 346 categorical
XTrain,
XTrain = 100×1 cell array
{6×20 double} {6×16 double} {6×11 double} {6×2 double} {6×18 double} {6×8 double} {6×13 double} {6×17 double} {6×19 double} {6×6 double} {6×1 double} {6×1 double} {6×17 double} {6×10 double} {6×5 double} {6×4 double}
options = trainingOptions('adam', ...
'MaxEpochs',20, ...
'MiniBatchSize',32, ...
'GradientThreshold',1, ...
'InitialLearnRate',0.005, ...
'Shuffle','every-epoch', ...
'Verbose',1, ...
'Plots','none');
net = trainNetwork(XTrain, YTrain, layers, options)
Training on single CPU. |========================================================================================| | Epoch | Iteration | Time Elapsed | Mini-batch | Mini-batch | Base Learning | | | | (hh:mm:ss) | Accuracy | Loss | Rate | |========================================================================================| | 1 | 1 | 00:00:00 | 46.88% | 0.7000 | 0.0050 | | 17 | 50 | 00:00:01 | 71.88% | 0.6504 | 0.0050 | | 20 | 60 | 00:00:01 | 53.12% | 0.6374 | 0.0050 | |========================================================================================| Training finished: Max epochs completed.
net =
SeriesNetwork with properties: Layers: [5×1 nnet.cnn.layer.Layer] InputNames: {'sequenceinput'} OutputNames: {'classoutput'}
  3 个评论
Bahadir
Bahadir 2025-8-28,20:58
When ı try trainnet, I get the error.
Caused by:
Layer 'classoutput': Detected output layer. The network must not have output layers.
Matt J
Matt J 2025-8-28,21:05
编辑:Matt J 2025-8-28,21:34
The error is complaining that you have not removed the output layer (classificationLayer) from your layers array. Output layers do not belong in the network when training with trainnet, because the loss function is separately specified to trainnet using the lossFcn input parameter.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Image Data Workflows 的更多信息

产品


版本

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by