Time Series Forecasting Using Deep Learning in MATLAB
Mostrar comentarios más antiguos
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.
3 comentarios
John Malik
el 20 de Jul. de 2018
Editada: John Malik
el 20 de Jul. de 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?
diana haron
el 27 de Sept. de 2020
Hi, how does your data looks like? Is it similar as the chicken pox example which is just one row with multiple columns?
Rudha Khudhair Mohammed
el 31 de Mzo. de 2023
I have same problem let mw know if you fix it
Respuestas (6)
Abolfazl Nejatian
el 23 de Nov. de 2018
3 votos
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
7 comentarios
xiaowei wang
el 23 de Nov. de 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?

Regards,
Xiaowei
Li Chuang
el 22 de Jul. de 2019
I came across the same condition, have you already solve this question ? thanks for sharing.
Chuang
Imran Khan
el 13 de Oct. de 2019
Have you got any solution for this problem?? Please share
dinos adamakos
el 27 de Abr. de 2020
BE CAUTIOUS. As also commented from someone else on the files tou have uploaded "You are normalizing the data previously to split it into training and testing.
That implies you know the mean and the standard deviation of the testing set, what is false. You need to calculate these measures for the training set, what is what you will have in online processing, and later apply it to the testing set.
Calculating them over the whole data set allows the LSTM to access data that is actually unavailable"
Chris P
el 29 de Sept. de 2020
I'm getting similar flat line results when I use code based on the LSTM mathworks example but it looks like Abolfazi's code shows a big improvement even with the slight delay.
Any further updates on the results? Would love to see how to avoid the flat line predictions and delay.
Chris P
el 29 de Sept. de 2020
Also, are the predictions forecasting the values of multiple time steps in the future? Or are they updating the network state with the observed values rather than the predicted values?
Refer to the last two sections of this example for further details on what I am asking. I would like my network to have the ability to make predictions multiple timesteps into the future before accesing any new observed values.
Abolfazl Nejatian
el 10 de Dic. de 2020
Dear Chris,
i have updated my code.
the updated things are listed as below:
time-series prediction with simple CNN network added
time series prediction with a ResNet50 added.
forecasting the future of data added to the code, and also some minor bugs were fixed.
Abolfazl Nejatian
el 8 de Jun. de 2018
0 votos
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
3 comentarios
dinos adamakos
el 27 de Abr. de 2020
BE CAUTIOUS. As also commented on the files tou have uploaded "You are normalizing the data previously to split it into training and testing.
That implies you know the mean and the standard deviation of the testing set, what is false. You need to calculate these measures for the training set, what is what you will have in online processing, and later apply it to the testing set.
Calculating them over the whole data set allows the LSTM to access data that is actually unavailable"
diana haron
el 27 de Sept. de 2020
Hi,
Im currently working on something based on this chicken pox example. The data for this example is only 1 row and multiple columns. I am trying to modify it for my data sets which comprises of multple rows and multiple columns. Any chance you know how to do it? Much appreciated.
Faris
el 24 de Dic. de 2022
I sent an email including an equation at the same object and your answer will help me to implement my study
Regards
Faris
xiaowei wang
el 20 de Nov. de 2018
0 votos
It happened to me as well. I think it is just trick that no one wants to mention...
qizal ashfaq
el 14 de Sept. de 2019
Editada: qizal ashfaq
el 14 de Sept. de 2019
0 votos
How to link this code with deep designer toolbox?I am talking about this.How this model works with that code?

1 comentario
ANGELIN ANTHONY
el 21 de Mzo. de 2020
did u get this answer??
qizal ashfaq
el 14 de Sept. de 2019
0 votos
Is this code valid for only one row ?
1 comentario
Chris P
el 30 de Sept. de 2020
Same question. I'm wondering if I can use an external time series in addition to the time series of interest. I want these two time series inputs to have delays as well.
How should we format the input matrix? 2xn cell where n is the number of time samples in the series? 1xn cell where each cell is dimension 2xd where d is the number of delays?
CARLOS ANDRES MENDEZ VALLEJO
el 2 de Nov. de 2020
0 votos
excelent work
Categorías
Más información sobre Deep Learning Toolbox en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!