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