Error NARX Model Prediction

Hello everyone...
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 8 years. It doesn't give any predictions in the code I wrote... Would you mind to help me ? Where is the mistake :( My 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;

댓글 수: 3

Shivansh
Shivansh 2024년 3월 22일
Hello Seda!
The prediction code is included in the shared code snippet. If you encounter any errors while using it, share the error message and some sample data so I can replicate the issue on my side.
Seda
Seda 2024년 4월 18일
First of all, thank you very much for your return.
I didn't get an error. I'm just not sure if my codes are correct.
Seda
Seda 2024년 4월 18일
Is the code I use to predict the future correct? @Shivansh

댓글을 달려면 로그인하십시오.

답변 (1개)

Purvaja
Purvaja 2025년 8월 26일
편집: Purvaja 2025년 8월 26일

0 개 추천

Hi @Seda,
I understand that you’re using a NARX neural network for predicting carbon emissions with multiple inputs. You trained the model, but when trying to forecast the next 8 years, your code didn’t produce results. The code that you had provided is correct, it just had incomplete loop, so in my dummy code I completed it and printing the results as below.
Here’s the changes I made on my side:
  1. Prediction loop:
for k = 1:numFutureSteps
[nextOutput, lastInputState, lastLayerState] = netc(lastOutput, lastInputState, lastLayerState);
predictedValues{k} = nextOutput;
lastOutput = nextOutput;
end
I have removed some redundant code and variables to make it straightforward like above.
2. Convert predictions into numeric values for display:
predictedValuesNumeric = cellfun(@(c) c, predictedValues);
disp('Predicted carbon emissions for next 8 years:');
disp(predictedValuesNumeric);
The predictions are stored in cells, so you need cellfun to extract the actual numbers before displaying them.
Dummy results I got after implementation:
Training performance: 0.040058
Closed-loop performance: 0.16581
Predicted carbon emissions for next 8 years:
0.0766 3.8152 -0.7831 -0.0690 -0.7864 -0.3953 -0.3675 -0.2086
For better clarification regarding the functions I used, you can go through following links:
  1. Preparets: https://www.mathworks.com/help/deeplearning/ref/preparets.html
  2. Cellfun: https://www.mathworks.com/help/matlab/ref/cellfun.html
I hope this solves your doubt!

카테고리

도움말 센터File Exchange에서 Sequence and Numeric Feature Data Workflows에 대해 자세히 알아보기

제품

릴리스

R2023b

질문:

2024년 3월 2일

편집:

2025년 8월 26일

Community Treasure Hunt

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

Start Hunting!

Translated by