I want to compile my MATLAB application that uses neural networks to a stand-alone application, but as you know MATLAB can't compile training neural network as stand-alone and can only compile already trained neural networks.
The core of my application consists of training a neural network on an imported data. How can I do that? Is there an alternative way to do this? My MATLAB version is R2014a.
I tried using deploytool
for compiling, but according to the MATLAB Compiler documentation:
THIS CAN BE COMPILED
* Pre-trained network
* command line functions
THIS CANNOT BE COMPILED
* All other command line functionality
* All GUIs provided with toolbox
* Simulink blocks
* gensim
So we get error after compiling the app if we have functions like newff
or patternnet
or other training functions in our code.
I know this is a limitation of the MATLAB Compiler and I searched for solutions for months but I didn't find any workarounds or alternative ways.
Apparently there is a function added to newer versions of MATLAB for using trained neural networks in MATLAB compiler: Deploy Neural Network Functions.
The bottom line is that MATLAB Compiler only supports deploying pre-trained neural networks.
This means you cannot
mcc
-compile functions with training functionality (anything containing TRAIN, ADAPT, etc...), you can only deploy functions that evaluate/simulate an already trained network object (SIM function and the like).For the supported scenario (deploying a pre-trained network), there are a couple of ways to go about it:
1) save/load pre-trained network object to a MAT-file
In a normal MATLAB session, load the training data you have, then create and train a neural network using desired settings (keep tuning the network parameters until you are satisfied with the result). Finally save the network object to disk (export as variable in a MAT-file).
Next create a deployable function that loads the saved network, and uses it to predict the output given some test data (note the use of
%#function
pragma line):simulateSavedNet.m
2) Generate a standalone M-function from pre-trained network
You can generate a standalone MATLAB function from a pre-trained network object using
genFunction
, which can then be used to simulate network output. This functionality was introduced in MATLAB R2013b.It will basically hard-code network settings, structure, and weights all in one M-function. The generated function is fully compatible with MATLAB Compiler
mcc
(compiled into one of the supported targets) as well as MATLAB Codercodegen
(converted to standalone C/C++ code).Here is the code for the generated function:
3) Manually simulate the pre-trained network
For simple static neural networks (feed-forward and the like), it is relatively easy to evaluate a pre-trained network and simulate its output (the hard part is training them!).
I've showed how to do this in previous answers. You basically extract the learned weights from the network, then plug those numbers into the transfer functions, feed it the input, and compute the propagated outputs (one layer at-a-time). You will have to take care of applying any pre-/post- processing on the data and use the same transfer functions in each layer.
In fact, this is basically what
genFunction
does in the previous approach, only it is automated and handles all cases (works for all kinds of neural networks, not just feed-forward ANNs).Here is an example from the network trained above:
simulateManualNet.m
4) Generate Simulink block from pre-trained network, and convert it using Simulink Coder
The idea here is to generate a Simulink block from the pre-trained network using
gensim
, then convert the generated block into a standalone C/C++ application using Simulink Coder (formerly known as Real-Time Workshop). Compiling neural network to Simulink blocks was introduced in R2010b.I'm not a Simulink expert, so I'll leave it to you to explore this approach:
In each approach above (first three anyway), the idea is to compile the
simulate
functions into one of the supported targets by MATLAB Compiler (standalone executable, shared library, Java package, .NET assembly) then deploy the generated component.(Actually approach #2 and #3 can also be converted into C/C++ source code using MATLAB Coder
codegen
).Here is how to compile each into a shared library using
mcc
command (you could usedeploytool
if you like):To check the resulting DLLs, below is a C++ test program that links against the produced shared library:
The code for the test program:
test_net.cpp
Here is the output of the resulting programs, compared against the original network object and the source M-functions:
This ended up being a long post, but I wanted to cover all possible cases for this question and future ones :) HTH