The function创Function
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创Function
is used by theNeural Net Fitting,Neural Net Pattern Recognition,Neural Net ClusteringandNeural Net Time Seriesapps. For information on these apps, seeFit Data with a Shallow Neural Network,Classify Patterns with a Shallow Neural Network,Cluster Data with a Self-Organizing Map, andShallow Neural Network Time-Series Prediction and Modeling.
The comprehensive scripts generated by these apps includes an example of deploying networks with创Function
.
The function创Function
创erates 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®block
UseMATLAB Compiler™to:
Generate stand-alone executables
Generate Excel®add-ins
UseMATLAB Compiler SDK™to:
Generate C/C++ libraries
Generate .COM components
Generate Java®components
Generate .NET components
UseMATLAB Coder™to:
Generate C/C++ code
Generate efficient MEX-functions
创Function(net,'pathname')
takes a neural network and file path, and produces a standalone MATLAB function filefilename.m
.
创Function(...,'MatrixOnly','yes')
overrides the default cell/matrix notation and instead generates a function that uses only matrix arguments compatible withMATLAB Codertools. 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'
.
创Function(___,'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.
创Function(bodyfatNet,'bodyfatFcn'); y2 = bodyfatFcn(x); accuracy2 = max(abs(y - y2)) editbodyfatFcn
You can compile the new function with theMATLAB Compilertools (license required) to a shared/dynamically linked library withmcc
.
mcc-Wlib:libBodyfat-Tlink:libbodyfatFcn
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 theMATLAB Codertoolcodegen
(license required), which is also tested.
创Function(bodyfatNet,'bodyfatFcn','MatrixOnly','yes'); y3 = bodyfatFcn(x); accuracy3 = max(abs(y - y3)) x1Type = coder.typeof(double(0), [13, Inf]);% Coder type of input 1codegenbodyfatFcn.m-config:mex-obodyfatCodeGen-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 withmcc
.
创Function(maglevNet,'maglevFcn'); [y2,xf,af] = maglevFcn(X,Xi,Ai); accuracy2 = max(abs(cell2mat(y)-cell2mat(y2))) mcc-Wlib:libMaglev-Tlink:libmaglevFcn
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 theMATLAB Codertoolcodegen
, which is also tested.
创Function(maglevNet,'maglevFcn','MatrixOnly','yes'); x1 = cell2mat(X(1,:));% Convert each input to matrixx2 = cell2mat(X(2,:)); xi1 = cell2mat(Xi(1,:));% Convert each input state to matrixxi2 = 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 1x2Type = coder.typeof(double(0),[1 Inf]);% Coder type of input 2xi1Type = coder.typeof(double(0),[1 2]);% Coder type of input 1 statesxi2Type = coder.typeof(double(0),[1 2]);% Coder type of input 2 statescodegenmaglevFcn.m-config:mex-omaglevNetCodeGen...-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 shallow neural networks and deploying trained neural networks with Simulink tools, seeDeploy Shallow Neural Network Simulink Diagrams.