[Issue] How can I apply 'predictAndUpdateState' on a multivariate multistep problem?

11 views (last 30 days)
Mohamed Nedal
Mohamed Nedal on 3 Jul 2020
Commented: Xymbu on 15 Aug 2021
Hello,
I have this code below (also attached with data samples) of a multivariate LSTM neural network model.
I am trying to modify it to forecast new steps in the future, but I get this error:
Conversion to double from cell is not possible.
Error in LSTM_multi_motores (line 82)
[net, Yforecast(:,i)] = predictAndUpdateState(net, XTest(:,i), 'ExecutionEnvironment', 'cpu');
I tried to modify this line from
[net, Yforecast(:,i)] = predictAndUpdateState(net, XTest(:,i), 'ExecutionEnvironment', 'cpu');
to
[net, Yforecast(:,i)] = predictAndUpdateState(net, XTest{:,i}, 'ExecutionEnvironment', 'cpu');
but I got a different error here:
Index in position 2 exceeds array bounds (must not exceed 389).
Error in LSTM_multi_motores (line 82)
[net, Yforecast(:,i)] = predictAndUpdateState(net, XTest{:,i}, 'ExecutionEnvironment', 'cpu');
Could you please tell me how to fix it?
Here's the code:
%% Multivariate Multi-step LSTM NN Model
% From: Marcelo Olmedo -- MathWorks | Edited by: Mohamed Nedal
close all; clear; clc
%% Import data
[~,~,data_train] = xlsread('ET_1.xlsx');
data_mat_train = cell2mat(data_train);
XTrain = (data_mat_train(:, 4:8))';
YTrain = (data_mat_train(:, 3))';
XTrain = num2cell(XTrain, 1);
YTrain = num2cell(YTrain, 1);
miniBatchSize = 1; % one predictor sequence
%% Define Network Architecture
numResponses = size(YTrain{1}, 1);
featureDimension = size(XTrain{1}, 1);
fprintf('Number of input features: %d \n', featureDimension)
fprintf('Number of target features: %d \n', numResponses)
maxepochs = 100;
miniBatchSize = 1;
numHiddenUnits = 100;
% A fully connected layer multiplies the input by a weight matrix and then
% adds a bias vector
outputSize = 500;
layers = [ ...
sequenceInputLayer(featureDimension)
lstmLayer(numHiddenUnits, 'OutputMode', 'sequence')
fullyConnectedLayer(outputSize)
dropoutLayer(0.1)
fullyConnectedLayer(numResponses)
regressionLayer];
options = trainingOptions('adam', ...
'MaxEpochs', maxepochs, ...
'GradientThreshold', 1, ...
'InitialLearnRate', 0.005, ...
'LearnRateSchedule', 'piecewise', ...
'LearnRateDropPeriod', 125, ...
'LearnRateDropFactor', 0.2, ...
'Plots', 'training-progress');
%% Train the Network
net = trainNetwork(XTrain, YTrain, layers, options);
%% Test the Network
[~,~,data_test] = xlsread('ET_2_m_input.xlsx');
data_mat_test = cell2mat(data_test);
XTest = (data_mat_test(:, 4:8))';
YTest = (data_mat_test(:, 3))';
XTest = num2cell(XTest, 1);
YTest = num2cell(YTest, 1);
net = resetState(net);
YPred = predict(net, XTest);
rmse = sqrt(mean((cell2mat(YPred)' - cell2mat(YTest)).^2));
fprintf('RMSE = %.2f \n', mean(rmse))
%% Plot
y1 = (cell2mat(YTest(1:end, 1:end))');
y2 = (cell2mat(YPred(1:end, 1:end))); % have to transpose as plot plots columns
figure
plot(y1)
hold on
plot(y2)
legend('Test', 'Predict')
%% Forecast the Future
clc
net = resetState(net);
Yforecast = [];
numTimeStepsTest = numel(XTest) + 500; % forecast new 500 steps in the future
k = 1;
for i = 1:numTimeStepsTest
[net, Yforecast(:,i)] = predictAndUpdateState(net, XTest(:,i), 'ExecutionEnvironment', 'cpu');
if k ~= numel(XTest)
[net, Yforecast(:,i)] = predictAndUpdateState(net, XTest(:,i), 'ExecutionEnvironment', 'cpu');
else
[net, Yforecast(:,i)] = predictAndUpdateState(net, Yforecast(:,i-1), 'ExecutionEnvironment', 'cpu');
end
k = k + 1;
end
fprintf('New data is predicted successfully \n')

Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by