# Multi step ahead forecasting with LSTM

26 views (last 30 days)
How to perform multi-step ahead forecasting with LSTM. I want to predict 2,3, and 4 time stesp ahead prediction with LSTM?

Forecasting is basicaly sequence-to-sequence regression, let suppos that your entire sequence is data,
1. You divide data into train and test parts, you can specify the proportion as you wish:
numTimeStepsTrain = floor(0.9*numel(data));% 90% for training 10%for testing
dataTrain = data(1:numTimeStepsTrain+1);
dataTest = data(numTimeStepsTrain+1:end);
2. Preparing training data and response sequences by shifting data by one time step, such as for data(t) the response will be data(t+1)
XTrain = dataTrain(1:end-1);
YTrain = dataTrain(2:end);
3. Preparing the network and training hyperparameters, then train the network using training data and training responses
numFeatures = 1;
numResponses = 1;
numHiddenUnits = 200;
layers = [ ...
sequenceInputLayer(numFeatures)
lstmLayer(numHiddenUnits)
fullyConnectedLayer(numResponses)
regressionLayer];
'MaxEpochs',250, ...
'InitialLearnRate',0.005, ...
'Verbose',0, ...
'Plots','training-progress');
net = trainNetwork(XTrain,YTrain,layers,options);
3. Now you can forecast 1, 2, 3 or 4 steps ahead using predictAndUpdateState function, since you use predicted values to update the network state and you don’t use actual values contained in dataTest for this, you can make predictions on any time step number
stepsAhead = 4; % you can use 1,2,3,4 on any value of steps ahead
end

#### 1 Comment

anurag kulshrestha on 30 May 2020
Thanks. But your code is preciting only 4 steps ahead. I have a test set with 30 data points (lets assume). This code is using only the one predicted value to generate one step ahead predicitons. But, if i want to utilize past 2 or 3 or 4 predicted values to generate one step ahead forecasting like mutli-step? WIll this code work?