MATLAB Answers

kheirou
1

Export a neural network trained with MATLAB in other programming languages

kheirou
さんによって質問されました 2014 年 2 月 21 日
最新アクティビティ Magnus Hansson さんによって 回答されました 2017 年 10 月 20 日
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 ?

  0 件のコメント

サインイン to comment.

5 件の回答

回答者: kheirou
2014 年 2 月 21 日

Is there any response ?

  0 件のコメント

サインイン to comment.


回答者: Asma DJAIDRI 2015 年 4 月 12 日

i search the solution too , please if you have found some thing please share it and thank you so much

  0 件のコメント

サインイン to comment.


回答者: H. Birkan
2017 年 3 月 9 日

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

  0 件のコメント

サインイン to comment.


回答者: H. Birkan
2017 年 3 月 9 日

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

  0 件のコメント

サインイン to comment.


回答者: Magnus Hansson 2017 年 10 月 20 日

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.

  0 件のコメント

サインイン to comment.



Translated by