LSTM network error: Predictors and responses must have the same number of observations.
4 次查看(过去 30 天)
显示 更早的评论
I am trying to use an LSTM neural network to output a number based on two separate time series. I give the time series in pairs, based on which the network should output a single number.
Here is a code example to reproduce the error message:
clc, clear all;
% Training data, series in pairs. There is a pair of series in each cell
% element.
seq = {[1 2 3; 2 3 4];...
[0 1 2; 4 5 6]};
% 2 channels, since the series are in pairs.
numChannels = 2;
numHiddenUnits = 20;
% I would like to have a single response from the network.
numResponses = 1;
% Basic LSTM network architecture, taken from:
% https://www.mathworks.com/help/deeplearning/ug/sequence-to-one-regression-using-deep-learning.html
layers = [sequenceInputLayer(numChannels, Normalization="zscore")
lstmLayer(numHiddenUnits, OutputMode = "last")
fullyConnectedLayer(numResponses)
regressionLayer];
% Output targets for training. 2 outputs for 2 pairs of sequences.
target = [1 2];
% Arbitrarily chosen data for validation.
seqValidation = {[1 2 3; 2 3 4]*2; [0 1 2; 4 5 6]*3};
targetValidation = [2 6];
options = trainingOptions("adam", ...
MaxEpochs = 250, ...
ValidationData = {seqValidation targetValidation}, ...
OutputNetwork = "best-validation-loss", ...
InitialLearnRate = 0.005, ...
SequenceLength = "shortest", ...
Plots = "training-progress", ...
Verbose = false);
net = trainNetwork(seq, target, layers, options);
When trying to run the script I am getting the following error (script is called LSTMtry.m):
Error using trainNetwork
Invalid training data. Predictors and responses must have the same number of observations.
Error in LSTMtry (line 34)
net = trainNetwork(seq, target, layers, options);
0 个评论
回答(1 个)
Vinayak Choyyan
2023-2-15
Hello hoaradam0207,
As per my understanding, you are trying to train a neural network model and have 2 times series as input. I see that you have given this as
seq = {[1 2 3; 2 3 4];...
[0 1 2; 4 5 6]};
size(seq)
This is a cell array of size 2x1. The output is a single number. You have given this as
target = [1 2];
size(target)
This is of size 1x2. Please refer to the below code. I have changed the shape of target and targetValidation and the error does not occur. The model does train.
clc, clear all;
% Training data, series in pairs. There is a pair of series in each cell
% element.
seq = {[1 2 3; 2 3 4];...
[0 1 2; 4 5 6]};
% 2 channels, since the series are in pairs.
numChannels = 2;
numHiddenUnits = 20;
% I would like to have a single response from the network.
numResponses = 1;
% Basic LSTM network architecture, taken from:
% https://www.mathworks.com/help/deeplearning/ug/sequence-to-one-regression-using-deep-learning.html
layers = [sequenceInputLayer(numChannels, Normalization="zscore")
lstmLayer(numHiddenUnits, OutputMode = "last")
fullyConnectedLayer(numResponses)
regressionLayer];
% Output targets for training. 2 outputs for 2 pairs of sequences.
target = [1;2];
% Arbitrarily chosen data for validation.
seqValidation = {[1 2 3; 2 3 4]*2; [0 1 2; 4 5 6]*3};
targetValidation = [2;6];
options = trainingOptions("adam", ...
MaxEpochs = 250, ...
ValidationData = {seqValidation targetValidation}, ...
OutputNetwork = "best-validation-loss", ...
InitialLearnRate = 0.005, ...
SequenceLength = "shortest", ...
Plots = "training-progress", ...
Verbose = false);
net = trainNetwork(seq, target, layers, options);
I hope this resolves the issue you were facing.
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!