Closed loop with LSTM for time series
Dear All
I am in troubling trying to perform a multi-ahead (closed loop) forecasting for a time series. I use Matlab2024 and the "old" command predictAndUpdate does not work with dlnetwork objects. I saw all the possible documentation, theoretically I can manage the question but practically not!. I sum up the excercise. Single univariate time serie (stock price) in LSTM net. No problem with trainnet and predict using historical training and test data. Now I’d like to go out of test data. This is the code
X = (XTest);
T = YTest;
%net2 is a dlnetwork object
offset = length(X);
[Z,state] = predict(net2,X(1:offset)); %predict and update state using test data
net2.State = state;
% five-steps ahead
numPredictionTimeSteps = 5;
Y = (zeros(numPredictionTimeSteps));
Y(1,:) = Z(end); %use the last forecast as starting point for the loop
for t = 2:numPredictionTimeSteps
[Y(:,t),state] = predict(net2,Y(:,t-1));
net2.State = state;
end
%I got:
Error using extractState (line 41)
If the hidden state argument is a matrix, then its number of observations must match the number of observations of the
input data.
I found a similar question on the web for the GRU net. If I restart the net before the loop (as suggested) it works but the forecast is very poor, so I wonder whether the restart can affect such a result. Is there an alternative to reset? I attach the data and the net.
thanks in advanceDear All
I am in troubling trying to perform a multi-ahead (closed loop) forecasting for a time series. I use Matlab2024 and the "old" command predictAndUpdate does not work with dlnetwork objects. I saw all the possible documentation, theoretically I can manage the question but practically not!. I sum up the excercise. Single univariate time serie (stock price) in LSTM net. No problem with trainnet and predict using historical training and test data. Now I’d like to go out of test data. This is the code
X = (XTest);
T = YTest;
%net2 is a dlnetwork object
offset = length(X);
[Z,state] = predict(net2,X(1:offset)); %predict and update state using test data
net2.State = state;
% five-steps ahead
numPredictionTimeSteps = 5;
Y = (zeros(numPredictionTimeSteps));
Y(1,:) = Z(end); %use the last forecast as starting point for the loop
for t = 2:numPredictionTimeSteps
[Y(:,t),state] = predict(net2,Y(:,t-1));
net2.State = state;
end
%I got:
Error using extractState (line 41)
If the hidden state argument is a matrix, then its number of observations must match the number of observations of the
input data.
I found a similar question on the web for the GRU net. If I restart the net before the loop (as suggested) it works but the forecast is very poor, so I wonder whether the restart can affect such a result. Is there an alternative to reset? I attach the data and the net.
thanks in advance Dear All
I am in troubling trying to perform a multi-ahead (closed loop) forecasting for a time series. I use Matlab2024 and the "old" command predictAndUpdate does not work with dlnetwork objects. I saw all the possible documentation, theoretically I can manage the question but practically not!. I sum up the excercise. Single univariate time serie (stock price) in LSTM net. No problem with trainnet and predict using historical training and test data. Now I’d like to go out of test data. This is the code
X = (XTest);
T = YTest;
%net2 is a dlnetwork object
offset = length(X);
[Z,state] = predict(net2,X(1:offset)); %predict and update state using test data
net2.State = state;
% five-steps ahead
numPredictionTimeSteps = 5;
Y = (zeros(numPredictionTimeSteps));
Y(1,:) = Z(end); %use the last forecast as starting point for the loop
for t = 2:numPredictionTimeSteps
[Y(:,t),state] = predict(net2,Y(:,t-1));
net2.State = state;
end
%I got:
Error using extractState (line 41)
If the hidden state argument is a matrix, then its number of observations must match the number of observations of the
input data.
I found a similar question on the web for the GRU net. If I restart the net before the loop (as suggested) it works but the forecast is very poor, so I wonder whether the restart can affect such a result. Is there an alternative to reset? I attach the data and the net.
thanks in advance lstm closed loop MATLAB Answers — New Questions