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

24 ビュー (過去 30 日間)
hoaradam0207
hoaradam0207 2023 年 2 月 12 日
回答済み: Vinayak Choyyan 2023 年 2 月 15 日
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);

回答 (1 件)

Vinayak Choyyan
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)
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.

カテゴリ

Help Center および File ExchangeSequence and Numeric Feature Data Workflows についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by