The prediction sequences are of feature dimension 1 but the input layer expects sequences of feature dimension 2.

6 views (last 30 days)
Hello everyone
I try to predict traffic flow by using real time data and previous data. So I have 2 input and one output. I use LSTM for this purpose and then when I want to predict data I use " YPred = predict(net,YTest); " but i face an error The prediction sequences are of feature dimension 1 but the input layer expects sequences of feature dimension 2.
My XTrain has 470 column that has [{2×1 double} ] and YTrain has 479 column with 1*1 double .
and XTest and YTest have 60 columns and the cell is the same as train
I try everything but I can't fix it
please help me
Thanks
My code is :
clc
close all
clear all
warning off
% [~,~,flow_data] = xlsread('two_days.xlsx');
flow_data = xlsread('two_days.xlsx') ; % Here we have two days data
% data_mat = cell2mat(flow_data(2:end,3:4));
data_mat =(flow_data(2:end,3:4));
% XTrain = data_mat(:,3:4)';
% YTrain = data_mat(:,3:4)';
XTrain = data_mat(:,:)';
YTrain = data_mat(:,1)';
XTrain = num2cell(XTrain,1);
YTrain = num2cell(YTrain,1);
% %
numResponses = 1 ;
% numResponses = size(YTrain{1},1);
featureDimension = 2;
numHiddenUnits = 200;
layers = [ ...
sequenceInputLayer(featureDimension)
lstmLayer(numHiddenUnits)
% dropoutLayer(0.1) %%0.5
fullyConnectedLayer(numResponses)
regressionLayer];
maxepochs = 500;
miniBatchSize = 1;
options = trainingOptions('adam', ... %%adam
'MaxEpochs',maxepochs, ...
'GradientThreshold',1, ...
'InitialLearnRate',0.005, ...
'LearnRateSchedule','piecewise', ...
'LearnRateDropPeriod',225, ...
'LearnRateDropFactor',0.2, ...
'Verbose',1, ...
'Plots','training-progress');
% %%Train the Network
net = trainNetwork(XTrain,YTrain,layers,options);
%
test_data = xlsread('test_data2.xlsx') ; % Here we have two days data
data_mat2 =(test_data(1:end,3:4));
% XTest = data_mat2(:,:)';
YTest = data_mat2(:,1)';
XTest = data_mat2(:,1)';
XTrain = num2cell(XTest,1);
YTrain = num2cell(YTest,1);
net = resetState(net);
YPred = predict(net,YTest);
YPred = round(YPred)
% net = predictAndUpdateState(net,XTrain);
% [net,YPred] = predictAndUpdateState(net,YTrain(end));
% %
% % %
% % Predict as long as the test period
% numTimeStepsTest = numel(YTest);
% for i = 2:numTimeStepsTest
% [net,YPred(:,i)] = predictAndUpdateState(net,YPred(:,i-1),'ExecutionEnvironment','cpu');
% end
% YPred
% y1 = (cell2mat(YPred(1:end, 1:end))); %have to transpose as plot plots columns
% plot(y1)
% hold on
% y2 = (cell2mat(YTest(1:end, 1:end))');
% plot(y2)

Answers (1)

Krishna
Krishna on 9 Jul 2024
Hello Arash,
The issue seems to lie in how you've structured your data for LSTM training. The input to the time series network should be organized in a cell array format of nx1 for both input and output, where n represents the total number of sequences we have.
Further, for one sequence if the input has 2 features, it should be formatted as 2x(sequence length), where the sequence length can vary. If the output is a single value, it should be in the format 1x (sequence length).
So, for input the format would be n x (totalFeatures = 2) x (sequence length) and output format would be n x (totalOutputFeatures = 1) x (sequence length). Also, n would be same for both input/output which is not same in your case. Additionally, ensure that for the ith sequence, the input and output sequences are of the same length. Different sequences can have varying lengths.
Please review the following example to understand more about how to structure your data correctly for training a lstm network in MATLAB,
Structuring your data correctly would definitely solve your problem.
Hope this helps.

Categories

Find more on Sequence and Numeric Feature Data Workflows in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!