Export a neural network trained with MATLAB in other programming languages

4 views (last 30 days)
I trained a NN with matlab using the following code :
p = [-1 -1 2 2; 0 5 0 5];
t = [-1 -1 1 1];
net = newff(p,t,3);
net = train(net,p,t);
y = sim(net,p)
Now i'm tring to use the weights & bias calculated by matlab in order to use them to creat a NN in other programming languages, so i used the NN parameters to calculate the output as 'sim function' do :
input = [-1;0]
y1 = tansig(netsum(dotprod(net.IW{1} , input) , net.b{1}));
Results = purelin(netsum(dotprod(net.LW{2} ,y1) ,net.b{2}))
Y = sim(net,[-1;0])
By this way i do not the same result ?

Answers (5)

H. Birkan
H. Birkan on 9 Mar 2017
After digging internet and Matlab documentation, finally reached a solution.
Here I am giving a solution in Matlab, but if you have tanh() function, you may easily convert it to any programming language. It is for just showing the fields from network object and the operations you need.
  • Assume you have a trained ann (network object) that you want to export
  • Assume that the name of the trained ann is trained_ann
Here is the script for exporting and testing. Testing script compares original network result with my_ann_evaluation() result % Export IT exported_ann_structure = my_ann_exporter(trained_ann);
% Run and Compare
% Works only for single INPUT vector
% Please extend it to MATRIX version by yourself
input = [12 3 5 100];
res1 = trained_ann(input')';
res2 = my_ann_evaluation(exported_ann_structure, input')';
where you need the following two functions
First my_ann_exporter:
function [ my_ann_structure ] = my_ann_exporter(trained_netw)
% Just for extracting as Structure object
my_ann_structure.input_ymax = trained_netw.inputs{1}.processSettings{1}.ymax;
my_ann_structure.input_ymin = trained_netw.inputs{1}.processSettings{1}.ymin;
my_ann_structure.input_xmax = trained_netw.inputs{1}.processSettings{1}.xmax;
my_ann_structure.input_xmin = trained_netw.inputs{1}.processSettings{1}.xmin;
my_ann_structure.IW = trained_netw.IW{1};
my_ann_structure.b1 = trained_netw.b{1};
my_ann_structure.LW = trained_netw.LW{2};
my_ann_structure.b2 = trained_netw.b{2};
my_ann_structure.output_ymax = trained_netw.outputs{2}.processSettings{1}.ymax;
my_ann_structure.output_ymin = trained_netw.outputs{2}.processSettings{1}.ymin;
my_ann_structure.output_xmax = trained_netw.outputs{2}.processSettings{1}.xmax;
my_ann_structure.output_xmin = trained_netw.outputs{2}.processSettings{1}.xmin;
end
Second my_ann_evaluation:
function [ res ] = my_ann_evaluation(my_ann_structure, input)
% Works with only single INPUT vector
% Matrix version can be implemented
ymax = my_ann_structure.input_ymax;
ymin = my_ann_structure.input_ymin;
xmax = my_ann_structure.input_xmax;
xmin = my_ann_structure.input_xmin;
input_preprocessed = (ymax-ymin) * (input-xmin) ./ (xmax-xmin) + ymin;
% Pass it through the ANN matrix multiplication
y1 = tanh(my_ann_structure.IW * input_preprocessed + my_ann_structure.b1);
y2 = my_ann_structure.LW * y1 + my_ann_structure.b2;
ymax = my_ann_structure.output_ymax;
ymin = my_ann_structure.output_ymin;
xmax = my_ann_structure.output_xmax;
xmin = my_ann_structure.output_xmin;
res = (y2-ymin) .* (xmax-xmin) /(ymax-ymin) + xmin;
end
  1 Comment
Lipi Mukherjee
Lipi Mukherjee on 26 Aug 2019
This gives the following error:
Dot indexing is not supported for variables of this type.
Error in network/subsref (line 152)
v = v.(subs);
Error in neuralWeights>predict (line 52)
xmax = net.outputs{2}.processSettings{1}.xmax
Could you please help me with this?

Sign in to comment.


kheirou
kheirou on 21 Feb 2014
Is there any response ?

Asma DJAIDRI
Asma DJAIDRI on 12 Apr 2015
i search the solution too , please if you have found some thing please share it and thank you so much

H. Birkan
H. Birkan on 9 Mar 2017
There must be an easy way to export trained ANN, maybe I want to use it in lua or javascript. It is just a matrix multiplications and additions

Magnus Hansson
Magnus Hansson on 20 Oct 2017
I have the same problem, where it will not really work to export, and I find the documentation not extensive at all.
However, I've found that you can use this functions:
genFunction(net,pathname) genFunction(___,'MatrixOnly','yes') genFunction(___,'ShowLinks','no')
Although, it is not always clear how what is exported works.

Categories

Find more on Sequence and Numeric Feature Data Workflows in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!