Write code for NN using the Weight and Bias data retrieved from the NN tool box

Hi,
I have trained a simple neural network and saved all the resultant biases and weights.
Now, I want to use them to build the network manually which I did. I mapped the inputs into -1 and 1 range. multiplied by inputWeights and added the bias. I applied the tansig transfer function.
Then for the second layer, I multiplied the previous output with the second group of weights, added the bias and applied the transfer function. However, I don't get correct results.
To compare: output of net(inputs) and myNN(inputs is different) and mine is wrong.
MatlabCorrect Mine(wrong)
0.0001 -0.8220
0.9733 -0.9720
0.0015 -0.9963
0.0003 -0.9991
0.0201 -0.9949
0.0026 -0.9638
What could be the reason?
Here is my code if it helps
% ILW: input layer weight
% HLW: hidden layer weight
% ILB: input layer bias
% HLB: hidden layer bias
ALLSAM = load ('allsamples.mat');
minArr = (min(allsamples'))';
maxArr = (max(allsamples'))';
%random sample
input = ALLSAM (:,15);
%mapping to -1 - +1 range
input = 2*((a1-minArr)./(maxArr-minArr)) -1;
[R C] = size(input);
L1 = zeros(size(ILW));
for i = 1:C
L1(:,i) = input(i) .* ILW(:,i);
end
L1 = sum(L1');
L1 = L1' + ILB;
L1 = tansig(L1);
[R C] = size (HLW);
O = zeros(size(HLW));
for i = 1:C
O(:,i) = L1(i) .* HLW(:,i);
end
O = sum(O');
O = O' + HLB;
O = tansig(O)

Answers (1)

Searching the Newsgroup using IW heath
close all, clear all, clc
[ x, t ] = simplefit_dataset;
[ I N ] = size(x)
[ O N ] = size(t)
figure
hold on
plot(x,t,'o','LineWidth',2)
drawnow
H = 10
net = feedforwardnet(H);
net = train(net,x,t);
y = net(x);
plot(x,y,'r','LineWidth',2)
drawnow
IW = net.IW{1,1}
LW = net.LW{2,1}
b1 = net.b{1}
b2 = net.b{2}
xmin = min(x)
xmax=max(x)
xn = -1+ 2*(x-xmin)/(xmax-xmin) ;
h =tansig(IW*xn+b1*ones(1,N));
yn = LW*h+b2;
tmin = min(t)
tmax = max(t)
yhat = tmin +(tmax-tmin)*(yn +1)/2;
plot(x,yhat,'g','LineWidth',2')
mse(y-yhat)
Hope this helps.
Greg

1 Comment

Can you help me with the code for NARX network (10 inputs, 1 target, 6 i/p and feedback delays) to manually calculate the output using extracted weights and bias from trained network?

Sign in to comment.

Categories

Find more on Deep Learning Toolbox in Help Center and File Exchange

Asked:

on 9 May 2012

Edited:

on 8 Apr 2022

Community Treasure Hunt

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

Start Hunting!