Generate MATLAB Function For Simulating Neural Network - MATLAB Genfunction
Generate MATLAB Function For Simulating Neural Network - MATLAB 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'.
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))
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))
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
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'
Extended Capabilities
See Also
gensim
Topics
Deploy Trained Neural Network Functions
Introduced in R2013b
3 of 3 4/18/2017 2:56 PM