Hi @ massimo giannini,
In LSTM networks, maintaining the correct state dimensions is crucial when making successive predictions. When you use predict, it expects the input shape to align with what it was trained on. Instead of resetting the network at each prediction step, pass the correct state dimensions into your predictions consistently. The typical approach is to initialize the state from your last prediction and update it iteratively. Here is an adjusted version of your loop that maintains state consistency without resetting:
X = (XTest);
T = YTest;
offset = length(X);
[Z,state] = predict(net2,X(1:offset)); % Initial prediction
net2.State = state;
% Prepare for multi-step ahead forecasting
numPredictionTimeSteps = 5;
% Adjust Y's size based on Z
Y = zeros(size(Z, 1), numPredictionTimeSteps);
Y(:, 1) = Z(end); % Use last forecast as starting point
for t = 2:numPredictionTimeSteps
% Predict using previous output
[Y(:, t), state] = predict(net2, Y(:, t-1));
net2.State = state; % Update the network state
end
Also, if your LSTM expects a specific input size or format (e.g., a column vector vs. a matrix), ensure that `Y(:, t-1)` matches those expectations. Based on observation of your code, you may want to explore other forecasting strategies such as using ensemble methods or combining LSTM outputs with other models to improve robustness in predictions. Hope this helps. Please let me know if you have any further questions.