Matlab neural network tool box,on extract weight & bias from feedforwardnet

£可爱£侵袭症+ 提交于 2019-12-23 03:32:11

问题


My problem is simple. I have trained a feedforwardnet. now I want to extract its weights and biases so i can test it on another programming language. but while i tested those trained weights by my own code, it always returns different results compare with neural tool box.here is my code

close all 
RandStream.setGlobalStream (RandStream ('mrg32k3a','Seed', 1234));
[x,t] = simplefit_dataset;
plot(t)
hold on 
topo = [2]
net = feedforwardnet(topo);
net = train(net,x,t);
view(net)
y = net(x);
plot(y)

%rewrite net

BI = net.B{1};
WI = net.IW{1};

BO = net.B{2};
WO = net.LW{2};

% input layer

Z = WI*x + BI*ones(1,length(x));
Z = 2./(1+exp(-2*Z))-1;

Y = WO*Z + BO*ones(1,length(x));

plot(Y)
legend('target','tool box result','my result')

it is a simple neural network only have two layer. no scaling or normalization be implied here is the result


回答1:


Neural network inputs and outputs are mapped to the [-1;1] range by default, so the calculation of Z and Y is correct for the mapped values, not for the actual inputs and outputs. To avoid this, you can unset the processFcns properties:

 ....
net = feedforwardnet(topo);
net.inputs{1}.processFcns= {};
net.outputs{2}.processFcns= {};
net = train(net,x,t);
 ....

Alternatively, you can map the input and output values manually:

x= mapminmax(x,-1,1);
Z = WI*x + BI*ones(1,length(x));
Z = 2./(1+exp(-2*Z))-1;
Y = WO*Z + BO*ones(1,length(x));
Y= mapminmax(Y,min(y),max(y));


来源:https://stackoverflow.com/questions/40837406/matlab-neural-network-tool-box-on-extract-weight-bias-from-feedforwardnet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!