How can I deploy a 'SeriesNetwork' into C/C++ Code?

5 views (last 30 days)
I successfully trained a LSTM-neural network in MATLAB R2018a with the following code:
numResponses = size(YTrain{1},1);
featureDimension = size(XTrain{1},1);
numHiddenUnits = 200;
layers = [ ...
sequenceInputLayer(featureDimension)
lstmLayer(numHiddenUnits,'OutputMode','sequence')
fullyConnectedLayer(50)
dropoutLayer(0.5)
fullyConnectedLayer(numResponses)
regressionLayer];
maxEpochs = 100;
miniBatchSize = 32;
options = trainingOptions('adam', ...
'MaxEpochs',maxEpochs, ...
'MiniBatchSize',miniBatchSize, ...
'InitialLearnRate',0.01, ...
'GradientThreshold',1, ...
'Shuffle','never', ...
'Plots','training-progress',...
'Verbose',0);
net = trainNetwork(XTrain,YTrain,layers,options);
Is there a way to export the trained network into C/C++ code? The function "genFunction" is only applicable for type "Network". When I try to use it on the lstm-network ("SeriesNetwork"), I get following error:
genFunction(net, 'simulateStandaloneNetTest.m', 'MatrixOnly','yes', 'ShowLinks','no')
"Undefined function 'genFunction' for input arguments of type 'SeriesNetwork'."
This thread has a similar question but no (sufficient) answer:
https://de.mathworks.com/matlabcentral/answers/321647-is-there-a-way-to-c-code-a-trained-cnn-to-be-used-in-a-c-c-program
I would like to add the C++ source code in my Microsoft Visual Studio project and predict there the output of the network.
Thank you very much in advance!
Domi
  2 Comments
Claudio Aguiar
Claudio Aguiar on 23 Aug 2022
After spening 18 hours (twice) learning how to train a LSTM neural network at MATLAB (finished self-pace course deep learning with matlab for the second time), I am pleased to find out that there is no pratical way to export that "seriesNetwork" out of matlab to use in any useful c-code. What a waste of my time. In a another post the support team says: "Furthermore, the objects that MATLAB uses to represent Neural Networks are transparent, and you can therefore access all the information that describes your trained network." OK, but you should have told that before I even tried, since you never told me how to use any of this information, and I have no idea how to code a "softmax" or "biLSTM" layer.
Steven Lord
Steven Lord on 24 Aug 2022
The list of functions in Deep Learning Toolbox that support the extended capability of C/C++ Code Generation (using MATLAB Coder) is here. Quickly skimming the list I see that lstmLayer does have this support, although there are some notes on its documentation page related to that functionality. softmaxLayer and bilstmLayer also appear in that list of functions with C/C++ Code Generation support.

Sign in to comment.

Answers (2)

Bill Chou
Bill Chou on 28 Jun 2018
In R2018a, GPU Coder added support to generate C/C++ code from deep learning networks for Intel and ARM processors. Specifically, it requires Intel processors that takes advantage of Intel® Math Kernel Library for Deep Neural Networks (MKL-DNN) and ARM processors that supports the ARM® Compute libraries for mobile platforms. This essentially means Intel Xeon processors and ARM Cortex A processors.
More info here:

Ryan Livingston
Ryan Livingston on 25 Jun 2018
Deploying these networks with MATLAB Coder is currently not supported (as of MATLAB R2018a when I write this).
A workaround is shown at:
Depending on the work you are doing, GPU Coder provides functionality to deploy some deep learning networks:

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!