Contents

genFunction

Generate MATLAB function for simulating neural network

Syntax

  • genFunction(net,pathname) example
  • genFunction(___,'MatrixOnly','yes') example
  • genFunction(___,'ShowLinks','no')

Description

example

genFunction(net,pathname) generates a complete stand-alone MATLAB® function for simulating a neural network including all settings, weight and bias values, module functions, and calculations in one file. The result is a standalone MATLAB function file.

Generating a MATLAB neural network simulation function can help you to:

  • Document the input-output transform of a neural network

  • Generate C/C++ code with MATLAB Coder™ codegen

  • Generate efficient MEX functions with MATLAB Coder codegen

  • Generate stand-alone C executables with MATLAB Compiler™ mcc

  • Generate C/C++ libraries with MATLAB Compiler mcc

  • Generate Excel® and .COM components with MATLAB Builder™ EX mcc options

  • Generate Java® components with MATLAB Builder JA mcc options

  • Generate .NET components with MATLAB Builder NE mcc options

example

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'.

Examples

expand all

Create Functions from Static Neural Network

This example shows how to create a MATLAB function and a MEX-function from a static neural network.

First, train a static network and calculate its outputs for the training data.

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

Next, generate and test a MATLAB function. Then the new function is compiled to a shared/dynamically linked library with mcc.

genFunction(houseNet,'houseFcn');
y2 = houseFcn(x);
accuracy2 = max(abs(y-y2))
mcc -W lib:libHouse -T link:lib houseFcn

Next, generate another version of the MATLAB function that supports only matrix arguments (no cell arrays), and test the function. Use the MATLAB Coder tool codegen to generate a MEX-function, 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))

Create Functions from Dynamic Neural Network

This example shows how to create a MATLAB function and a MEX-function from a dynamic neural network.

First, train a dynamic network and calculate its outputs for the training data.

[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, generate and test a MATLAB function. Use the function 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

Next, generate another version of the MATLAB function that supports only matrix arguments (no cell arrays), and test the function. Use the MATLAB Coder tool codegen to generate a MEX-function, 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))

Input Arguments

expand all

net — neural networknetwork object

Neural network, specified as a network object.

Example: net = feedforwardnet(10);

pathname — location and name of generated function file (default) | character string

Location and name of generated function file, specified as a character string. If you do not specify a file name extension of .m, it is automatically appended. If you do not specify a path to the file, the default location is the current working folder.

Example: 'myFcn.m'

Data Types: char

See Also

Was this topic helpful?