How to forecast with a CNN 2784 setps in the future after the last value in the time series

4 views (last 30 days)
Dear all, I trained a CNN that works well and validated it with the predict function. Then, I saved the trained model in the file v_1.mat, in the variable "trainedModel". Now, using the trained model, I want to forecast the next 2784 values found imediatelly after the last value in the time series that I used initially for training and validation. The actual data for comparison are in the file 'february_24.xlsx'. In a separate script, I use the folowing code but it says "Incorrect number or types of inputs or outputs for function forecast."
clc
load('v_1.mat');
past_data=xlsread('training_testing_data.xlsx','35233:38016');
yf=forecast(trainedModel,transpose(past_data),2784);
data_february=xlsread('february_24.xlsx','1:2784');
mape(transpose(data_february),yf)
figure(1)
plot(data_february, 'DisplayName', 'Actual');
hold on;
plot(yf, 'DisplayName', 'Predicted');
legend('show');
title('Forecast february 2024');
What am I doing wrong? Is the "forecast" function not compatible to a neural network model ? Is there any alternative to this code ?
Thank you all very much in advance!

Answers (1)

Vinayak
Vinayak on 16 May 2024
Hi Adrian,
The documentation for forecast says it accepts a linear/non-linear time-series based system model as input (here). I don’t have your model, but most CNNs are not in such format. I would suggest you use the predict function in a loop refeeding it the latest data for the next ‘n’ values.
The sample code might look something like:
numForecasts = 2784;
yf = zeros(1, numForecasts);
inputData = transpose(past_data);
for i = 1:numForecasts
% Predict the next value
nextValue = predict(trainedModel, inputData(:, end));
% Append the next value to the forecast array
yf(i) = nextValue;
% Update the input data by appending the predicted value
inputData = [inputData, nextValue];
end
You also might consider creating a custom forecast function which can perform similar calculation under the hood.
I hope this helps!

Categories

Find more on Parallel and Cloud in Help Center and File Exchange

Products


Release

R2024a

Community Treasure Hunt

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

Start Hunting!