LSTM network error: Predictors and responses must have the same number of observations.

5 views (last 30 days)
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);

Answers (1)

Vinayak Choyyan
Vinayak Choyyan on 15 Feb 2023
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)
ans = 1×2
2 1
This is a cell array of size 2x1. The output is a single number. You have given this as
target = [1 2];
size(target)
ans = 1×2
1 2
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.

Categories

Find more on Image Data Workflows in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!