问题
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