The function `genFunction`

allows
stand-alone MATLAB^{®} functions for a trained neural network. The
generated code contains all the information needed to simulate a neural
network, including settings, weight and bias values, module functions,
and calculations.

The generated MATLAB function can be used to inspect the exact simulation calculations that a particular neural network performs, and makes it easier to deploy neural networks for many purposes with a wide variety of MATLAB deployment products and tools.

The function `genFunction`

is introduced
in the deployment panels in the tools `nftool`

, `nctool`

, `nprtool`

and `ntstool`

. For information on these tool
features, see Fit Data with a Neural Network, Classify Patterns with a Neural Network, Cluster Data with a Self-Organizing Map, and Neural Network Time Series Prediction and Modeling.

The advanced scripts generated on the Save Results panel of
each of these tools includes an example of deploying networks with `genFunction`

.

The function `genFunction`

generates
a stand-alone MATLAB function for simulating any trained neural
network and preparing it for deployment. This might be useful for
several tasks:

Document the input-output transforms of a neural network used as a calculation template for manual reimplementations of the network

Use the MATLAB Function block to create a Simulink

^{®}blockUse MATLAB Compiler™ to:

Generate stand-alone executables

Generate Excel

^{®}add-ins

Use MATLAB Compiler SDK™ to:

Generate C/C++ libraries

Generate .COM components

Generate Java

^{®}componentsGenerate .NET components

Use MATLAB Coder™ to:

Generate C/C++ code

Generate efficient MEX-functions

`genFunction(net,'pathname')`

takes a neural
network and file path, and produces a standalone MATLAB function
file `filename.m`

.

`genFunction(...,'MatrixOnly','yes')`

overrides
the default cell/matrix notation and instead generates a function
that uses only matrix arguments compatible with MATLAB Coder tools.
For static networks, the matrix columns are interpreted as independent
samples. For dynamic networks, the matrix columns are interpreted
as a series of time steps. The default value is `'no'`

.

`genFunction(___,'ShowLinks','no')`

disables
the default behavior of displaying links to generated help and source
code. The default is `'yes'`

.

Here a static network is trained and its outputs calculated.

[x,t] = house_dataset; houseNet = feedforwardnet(10); houseNet = train(houseNet,x,t); y = houseNet(x);

The following code generates, tests, and displays a MATLAB function with the same interface as the neural network object.

genFunction(houseNet,'houseFcn'); y2 = houseFcn(x); accuracy2 = max(abs(y-y2)) edit houseFcn

You can compile the new function with the MATLAB Compiler tools
(license required) to a shared/dynamically linked library with `mcc`

.

mcc -W lib:libHouse -T link:lib houseFcn

The next code generates another version of the MATLAB function
that supports only matrix arguments (no cell arrays). This function
is tested. Then it is used to generate a MEX-function with the MATLAB Coder tool `codegen`

(license
required), which is also tested.

genFunction(houseNet,'houseFcn','MatrixOnly','yes'); y3 = houseFcn(x); accuracy3 = max(abs(y-y3)) x1Type = coder.typeof(double(0),[13 Inf]); % Coder type of input 1 codegen houseFcn.m -config:mex -o houseCodeGen -args {x1Type} y4 = houseCodeGen(x); accuracy4 = max(abs(y-y4))

Here a dynamic network is trained and its outputs calculated.

[x,t] = maglev_dataset; maglevNet = narxnet(1:2,1:2,10); [X,Xi,Ai,T] = preparets(maglevNet,x,{},t); maglevNet = train(maglevNet,X,T,Xi,Ai); [y,xf,af] = maglevNet(X,Xi,Ai);

Next a MATLAB function is generated and tested. The function
is then used to create a shared/dynamically linked library with `mcc`

.

genFunction(maglevNet,'maglevFcn'); [y2,xf,af] = maglevFcn(X,Xi,Ai); accuracy2 = max(abs(cell2mat(y)-cell2mat(y2))) mcc -W lib:libMaglev -T link:lib maglevFcn

The following code generates another version of the MATLAB function
that supports only matrix arguments (no cell arrays). This function
is tested. Then it is used to generate a MEX-function with the MATLAB Coder tool `codegen`

,
which is also tested.

genFunction(maglevNet,'maglevFcn','MatrixOnly','yes'); x1 = cell2mat(X(1,:)); % Convert each input to matrix x2 = cell2mat(X(2,:)); xi1 = cell2mat(Xi(1,:)); % Convert each input state to matrix xi2 = cell2mat(Xi(2,:)); [y3,xf1,xf2] = maglevFcn(x1,x2,xi1,xi2); accuracy3 = max(abs(cell2mat(y)-y3)) x1Type = coder.typeof(double(0),[1 Inf]); % Coder type of input 1 x2Type = coder.typeof(double(0),[1 Inf]); % Coder type of input 2 xi1Type = coder.typeof(double(0),[1 2]); % Coder type of input 1 states xi2Type = coder.typeof(double(0),[1 2]); % Coder type of input 2 states codegen maglevFcn.m -config:mex -o maglevNetCodeGen ... -args {x1Type x2Type xi1Type xi2Type} [y4,xf1,xf2] = maglevNetCodeGen(x1,x2,xi1,xi2); dynamic_codegen_accuracy = max(abs(cell2mat(y)-y4))

For information on simulating neural networks and deploying neural networks with Simulink tools, see Deploy Neural Network Simulink Diagrams.

Was this topic helpful?