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);



댓글 수: 0
채택된 답변
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
XTrain,
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)
댓글 수: 3
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 Center 및 File Exchange에서 Image Data Workflows에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!