Multiple future timestep prediction of multivariate timeseries data in matlab
3 views (last 30 days)
Show older comments
I have past 27 years dataset of a target and its corresponding predictors variables. I have trained my network and test it as well now I want to predict from this trained network next 5 timesteps of my target how can I do that? I am doing
layers = [
sequenceInputLayer(size(predictors, 2)) % Input layer for sequences
fullyConnectedLayer(128)
reluLayer
fullyConnectedLayer(1)
regressionLayer];
% Set up training options
options = trainingOptions('adam', ...
'MaxEpochs', 1000, ...
'MiniBatchSize', 128, ...
'Verbose', true);%, ...
% 'Plots', 'training-progress'); % Disable training progress plot
% Train the neural network
net = trainNetwork(trainPredictors, trainTarget', layers, options);
% Test the trained network on the testing data
predictions = predict(net, testPredictors);
rmse = sqrt(mean((testTarget - predictions.').^2,'all'));
Approch No 1: For predicting 5 timestep ahead from this trained network I am using close loop approch:
% Number of steps to forecast into the future
numForecastSteps = 5;
TrTestPredictors=testPredictors;
%Closed Loop Forecasting
net = resetState(net);
% % offset = size(testPredictors,2);
% lastTrainData = TrTestPredictors(end, :).';
% Preallocate an array to store the forecasted target variable values
forecastedTarget = zeros(numForecastSteps, 1);
%
% forecast = predict(net, lastTrainData,'5');
% % Forecast future time steps using the best trained network
for t = 1:numForecastSteps
% Predict the next time step using the best trained network
[net,forecastedTarget(t)] = predictAndUpdateState(net, lastTrainData);
% Store the forecasted value
forecast = forecastedTarget(t);
% Update lastTrainData for the next iteration (shift one time step)
lastTrainData = [lastTrainData(2:end); forecast];
end
This gives me abnormal results. The reason for it forecastedTarget is target variable and at the end I merging it to lastTrainData which are my predictors.
Approch No 2: I am using another approch to resolve my problem in which I produced my target values from testPredictors and then using this predicted target values as input to the model trained on multivariate predictors to predict future timesteps:
ValidPredictors= predictors(end-8:end,:);
% Number of steps to forecast into the future
numForecastSteps = 5;
TrTestPredictors=ValidPredictors.';
net = resetState(net);
[net,Z] = predictAndUpdateState(net,TrTestPredictors);
% Last portion of the training data to be used as initial input for forecasting
lastTrainData = Z(end, :).'; % produce targets from predictors
%
% Preallocate an array to store the forecasted target variable values
forecastedTarget = zeros(numForecastSteps, 1);
% % Forecast future time steps using the best trained network
for t = 1:numForecastSteps
% Predict the next time step using the best trained network
[net,forecastedTarget(t)] = predictAndUpdateState(net, lastTrainData);
% Store the forecasted value
forecast = forecastedTarget(t);
% Update lastTrainData for the next iteration (shift one time step)
lastTrainData = [lastTrainData(2:end); forecast];
end
Here is the details of size of my input variables:
Please help me to resolve this problem?
2 Comments
Venu
on 10 Jan 2024
Your second approach is more aligned with common practices for time series forecasting. However, still if you are getting 53.6242 rmse, I would suggest you to do some data preprocessing (i.e scaling, normalization). Compare this rmse with the training rmse to identify whether overfitting is happening.
Experiment with the network architecture by adding more layers to increase complexity, changing the number of neurons, or using different types of layers (such as LSTM or GRU layers if you're dealing with time-series data).
If you have a small dataset, the model may not learn effectively. Consider collecting more data or using data augmentation techniques if applicable. Can you provide your data? It will help investigate further.
Answers (0)
See Also
Categories
Find more on Sequence and Numeric Feature Data Workflows in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!