0% found this document useful (0 votes)
89 views

Generate MATLAB Function For Simulating Neural Network - MATLAB Genfunction

This document discusses how to generate a MATLAB function for simulating a neural network. It describes the genFunction command which takes a trained network and generates a standalone MATLAB function file. It also provides examples of generating functions from both static and dynamic neural networks and testing the generated functions.

Uploaded by

jose diaz
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
89 views

Generate MATLAB Function For Simulating Neural Network - MATLAB Genfunction

This document discusses how to generate a MATLAB function for simulating a neural network. It describes the genFunction command which takes a trained network and generates a standalone MATLAB function file. It also provides examples of generating functions from both static and dynamic neural networks and testing the generated functions.

Uploaded by

jose diaz
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Generate MATLAB function for simulating neural network - MATLAB ... https://www.mathworks.com/help/nnet/ref/genfunction.

html

genFunction
Generate MATLAB function for simulating neural network

Syntax

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

Description
genFunction(net,pathname) generates a complete stand-alone MATLAB® function for simulating a example
neural network including all settings, weight and bias values, module functions, and calculations in one
file. The result is a standalone MATLAB function file. You can also use this function with MATLAB
Compiler™ and MATLAB Coder™ tools.
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 collapse 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] = bodyfat_dataset;
bodyfatNet = feedforwardnet(10);
bodyfatNet = train(bodyfatNet,x,t);
y = bodyfatNet(x);

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

genFunction(bodyfatNet,'bodyfatFcn');
y2 = bodyfatFcn(x);
accuracy2 = max(abs(y-y2))
mcc -W lib:libBodyfat -T link:lib bodyfatFcn

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.

1 of 3 4/18/2017 2:56 PM
Generate MATLAB function for simulating neural network - MATLAB ... https://www.mathworks.com/help/nnet/ref/genfunction.html

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 = bodyfatodeGen(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))

2 of 3 4/18/2017 2:56 PM
Generate MATLAB function for simulating neural network - MATLAB ... https://www.mathworks.com/help/nnet/ref/genfunction.html

Input Arguments collapse all

net — neural network


network 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

Extended Capabilities

C/C++ Code Generation


Generate C and C++ code using MATLAB® Coder™.

See Also
gensim

Topics
Deploy Trained Neural Network Functions

Introduced in R2013b

3 of 3 4/18/2017 2:56 PM

You might also like