Narx network in real-time task

4 views (last 30 days)
Andrey
Andrey on 17 Aug 2024
Edited: Andrey on 22 Aug 2024
Good afternoon,
I understand that the topic of NARX networks has been discussed here for over 10 years, but I would like to revisit it.
I attempted to implement a solution for processing data from a sensor in real-time, but unfortunately, I could not find any examples demonstrating how to do this correctly. Most publications on this topic either conclude with a performance evaluation of the network or focus on discussions of the code generated by the application.
Let me share some code:
% targer timeseries
T = num2cell(lfarr(1:8000));
% input timeseries from sensor
X = num2cell(source(1:8000));
The signals are extremely simple - data from the sensor and its smoothed version.
Next, I followed the standard steps outlined in the documentation and various discussions on this forum:
%create narxnet
sh = 7
d1 = [1:sh];
d2 = [1:sh];
narx_net = narxnet(d1,d2,20);
narx_netnet.divideFcn = 'divideblock';
(If Greg suddenly comes, tell him that I remember about autocorrelation and am already carefully studying his code from user group :))
% standard data preparation
[Xs,Xi,Ai,Ts] = preparets(narx_net,X,{},T);
% training
rng( 'default' )
[narx_net, tr, Ys, Es, Xf, Af ] = train(narx_net,Xs,Ts,Xi,Ai);
% performance evaluation
[Y,Xf,Af] = narx_net(Xs,Xi,Ai);
perf = perform(narx_net,Ts,Y) % perf = 3.4381e-08. I think it's not bad.
The regression diagram speaks for itself
Next I close the network and simulate signal processing on the remainder of the data that the network has not seen.
Here, I tried to base my approach on the last two paragraphs of the documentation - Following Closed-Loop Simulation with Open-Loop Simulation.
[narx_netc,Xic,Aic] = closeloop(narx_net, Xf, Af);
% number of prediction steps
k = 5;
% A loop that simulates a real-time data flow
for i = 8001:length(lf_arr)
% getting a new value from the sensor
newSensorValue = num2cell(source(i));
% local variable for indexing the prediction array
ii = 1;
% array of predictions
res = [];
% predictions for k steps
for j=i:i+k
[Yc, Xic, Aic] = narx_netc(newSensorValue, Xic, Aic);
% disp(['Predicted meaning: ', num2str(cell2mat(Yc))]);
res(ii) = cell2mat(Yc);
ii = ii+1;
end
% saving results
res1(i-8000,:) = res';
% form new input and target arrays with new input value and the predicted result
u = [X(2:end),newSensorValue];
y = [T(2:end), {res(1)}];
% repeating initialization cycle for next simulation step
[X1,Xi,Ai,T1] = preparets(narx_net,u,{},y);
[Y,Xio,Aio] = narx_net(X1,Xi,Ai);
[narx_netc,Xic,Aic] = closeloop(narx_net, Xio, Aio);
end
The network starts working, but after some steps its normal operation stops.
I would be immensely grateful for any insights into where my reasoning or actions might have gone astray.
Thanks

Accepted Answer

Aneela
Aneela on 19 Aug 2024
Hi Andrey,
If the network has stopped producing any output, it suggests a more fundamental issue that might be causing the network to fail or produce NaN/zero values.
These are the possible workarounds:
  • Add assertions or logging to check for NaN or Inf values in the network outputs and states.
  • Test the network with a smaller dataset or fewer steps to isolate the issue.
  • Ensure that newSensorValue is correctly updated at each step and that the network's state (Xic, Aic) is correctly maintained.
  • The new sensor value is fetched once per outer loop iteration. Use the latest prediction as new input in the inner loop. This simulates the real-time update more accurately by using the most recent prediction as input for the next step.
  1 Comment
Andrey
Andrey on 21 Aug 2024
Edited: Andrey on 22 Aug 2024
Thank you! Your answer pushed me in the right direction.
The inner loop is completely unnecessary.
Greg Heath wrote about this in one of his posts.
The prediction horizon is controlled by the size of the cell array fed to the network with a closed inner loop.
The fact that the network, working with a closed internal cycle, will sooner or later become unstable does not raise questions.
Now, it remains to understand how to correctly manipulate open and closed cycles when new data arrives.

Sign in to comment.

More Answers (0)

Categories

Find more on Sequence and Numeric Feature Data Workflows 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!