How to continuously train neural network in Simulink?

3 views (last 30 days)
I have a MATLAB Function block in Simulink that I want to use to create and train a neural network. It inputs training and target data along with a percentage of the file used for testing.
I want the "net" network variable to be the same network that is continuously retrained with every new file. I think I can do this by outputing the network variable and keep passing and saving it with each new file, but I'm running into an error.
When I try to output the new_net variable, I get the error...
Function output 'new_net' cannot be an mxArray in this context. Consider preinitializing the output variable with a known type.
In another testing file, the new_net variable is a 1x1 network with the following attributes. I'm not sure how to initialize the new_net variable.
val =
Neural Network
dimensions:
connections:
subobjects:
input: Equivalent to inputs{1}
output: Equivalent to outputs{2}
functions:
weight and bias values:
methods:
But even if I can output "net", I don't know if I'll get the "retraining neural network" I need. Here is the code. Does anyone know how to do this in Simulink? Thank you for your help.
function new_net = fcn(target_data, input_data, test_percent)
coder.extrinsic('feedforwardnet')
coder.extrinsic('train')
numobservations = size(target_data,2);
idxrand = transpose(randperm(numobservations,ceil(numobservations*test_percent)));
train_target = target_data;
train_input = input_data;
% keeps the random indexes for training data
train_target(:,idxrand) = [];
train_input(:,idxrand) = [];
net = feedforwardnet(size(input_data,1));
[net, tr] = train(net, train_input, train_target);
new_net = net;

Answers (1)

Nithin
Nithin on 25 Mar 2025
Hi @Jack,
The issue arises because Simulink requires a fixed-size and type for the outputs of a MATLAB Function block, and "new_net" is currently being returned as an "mxArray", which is not directly supported. The first step is to initialize "new_net" with a known type. This can be achieved by creating a persistent variable that holds the network and is initialized only once. Persistent variables in MATLAB retain their values between function calls, making them perfect for storing the neural network that you want to update continuously with each new set of data.
Next, you need to ensure that the output is pre-initialized with a known type. You can explicitly specify the output data type and dimensions using "coder.varsize" if necessary. This helps Simulink understand what kind of data it should expect from the MATLAB Function block. In addition, declare the functions "feedforwardnet" and "train" as "extrinsic" functions. This is necessary because these functions are not supported for code generation by Simulink, but you can still call them in interpreted mode to perform the neural network operations. Kindly refer to the following documentation to know more about "extrinsic” functions: https://www.mathworks.com/help/simulink/slref/coder.extrinsic.html
Hope this resolves your issue.

Categories

Find more on Deep Learning Toolbox in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!