Using NARX model with Neural Network Prediction

15 次查看(过去 30 天)
Hello,
Im doing a carbon emission(output) with multiple input using neural network approached (NARX). I trained the model using NARX tollbox. I couldn't find the code I needed to write to predict the next 5 years.
Can you help me?
  1 个评论
Seda
Seda 2023-11-10
My code;
% Solve an Autoregression Problem with External Input with a NARX Neural Network
% Script generated by Neural Time Series app
% Created 10-Nov-2023 12:42:04
%
% This script assumes these variables are defined:
%
% x - input time series.
% t - feedback time series.
X = tonndata(x,false,false);
T = tonndata(t,false,false);
% Choose a Training Function
% For a list of all training functions type: help nntrain
% 'trainlm' is usually fastest.
% 'trainbr' takes longer but may be better for challenging problems.
% 'trainscg' uses less memory. Suitable in low memory situations.
trainFcn = 'trainlm'; % Levenberg-Marquardt backpropagation.
% Create a Nonlinear Autoregressive Network with External Input
inputDelays = 1:2;
feedbackDelays = 1:2;
hiddenLayerSize = 10;
net = narxnet(inputDelays,feedbackDelays,hiddenLayerSize,'open',trainFcn);
% Prepare the Data for Training and Simulation
% The function PREPARETS prepares timeseries data for a particular network,
% shifting time by the minimum amount to fill input states and layer
% states. Using PREPARETS allows you to keep your original time series data
% unchanged, while easily customizing it for networks with differing
% numbers of delays, with open loop or closed loop feedback modes.
[x,xi,ai,t] = preparets(net,X,{},T);
% Setup Division of Data for Training, Validation, Testing
net.divideParam.trainRatio = 80/100;
net.divideParam.valRatio = 10/100;
net.divideParam.testRatio = 10/100;
% Train the Network
[net,tr] = train(net,x,t,xi,ai);
% Test the Network
y = net(x,xi,ai);
e = gsubtract(t,y);
performance = perform(net,t,y)
% View the Network
view(net)
% Plots
% Uncomment these lines to enable various plots.
%figure, plotperform(tr)
%figure, plottrainstate(tr)
%figure, ploterrhist(e)
%figure, plotregression(t,y)
%figure, plotresponse(t,y)
%figure, ploterrcorr(e)
%figure, plotinerrcorr(x,e)
% Closed Loop Network
% Use this network to do multi-step prediction.
% The function CLOSELOOP replaces the feedback input with a direct
% connection from the output layer.
netc = closeloop(net);
netc.name = [net.name ' - Closed Loop'];
view(netc)
[xc,xic,aic,tc] = preparets(netc,X,{},T);
yc = netc(xc,xic,aic);
closedLoopPerformance = perform(net,tc,yc)
% Step-Ahead Prediction Network
% For some applications it helps to get the prediction a timestep early.
% The original network returns predicted y(t+1) at the same time it is
% given y(t+1). For some applications such as decision making, it would
% help to have predicted y(t+1) once y(t) is available, but before the
% actual y(t+1) occurs. The network can be made to return its output a
% timestep early by removing one delay so that its minimal tap delay is now
% 0 instead of 1. The new network returns the same outputs as the original
% network, but outputs are shifted left one timestep.
nets = removedelay(net);
nets.name = [net.name ' - Predict One Step Ahead'];
view(nets)
[xs,xis,ais,ts] = preparets(nets,X,{},T);
ys = nets(xs,xis,ais);
stepAheadPerformance = perform(nets,ts,ys)

请先登录,再进行评论。

回答(1 个)

Venu
Venu 2023-11-29
Hi @Seda,
I understand you want to predict next 5 years output. For that we need to know format of your input time series dataset. For example you have the data of 10 months, 1 month will be the predicted output time series since your test data is 1/10th. So you need to predict for next 60 months i.e 5 years.
You can find the below example code as reference which is like extention to your code.
% Number of future time steps to predict
numFutureSteps = 60; % 5 years of monthly predictions
% cell array for the predicted values
predictedValues = cell(1, numFutureSteps);
% Initialize the prediction loop state with the last values of the input and layer states
[~, lastInputState, lastLayerState] = preparets(netc, X, {}, T);
lastOutput = yc{end}; % last predicted output from the closed loop network
% predict into the future, one step at a time
for i = 1:numFutureSteps
% use the last predicted output as the current input for the prediction
[nextOutput, nextInputState, nextLayerState] = netc({lastOutput}, lastInputState, lastLayerState);
% store the predicted value
predictedValues{i} = nextOutput;
% Update the last output and states for the next iteration
lastOutput = nextOutput;
lastInputState = nextInputState;
lastLayerState = nextLayerState;
end
% The predictedValues cell array now contains your 5-year predictions
  5 个评论
Seda
Seda 2024-3-2
@Venu And i wrote this code;
x=xlsread('Input.xlsx');
t=xlsread('Output.xlsx');
X = tonndata(x,false,false);
T = tonndata(t,false,false);
trainFcn = 'trainlm';
inputDelays = 1:2;
feedbackDelays = 1:2;
hiddenLayerSize = 10;
net = narxnet(inputDelays,feedbackDelays,hiddenLayerSize,'open',trainFcn);
[x,xi,ai,t] = preparets(net,X,{},T);
net.divideParam.trainRatio = 80/100;
net.divideParam.valRatio = 10/100;
net.divideParam.testRatio = 10/100;
[net,tr] = train(net,x,t,xi,ai);
y = net(x,xi,ai);
e = gsubtract(t,y);
performance = perform(net,t,y)
view(net)
netc = closeloop(net);
netc.name = [net.name ' - Closed Loop'];
view(netc)
[xc,xic,aic,tc] = preparets(netc,X,{},T);
yc = netc(xc,xic,aic);
closedLoopPerformance = perform(net,tc,yc)
nets = removedelay(net);
nets.name = [net.name ' - Predict One Step Ahead'];
view(nets)
[xs,xis,ais,ts] = preparets(nets,X,{},T);
ys = nets(xs,xis,ais);
stepAheadPerformance = perform(nets,ts,ys)
numFutureSteps = 8;
predictedValues = cell(1, numFutureSteps);
[~, lastInputState, lastLayerState] = preparets(netc, X, {}, T);
lastOutput = yc{end};
for i = 1:numFutureSteps
[nextOutput, nextInputState, nextLayerState] = netc({lastOutput}, lastInputState, lastLayerState);
predictedValues{i} = nextOutput;
lastOutput = nextOutput;
lastInputState = nextInputState;
lastLayerState = nextLayerState;
Venu
Venu 2024-3-7
Hi @Seda,
Your code seems good to me. Can you elaborate the issue or any error if you are facing with this approach?
Thanks

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Sequence and Numeric Feature Data Workflows 的更多信息

标签

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by