Time series prediction using LSTM
43 views (last 30 days)
Show older comments
Mustafa Al-Nasser on 31 Oct 2019
Answered: AMMAR ATIF on 17 Aug 2022
I am trying to build an LSTM model to prodict the repsone of time series (deterministic) but the result is not good at all .
i try to change the parameters but still i can get good results. could you help how can i imporve the results
The code is below and i attached the data.
numTimeStepsTrain = floor(0.95*numel(data));
dataTrain = data(1:numTimeStepsTrain+1);
dataTest = data(numTimeStepsTrain+1:end);
mu = mean(dataTrain);
sig = std(dataTrain);
dataTrainStandardized = (dataTrain - mu) / sig;
XTrain = dataTrainStandardized(1:end-1);
YTrain = dataTrainStandardized(2:end);
numFeatures = 1;
numResponses = 1;
numHiddenUnits = 100;
layers = [ ...
options = trainingOptions('adam', ...
net = trainNetwork(XTrain,YTrain,layers,options);
dataTestStandardized = (dataTest - mu) / sig;
XTest = dataTestStandardized(1:end-1);
net = predictAndUpdateState(net,XTrain);
[net,YPred] = predictAndUpdateState(net,YTrain(end));
numTimeStepsTest = numel(XTest);
for i = 2:numTimeStepsTest
[net,YPred(:,i)] = predictAndUpdateState(net,YPred(:,i-1),'ExecutionEnvironment','cpu');
YPred = sig*YPred + mu;
YTest = dataTest(2:end);
rmse = sqrt(mean((YPred-YTest).^2))
idx = numTimeStepsTrain:(numTimeStepsTrain+numTimeStepsTest);
stem(YPred - YTest)
title("RMSE = " + rmse)
Shashank Gupta on 11 Dec 2019
While working on LSTM, we cannot have a final, definite, rule of thumb on how many layers or nodes or hidden neuron/units one must choose, this are all hyperparameter and very often a trail and error approach will give you the considerable better results. The most common framework people use is “K-fold Validation”. Maybe you should consider looking at it.
Every LSTM layer should be accompanied by a Dropout layer. It helps to prevent from overfitting. For choosing the optimizer, adaptive moment estimation or ADAM works well. Also MATLAB provide a way to get the optimal hyperparameter for training models, May be this link give you an idea of how to approach the problem.
Hope this helps.
lotus whit on 23 Oct 2021
Edited: lotus whit on 23 Oct 2021
can you please to specify the minmum number of data (rows), to get a good reult of prediction ,because i have 33 entry(as time series from 1988:2012), but the result varied when i tried to duplicate the value to get good predictor?
AMMAR ATIF on 17 Aug 2022
Reduce the LearnRateDropFactor, you can make it 0.1 and increase the number of epochs to 1000 as long as the training time is only 2 mins, the obtained RMSE error is 9.2668e-06, which is perfect !!
Find more on Sequence and Numeric Feature Data Workflows in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!