MATLAB Answers

Timeseries prediction using LSTM

31 views (last 30 days)
Milton Logothetis
Milton Logothetis on 21 Jan 2021
Answered: Vineet Joshi on 22 Apr 2021
Hello everyone,
I am trying to implement MATLAB's LSTM example for timeseries forecasting.
While I got the code to run on my univariate data, I receive undesired results.
I have seen it is general practise when training LSTM's to create a feature vector of the responses (YTrain) such that the network tries to predict the next datapoint from the previous "n" (e.g. 6) timeseries datapoints.
MATLAB's example uses the observations and the responses lagged by 1 unit apart, such that the previous datapoint predicts the next:
XTrain = dataTrainStandardized(1:end-1);
YTrain = dataTrainStandardized(2:end);
I tried creating my response matrix such that:
lookback = 6;
lag = 1;
XTrain = dataTrainStandardized(1:end-1)
YTrain = zeros(length(dataTrainStandardized)-lag,lookback);
for i = 1+lag:length(dataTrainStandardized)-lookback
YTrain(i-1,:) = dataTrainStandardized(i:i+lookback-1);
However I get the error:
Error using trainNetwork (line 183)
Number of elements must not change. Use [] as one of the size inputs to automatically calculate the appropriate size for that
Which is reasonable since XTrain is a [1 x i] vector and YTrain is a [i x lookback] matrix .
Can anyone help? How can I train my LSTM to derive the predictions from the previous "n" datapoints and not just the last?Maybe this isn't possible with MATLAB's Deep Learning Toolbox.

Answers (2)

Arthur B
Arthur B on 8 Mar 2021
Hi Milton,
Same issue here : I'd like to perform sequence-to-sequence regression or classification, but I beleive that the LSTM looks back at ALL the previous timesteps, and I would like to limit it looking at only a few (say 10) timesteps.
My timeserie is thousands of timesteps long, but I know that those older than 10 timesteps have no effect on the output.
Since I found no option to limit the number of lookback timesteps, I thought like you I could duplicate my timeserie into multiple features which would just represent a window of 10 running timesteps, with one output (and therefore perform a sequence-to-one regression).
Have you been able to progress with your problem?
Can anyone confirm the proposed way forward is the best, or if there is better?

Community Treasure Hunt

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

Start Hunting!