Error in training data sequence length. How to rectify this?

1 view (last 30 days)
Hi,
I am creating cnn+lstm architecture for prediction. When I am training the model, I am getting error " Error using trainNetwork: Invalid training data. Sequence responses must have the same sequence length as the corresponding predictors."
the code is below. The sample data is attached here.
data = xlsread("sample_data.xlsx", 1);
data_norm = normalize(data(:,1:end));
train_ratio = 0.8;
n_train = round(size(data,1) * train_ratio);
train_data = data(1:n_train,:);
test_data = data(n_train+1:end,:);
numFeatures = size(train_data,2)-1;
XTrain = train_data(:,1:end-1);
YTrain = train_data(:,end);
XTest = test_data(:,1:end-1);
YTest = test_data(:,end);
sequenceLength = 1;
% Reshape the input sequences
numTrain = floor(size(train_data,1)/sequenceLength);
XTrain = XTrain(1:numTrain*sequenceLength,:);
YTrain = YTrain(1:numTrain*sequenceLength,:);
XTrain = reshape(XTrain, [1, sequenceLength, numFeatures, numTrain]);
YTrain = reshape(YTrain, [1, sequenceLength, 1, numTrain]);
numValidation = floor(size(test_data,1)/sequenceLength);
XTest = XTest(1:numValidation*sequenceLength,:);
YTest = YTest(1:numValidation*sequenceLength,:);
XTest = reshape(XTest, [1, sequenceLength, numFeatures, numValidation]);
YTest = reshape(YTest, [1, sequenceLength, 1, numValidation]);
% the CNN LSTM model
inputSize = [1 size(XTrain,2) size(XTrain,3)];
numHiddenUnits = 64;
numClasses = 1;
layers = [ ...
sequenceInputLayer(inputSize)
convolution2dLayer([1 3],16,'Padding','same')
batchNormalizationLayer
reluLayer
convolution2dLayer([1 3],32,'Padding','same')
batchNormalizationLayer
reluLayer
convolution2dLayer([1 3],64,'Padding','same')
batchNormalizationLayer
reluLayer
flattenLayer
lstmLayer(numHiddenUnits,'OutputMode','sequence')
fullyConnectedLayer(numClasses)
regressionLayer];
% Specify the training options
options = trainingOptions('adam', ...
'MaxEpochs',50, ...
'MiniBatchSize',32, ...
'ValidationData',{test_data(:,1:end-1), test_data(:,end)}, ...
'ValidationFrequency',10, ...
'Shuffle','every-epoch', ...
'Plots','training-progress');
% Train the CNN LSTM model
net = trainNetwork(XTrain,YTrain, layers, options);
Error using trainNetwork
Invalid training data. Sequence responses must have the same sequence length as the corresponding predictors.
As for my analysis, the dimension seems to be correct. However I couldnot rectify this error.
Please help me to rectify this error.

Answers (1)

Pravarthana P
Pravarthana P on 8 May 2023
Hi,
This error occurs when the input value to ‘sequenceInputLayer’ is different from the ‘numFeatures’ in ‘XTrain’ data.
Changing the 'inputSize' in the code according to the 'XTrain' data might help. For more information, refer to the following documentation link :

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!