## Deploy Shallow Neural Network Functions

### Deployment Functions and Tools for Trained Networks

The function `genFunction`

allows stand-alone MATLAB^{®} functions for a trained shallow 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 shallow 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 used by the Neural Net
Fitting, Neural Net Pattern Recognition, Neural Net
Clustering and Neural Net Time Series apps. For information on
these apps, see Fit Data with a Shallow Neural Network, Pattern Recognition with a Shallow Neural Network, Cluster Data with a Self-Organizing Map, and Shallow Neural Network Time-Series Prediction and Modeling.

The comprehensive scripts generated by these apps includes an example of deploying
networks with `genFunction`

.

### Generate Neural Network Functions for Application Deployment

The function `genFunction`

generates a stand-alone MATLAB function for simulating any trained shallow 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] = bodyfat_dataset; bodyfatNet = feedforwardnet(10); bodyfatNet = train(bodyfatNet, x, t); y = bodyfatNet(x);

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

genFunction(bodyfatNet, 'bodyfatFcn'); y2 = bodyfatFcn(x); accuracy2 = max(abs(y - y2)) edit bodyfatFcn

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

.

mcc -W lib:libBodyfat -T link:lib bodyfatFcn

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(bodyfatNet, 'bodyfatFcn', 'MatrixOnly', 'yes'); y3 = bodyfatFcn(x); accuracy3 = max(abs(y - y3)) x1Type = coder.typeof(double(0), [13, Inf]); % Coder type of input 1 codegen bodyfatFcn.m -config:mex -o bodyfatCodeGen -args {x1Type} y4 = bodyfatCodeGen(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))

### Generate Simulink Diagrams

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