MATLAB Answers

Issue using LSTM neural network "Time Series Forecasting Using Deep Learning"

11 views (last 30 days)
Ibrahim Ibrahim
Ibrahim Ibrahim on 25 Jul 2020
Answered: Srivardhan Gadila on 19 Aug 2020
I am trying to use the LSTM regression
The code:
"%%Load Sequence Data
%Input data
S=xlsread("AVcigs.csv","AVcigs","C7:C56385");
%Output data
Pmpp=xlsread("AVcigs.csv","AVcigs","F7:F56385");
for i=1:length(Pmpp)
if Pmpp(i) <0
Pmpp(i)=0;
else
Pmpp(i)=Pmpp(i);
end
end
%Partition the training and test data.
numTimeStepsTrain = floor(0.7*numel(S));
dataTrain_S = S(1:numTimeStepsTrain+1);
dataTest_S = S(numTimeStepsTrain+1:end);
dataTrain_Pmpp = Pmpp(1:numTimeStepsTrain+1);
dataTest_Pmpp = Pmpp(numTimeStepsTrain+1:end);
%%Standardize Data
mu_S = mean(dataTrain_S);
sig_S = std(dataTrain_S);
dataTrainStandardized_S = (dataTrain_S - mu_S) / sig_S;
mu_Pmpp = mean(dataTrain_Pmpp);
sig_Pmpp = std(dataTrain_Pmpp);
dataTrainStandardized_Pmpp = (dataTrain_Pmpp - mu_Pmpp) / sig_Pmpp;
%%Prepare Predictors and Responses
XTrain = dataTrainStandardized_S(1:end-1);
XTrain = XTrain';
YTrain = dataTrainStandardized_Pmpp(1:end-1);
YTrain = YTrain';
for i=1:length(YTrain)
if YTrain(i) <0
YTrain(i)=0;
else
YTrain(i)=YTrain(i);
end
end
%%Define LSTM Network Architecture
numFeatures = 1;
numResponses = 1;
numHiddenUnits = 200;
layers = [ ...
sequenceInputLayer(numFeatures)
lstmLayer(numHiddenUnits)
fullyConnectedLayer(numResponses)
regressionLayer];
options = trainingOptions('adam', ...
'MaxEpochs',250, ...
'GradientThreshold',1, ...
'InitialLearnRate',0.005, ...
'LearnRateSchedule','piecewise', ...
'LearnRateDropPeriod',125, ...
'LearnRateDropFactor',0.2, ...
'Verbose',0, ...
'Plots','training-progress');
%%Train LSTM Network
net = trainNetwork(XTrain,YTrain,layers,options);
%%Forecast Future Time Steps
%Standardize the test data using the same parameters as the training data.
dataTestStandardized_S = (dataTest_S - mu_S) / sig_S;
dataTrainStandardized_Pmpp = (dataTest_Pmpp - mu_Pmpp) / sig_Pmpp;
net = predictAndUpdateState(net,XTrain);
[net,YPred] = predictAndUpdateState(net,YTrain(end));
YPred = sig_Pmpp*YPred + mu_Pmpp;
XTest=dataTestStandardized_S(1:end-1);
XTest=XTest';
YTest = dataTrainStandardized_Pmpp(1:end-1);
YTest = YTest';
for i=1:length(YTest)
if YTest(i) <0
YTest(i)=0;
else
YTest(i)=YTest(i);
end
end
numTimeStepsTest = numel(XTest);
for i = 2:numTimeStepsTest
[net,YPred(:,i)] = predictAndUpdateState(net,YPred(:,i-1),'ExecutionEnvironment','gpu');
end
YTest = Pmpp(2:end);
rmse = sqrt(mean((YPred-YTest).^2))
figure(1)
plot(dataTrain_Pmpp(1:end-1))
hold on
idx = numTimeStepsTrain:(numTimeStepsTrain+numTimeStepsTest);
plot(idx,[Pmpp(numTimeStepsTrain) YPred],'.-')
hold off
xlabel("Time")
ylabel("Pmpp")
title("Forecast")
legend(["Observed" "Forecast"])
figure(2)
subplot(2,1,1)
plot(YTest)
hold on
plot(YPred,'.-')
hold off
legend(["Observed" "Forecast"])
ylabel("Pmpp")
title("Forecast")
subplot(2,1,2)
stem(YPred - YTest)
xlabel("time")
ylabel("Error")
title("RMSE = " + rmse)
%%Update Network State with Observed Values
net = resetState(net);
net = predictAndUpdateState(net,XTrain);
YPred = [];
numTimeStepsTest = numel(XTest);
for i = 1:numTimeStepsTest
[net,YPred(:,i)] = predictAndUpdateState(net,XTest(:,i),'ExecutionEnvironment','cpu');
end
YPred = sig_Pmpp*YPred + mu_Pmpp;
rmse = sqrt(mean((YPred-YTest).^2))
figure (3)
subplot(2,1,1)
plot(YTest)
hold on
plot(YPred,'.-')
hold off
legend(["Observed" "Predicted"])
ylabel("Pmpp")
title("Forecast with Updates")
subplot(2,1,2)
stem(YPred - YTest)
xlabel("Time")
ylabel("Error")
title("RMSE = " + rmse)"
I keep having the following error:
"Out of memory.
Error in stem (line 104)
h(k) = matlab.graphics.chart.primitive.Stem('Parent', parax, 'YData',matlab.graphics.chart.internal.datachk(y(:,k)),
xdata{:},...
Error in LSTM_onefeature (line 150)
stem(YPred - YTest)"

  0 Comments

Sign in to comment.

Answers (1)

Srivardhan Gadila
Srivardhan Gadila on 19 Aug 2020
As we can see from the error message " Out of memory. Error in stem (line 104)
h(k) = matlab.graphics.chart.primitive.Stem('Parent', parax, 'YData',matlab.graphics.chart.internal.datachk(y(:,k)), xdata{:},..."
I think that the issue is w.r.t the size of YTest & YPred.
Try the plot and stem functions for subset of YTest & YPred i.e.,
stem(YPred(1:10) - YTest(1:10))
and check whether you are still facing the issue or not.
The "Out of memory" issue with stem function is similar to the "Out of Memory" issue of the following MATLAB Answer: Why do I receive an "Out of Memory" error when I attempt to render a surface with a large number of data points in MATLAB?

  0 Comments

Sign in to comment.

Tags

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!