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

4 vues (au cours des 30 derniers jours)
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);

Réponse acceptée

Matt J
Matt J le 28 Août 2025
Modifié(e) : Matt J le 28 Août 2025
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 commentaires
Bahadir
Bahadir le 28 Août 2025
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 le 28 Août 2025
Modifié(e) : Matt J le 28 Août 2025
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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Image Data Workflows dans Help Center et File Exchange

Produits


Version

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by