MATLAB Answers

LSTM time series forecasting with 3 inputs using chickenpox example

77 views (last 30 days)
jeonga ryu
jeonga ryu on 6 May 2019
Answered: Ismaeel etwa 2 Stunden ago
Please let me know how to apply 3 inputs for the time series forecasting using LSTM example below.
chickenpox example link:
In this example, input is one(numFeatures = 1;) and I'm having trouble with changing the codes for the multiple inputs.
I'm not sure with the data input structures, and following codes for it.
It would be appreciate to provide the example or explanation about it.
Thank you.

Sign in to comment.

Answers (2)

Jon Cherrie
Jon Cherrie on 2 May 2021
Rather than starting from the chickenpox example, you might be better off starting from the "Sequence-to-Sequence Regression Using Deep Learning" example:
In that case, the input is from 26 sensors and there is one output, an estimate of the remaining useful life of the engine.
Jon Cherrie
Jon Cherrie etwa 20 Stunden ago
Here's anothe example. Does it help with what you are looking for?
This is based on pieces from the following examples.
The data is taken from the first one and LSTM settings are inspired by the second.
Load Data
The data is a bivariate time series consisting of 1-predator 1-prey populations (in thousands) collected 10 times a year for 20 years. For more information about the data, see Three Ecological Population Systems: MATLAB and C MEX-File Modeling of Time-Series.
predPreyCrowding = load("PredPreyCrowdingData").y.';
Use the first 120 time steps for training the model and the rest of the data to test the model
trainingData = predPreyCrowding(:,1:120);
testData = predPreyCrowding(:,121:end);
title("Species 1")
legend("Training Data", "Test Data")
title("Species 2")
legend("Training Data", "Test Data")
Prepare Training Data
In this case, the model will take one time step as input and provide the next time step as output. Prepare the data into this by creating a 'output' (y) that is one time step ahead of the 'input' (x)
XTrain = trainingData(:,1:end-1);
YTrain = trainingData(:,2:end);
Define Model
For the model, use an LSTM with two inputs (numFeatures = 2) and two outputs (numResponses = 2). That is one input and one output for each species.
The number of hidden units controls the complexity of the model.
numFeatures = 2;
numResponses = 2;
numHiddenUnits = 10;
layers = [
Train Model
options = trainingOptions('adam', ...
'MaxEpochs',100, ...
'GradientThreshold',1, ...
'InitialLearnRate',0.005, ...
'LearnRateSchedule','piecewise', ...
'LearnRateDropPeriod',125, ...
'LearnRateDropFactor',0.2, ...
'Verbose',0, ...
net = trainNetwork(XTrain,YTrain,layers,options);
Run Inference: Predict the Future
net = resetState(net);
The network is initialized to work from the start of the training set. Before making predictions beyond the training set, need to run inference up to the point the test data starts:
[net,YBefore] = predictAndUpdateState(net,trainingData);
The last of the predictions from inputs 'before' is the first of predictions beyond the training data
YPred = YBefore(:,end);
Because of the way the model is doing one step ahead a prediction, the output from one time step, becomes the input to the next time step
numTimeStepsTest = 81;
for i = 2:numTimeStepsTest
XThis = YPred(:,i-1);
[net,YNext] = predictAndUpdateState(net,XThis);
YPred(:,i) = YNext;
% Plot
title("Species 1")
legend("Training Data", "Test Data", "Prediction", "Forecast")
title("Species 2")
legend("Training Data", "Test Data", "Prediction", "Forecast")
Compare with predictions running on the whole time series, not just beyond the training data
net = resetState(net);
[net, YPred] = predictAndUpdateState(net,predPreyCrowding(:,1));
for i = 2:201
[net,YPred(:,i)] = predictAndUpdateState(net,YPred(:,i-1));
% Plot
title("Species 1")
legend("Training Data", "Test Data", "Predictions")
title("Species 2")
legend("Training Data", "Test Data", "Predictions")

Sign in to comment.

Ismaeel etwa 2 Stunden ago
Thank you Jon Cherrie, for your help.
It works for me now (with an edit in the standerization and destanderization of the input-output data).
Thank you very much.


Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by