How to set the initial state of NARX networks during a Simulink simulation?

5 ビュー (過去 30 日間)
Luke135
Luke135 2018 年 8 月 3 日
回答済み: Aditya 2025 年 8 月 21 日 4:15
Hi all, I have two NARX neural networks (say NN1 and NN2) already trained and tested. In a Simulink model, I want to use NN1 or NN2 to produce a single output Out1. The choice between NN1 or NN2 is based on a control signal (or e.g. an if-if/action subsystem),and it changes during the simulation. The point is that, for example, when NN1 is "activated", it should be initialized with the last value of Out1 coming from NN2, which is now "deactivated". I managed to deploy my neural networks in Simulink, and initialize them using setsiminit. However I do not know how to "dynamically initialize" NN1 and NN2 during the simulation by setting their initial state value at each "activation" during the simulation. Any suggestions?
Thank you !

回答 (1 件)

Aditya
Aditya 2025 年 8 月 21 日 4:15
Hi Luke,
When switching between two trained NARX neural networks (NN1 and NN2) in Simulink, and you need the active network to initialize its state with the last output of the previously active network, you cannot achieve this directly with built-in NARX blocks, as they do not support dynamic state initialization during simulation. Instead, you should use a MATLAB Function block or a custom Level-2 MATLAB S-Function to explicitly manage the state (delays) of both networks. In this setup, both networks are loaded in the function, and their internal states are stored using persistent variables. When the control signal indicates a switch, the function sets the initial state of the newly activated network to the last output of the previously active network, ensuring a smooth transition. Below is a conceptual example using a MATLAB Function block, where you manually manage the switching and state transfer logic.
Following is a sample MATLAb function block code:
function out = switch_narx(u, control)
% u: current input, control: 1 for NN1, 2 for NN2
persistent NN1_state NN2_state NN1 NN2 last_output last_control
if isempty(NN1)
% Load your trained NARX networks (replace with your actual objects)
load('NN1.mat','net1'); % Assume net1 is your trained NARX object
load('NN2.mat','net2'); % Assume net2 is your trained NARX object
NN1 = net1;
NN2 = net2;
NN1_state = {}; % Initialize as needed
NN2_state = {};
last_output = 0;
last_control = 1;
end
if control ~= last_control
% Switching occurred: initialize the new network's state
if control == 1
% Switching to NN1
NN1_state = {last_output}; % Adjust to match your delay length
else
% Switching to NN2
NN2_state = {last_output}; % Adjust to match your delay length
end
end
% Prepare input and state for the selected network
if control == 1
% Simulate NN1 with current input and NN1_state as initial state
% For demonstration, assuming one-step-ahead prediction
[~,~,~,~,NN1_state] = NN1({u}, {}, NN1_state, {});
out = NN1({u}, {}, NN1_state, {});
else
% Simulate NN2 with current input and NN2_state as initial state
[~,~,~,~,NN2_state] = NN2({u}, {}, NN2_state, {});
out = NN2({u}, {}, NN2_state, {});
end
last_output = out;
last_control = control;
You can refer to the following documentation for further details;

カテゴリ

Help Center および File ExchangeSequence and Numeric Feature Data Workflows についてさらに検索

製品


リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by