MATLAB Answers

0

Time Series Forecasting Using Deep Learning in MATLAB

Asked by Amin Karami on 24 May 2018
Latest activity Commented on by Imran Khan on 13 Oct 2019 at 9:35
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

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.

5 Answers

Answer by 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.


Answer by 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.


Answer by 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

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
I came across the same condition, have you already solve this question ? thanks for sharing.
Chuang
Have you got any solution for this problem?? Please share

Sign in to comment.


Answer by qizal ashfaq on 14 Sep 2019
Edited by 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.


Answer by qizal ashfaq on 14 Sep 2019

Is this code valid for only one row ?

  0 Comments

Sign in to comment.