MATLAB Answers

Time Series Forecasting Using Deep Learning in MATLAB

159 views (last 30 days)
Amin Karami
Amin Karami on 24 May 2018
Commented: Imran Khan on 13 Oct 2019
I am using the time series forecasting sample from MathWorks in: Time Series Forecasting Using Deep Learning
I only changed the dataset and ran the algorithm. Surprisingly, the algorithm is not working good with my dataset and generates a line as forecast as follows:
I am really confused and I cannot understand the reason behind that. I might be need to tune parameters in the algorithm that I am not aware on that. The code I am using is:
%%Load Data
%data = chickenpox_dataset;
%data = [data{:}];
data = xlsread('data.xlsx');
data = data';
%%Divide Data: Training and Testing
numTimeStepsTrain = floor(0.7*numel(data));
XTrain = data(1:numTimeStepsTrain);
YTrain = data(2:numTimeStepsTrain+1);
XTest = data(numTimeStepsTrain+1:end-1);
YTest = data(numTimeStepsTrain+2:end);
%%Standardize Data
mu = mean(XTrain);
sig = std(XTrain);
XTrain = (XTrain - mu) / sig;
YTrain = (YTrain - mu) / sig;
XTest = (XTest - mu) / sig;
%%Define LSTM Network
inputSize = 1;
numResponses = 1;
numHiddenUnits = 500;
layers = [ ...
sequenceInputLayer(inputSize)
lstmLayer(numHiddenUnits)
fullyConnectedLayer(numResponses)
regressionLayer];
%%Training Options
opts = trainingOptions('adam', ...
'MaxEpochs',500, ...
'GradientThreshold',1, ...
'InitialLearnRate',0.005, ...
'LearnRateSchedule','piecewise', ...
'LearnRateDropPeriod',125, ...
'LearnRateDropFactor',0.2, ...
'Verbose',0, ...
'Plots','training-progress');
%%Train Network
net = trainNetwork(XTrain,YTrain,layers,opts);
%%Forecast Future Time Steps
net = predictAndUpdateState(net,XTrain);
[net,YPred] = predictAndUpdateState(net,YTrain(end));
numTimeStepsTest = numel(XTest);
for i = 2:numTimeStepsTest
[net,YPred(1,i)] = predictAndUpdateState(net,YPred(i-1));
end
%%Unstandardize the predictions using mu and sig calculated earlier.
YPred = sig*YPred + mu;
%%RMSE and MAE Calculation
rmse = sqrt(mean((YPred-YTest).^2))
MAE = mae(YPred-YTest)
%%Plot results
figure
plot(data(1:numTimeStepsTrain))
hold on
idx = numTimeStepsTrain:(numTimeStepsTrain+numTimeStepsTest);
plot(idx,[data(numTimeStepsTrain) YPred],'.-')
hold off
xlabel("Month")
ylabel("Cases")
title("Forecast")
legend(["Observed" "Forecast"])
%%Compare the forecasted values with the test data
figure
subplot(2,1,1)
plot(YTest)
hold on
plot(YPred,'.-')
hold off
legend(["Observed" "Forecast"])
ylabel("Cases")
title("Forecast")
subplot(2,1,2)
stem(YPred - YTest)
xlabel("Month")
ylabel("Error")
title("RMSE = " + rmse)
And the data.xlsx is in: https://www.dropbox.com/s/vv1apug7iqlocu1/data.xlsx?dl=1
I really appreciate if there is any help.

  1 Comment

John Malik
John Malik on 20 Jul 2018
This is also happening to me. Looks like I have to start using Python for deep learning. BTW have you figured out the problem?

Sign in to comment.

Answers (5)

Abolfazl Nejatian
Abolfazl Nejatian on 8 Jun 2018
dear Amin well I think this might be happened because of your dataset Size, I mean you should use a big one Set or a smaller network. I'm working on time series prediction too in Forex; and I'm disagree with this kind of making input data and target data with one step delay!
if you have any question don't hastate to ask me.
with best wishes
Nejatian

  0 Comments

Sign in to comment.


xiaowei wang
xiaowei wang on 20 Nov 2018
It happened to me as well. I think it is just trick that no one wants to mention...

  0 Comments

Sign in to comment.


Abolfazl Nejatian
Abolfazl Nejatian on 23 Nov 2018
here is my code,
this piece of code predicts time series data by use of deep learning and shallow learning algorithm.
best wish
abolfazl nejatian

  3 Comments

xiaowei wang
xiaowei wang on 23 Nov 2018
Hi Abolfazi,
Thanks for the sharing. I tested it and I can see my prediction result has one time step lag with the history data. Any idea why this could happen?
400 test set.png
Regards,
Xiaowei
Li Chuang
Li Chuang on 22 Jul 2019
I came across the same condition, have you already solve this question ? thanks for sharing.
Chuang
Imran Khan
Imran Khan on 13 Oct 2019
Have you got any solution for this problem?? Please share

Sign in to comment.


qizal ashfaq
qizal ashfaq on 14 Sep 2019
Edited: qizal ashfaq on 14 Sep 2019
How to link this code with deep designer toolbox?I am talking about this.How this model works with that code?Capture.JPG

  0 Comments

Sign in to comment.


qizal ashfaq
qizal ashfaq on 14 Sep 2019
Is this code valid for only one row ?

  0 Comments

Sign in to comment.

Sign in to answer this question.