How to perdict timeseries using already trained net from nnstart tool in Matlab

8 views (last 30 days)
Through google I find nnstart tool where I can predict my timeseries dataset, I opened this app and seletec NAR option then select target as martrix column, then after partition my data I used Levenberg-Marquardt as training algothim. After getting best training and validation results I save the model output in my workspace. I want to predict from this train net 5 years ahead of my timeseries data in data variable however whenever I run predict or predictAndUpdate my code below error:
>> predict(net,data)
Error using predict (line 126)
No valid system or dataset was specified.
>> predictAndUpdateState(net,data)
Undefined function 'predictAndUpdateState' for input arguments of type 'network'.
I've called my timeseries data from excel file to matrix data (1,20 size) and want to get predicted data(1,25 size) using this tool. I am also sharing my workspace variables contain input variable data and output variables from train model using nntool (error,info,net,output, results) please guide me how can I predict my data variable 5 steps ahead using this train network? Many thanks for timely reply please

Accepted Answer

LeoAiE
LeoAiE on 13 May 2023
here is what I did:
% Load data
load('Test_1.mat');
dataSeries = data;
% Prepare input and target data
X = num2cell(dataSeries, 1);
T = X;
% Define and train the NARX network
inputDelays = 1:2;
feedbackDelays = 1:2;
hiddenLayerSize = 10;
net = narxnet(inputDelays, feedbackDelays, hiddenLayerSize);
% Divide data into training, validation, and testing sets
net.divideFcn = 'divideblock';
net.divideParam.trainRatio = 70/100;
net.divideParam.valRatio = 15/100;
net.divideParam.testRatio = 15/100;
% Prepare the data for training
[xs, xi, ai, ts] = preparets(net, X, {}, T);
% Train the NARX network
[net, tr] = train(net, xs, ts);
% Make predictions
nPredict = 5; % Number of steps ahead to predict
netc = closeloop(net);
[xc, xi, ai, tc] = preparets(netc, X, {}, T);
YPredicted = netc(xc, xi, ai);
% Convert the predicted output back to a matrix format
YPredictedMatrix = cell2mat(YPredicted);
% Transpose the YPredictedMatrix
YPredictedMatrix = YPredictedMatrix';
% Create a new variable to store the original data and the predicted values
  6 Comments
Muhammad Usman Saleem
Muhammad Usman Saleem on 14 May 2023
@LeoAiE I am using this code according to this documentation:
%https://www.mathworks.com/help/deeplearning/ref/narnet.html
load('Test_1.mat');
dataSeries = data;
% Prepare input and target data
X = num2cell(dataSeries, 1);
T = X;
% Define and train the NARX network
inputDelays = 1:2;
feedbackDelays = 1:2;
hiddenLayerSize = 10;
trainFcn='trainlm';
net = narxnet(inputDelays, feedbackDelays, hiddenLayerSize,'closed',trainFcn);
% Divide data into training, validation, and testing sets
net.divideFcn = 'divideblock';
net.divideParam.trainRatio = 70/100;
net.divideParam.valRatio = 15/100;
net.divideParam.testRatio = 15/100;
% Prepare the data for training
[Xs,Xi,Ai,Ts] = preparets(net,{},{},T);
net = train(net,Xs,Ts,Xi,Ai); %@LeoAiE you used just Xs,Ts only why?
%view(net)
[Y,Xf,Af] = net(Xs,Xi,Ai);
perf = perform(net,Ts,Y)
What is performance in above code? Is its RMSE or R2 or something else? Above mentioned documentation used this method to findout next five values in future
%To predict the output for the next 20 time steps
[netc,Xic,Aic] = closeloop(net,Xf,Af);
%view(netc)
%give an empty array according to documentation available on website
Yc = netc(cell(0,5),Xic,Aic);
YPredictedMatrix = cell2mat(Yc);
% Transpose the YPredictedMatrix
YPredictedMatrix = YPredictedMatrix';
@LeoAiE Sir, your kind way to find out next 5 values is given as below:
% Make predictions
nPredict = 5; % Number of steps ahead to predict
netc = closeloop(net);
[xc, xi, ai, tc] = preparets(netc, {}, {}, T);
YPredicted is same size of xc (1 by 18)
YPredicted = netc(xc, xi, ai);
% Convert the predicted output back to a matrix format
YPredictedMatrix = cell2mat(YPredicted);
% Transpose the YPredictedMatrix
YPredictedMatrix = YPredictedMatrix';
% Create a new variable to store the original data and the predicted values
predictedSeries = [dataSeries; YPredictedMatrix(end-nPredict+1:end)];
and in this code below you are just taking last 5 values in YPredictedMatrix as my 5 steps next values (means 21 to 25 for dataseries)?
Please check YPredictedMatrix size is 18,1 and out of 18 you are taking last 5 values as next 5 predicted values of dataseries? May I right or not? Please check the above method shared in documentation it suggest to give an empty cell array of size for which you want to get prediction respected sir?

Sign in to comment.

More Answers (0)

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!