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

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

 Respuesta aceptada

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 comentarios

@LeoAiE thanks for your kind reply. I got YPredictedMatrix of size 1,18. You have not used nPredict variable in your code? How can I get next 5 steps predictions of my data varaible? I used predict function in different form however it give me error which i posted above?
I found a different in training network from u and over this website. This tutorial used this method of training
% Prepare the data for training
[Xs,Xi,Ai,Ts] = preparets(net,{},{},T);
While you have used this method of training
% Prepare the data for training
[xs, xi, ai, ts] = preparets(net, X, {}, T)
Why you have used x as non-feedback input inpreparets? Is this mean, in training network, my previous input will not used as input for next data in timeseries? Am I right or not? While refer website leave it empty in training what is the difference why you do that dear?
@LeoAiE and one more explaination from your kind code, why you used this inputs in this train
net = narxnet(inputDelays, feedbackDelays, hiddenLayerSize)
inputDelays and feedbackDelays are same vector in your case Why you use this? According to documentation it should be like this
narnet(feedbackDelays,hiddenSizes,feedbackMode,trainFcn)
why you used inputDelays, and not given feebbackmode and trainFcn? I want to used different trainFcn available on document but that line ask me to set feedbackMode as 'open','closed','non'. I have single timeseries vector I want to get future values by giving input of available timeseries data? Is closed option will be fine for me or someother please guide me what is best for me please? Thank You
you have to share your code with us to be able to help you troubleshoot and overcome the obstacle that you are facing, these are just examples to help you start. Regarding the use of inputDelays and feedbackDelays, the narxnet function can also accept two separate delay vectors for input and feedback, as shown in the documentation. However, you can certainly follow the example from the MathWorks documentation and use the narnet function instead.Now the code uses the narnet function and follows the example in the MathWorks documentation. The feedbackMode is set to 'open' as it is more suitable for time series prediction tasks. If you want to change the training function, simply replace 'trainlm' with your desired training function.
% Load data
load('Test_1.mat');
dataSeries = data;
% Prepare input and target data
T = num2cell(dataSeries, 1);
% Define and train the NARX network
feedbackDelays = 1:2;
hiddenLayerSize = 10;
net = narnet(feedbackDelays, hiddenLayerSize, 'open', 'trainlm');
% 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);
% 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, {}, {}, 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
predictedSeries = [dataSeries; YPredictedMatrix(end-nPredict+1:end)];
disp(predictedSeries);
@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?
@LeoAiE respected sir; Please respond dear?

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Community Treasure Hunt

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

Start Hunting!

Translated by