I am implementing forward neural network for prediction while taking weights from patternnet trained model
Dir = ‘.’;
outputFile = fullfile(Dir, ‘net_test1.mat’);
load(outputFile, ‘TrainedNet’);
%%
ih1w = TrainedNet.IW{ 1, 1 };
h1h2w = TrainedNet.LW{ 2, 1 };
h2ow = TrainedNet.LW{ 3, 2 };
h1b = TrainedNet.b{1};
h2b = TrainedNet.b{2};
ob = TrainedNet.b{3};
%%
maxx = TrainedNet.inputs{1}.processSettings{1,1}.xmax;
minx = TrainedNet.inputs{1}.processSettings{1,1}.xmin;
gain = TrainedNet.inputs{1}.processSettings{1,1}.gain;
rangex = TrainedNet.inputs{1}.processSettings{1,1}.xrange;
offset = TrainedNet.inputs{1}.processSettings{1,1}.xoffset;
TrainedNet.inputs{1}.processSettings{1,1}
%%
function y = tanh(x)
y = (2 / (1 + exp(-2 * x))) – 1;
end
function y = sigmoid(x)
y = 1 / (1 + exp(-x));
end
inputlayer = ones(1,1036);
inputlayer = inputlayer’;
inputlayer_normalized = [];
for x = 1:1036
inputlayer_normalized(x) = (inputlayer(x)-offset(x))*gain(x);
end
% Initialize variables
h1size = size(ih1w, 1);
inputsize = size(ih1w, 2);
h2size = size(h1h2w, 1);
outputsize = size(h2ow, 1);
% First hidden layer computation
hl1 = zeros(1, h1size);
for k = 0:h1size-1
sum = 0;
for i = 0:inputsize-1
sum = sum + (ih1w(k+1, i+1) * inputlayer_normalized(i+1));
end
sum = sum + h1b(k+1);
hl1(k+1) = tanh(sum);
end
% Second hidden layer computation
hl2 = zeros(1, h2size);
for k = 0:h2size-1
hl2(k+1) = 0;
for i = 0:h1size-1
hl2(k+1) = hl2(k+1) + (h1h2w(k+1, i+1) * hl1(i+1));
end
hl2(k+1) = hl2(k+1) + h2b(k+1);
hl2(k+1) = tanh(hl2(k+1));
end
% Output layer computation
ol = zeros(1, outputsize);
for k = 0:outputsize-1
ol(k+1) = 0;
for i = 0:h2size-1
ol(k+1) = ol(k+1) + (h2ow(k+1, i+1) * hl2(i+1));
end
ol(k+1) = ol(k+1) + ob(k+1);
ol(k+1) = sigmoid(ol(k+1));
end
Ipred = TrainedNet(inputlayer);
this is code what i am implementing above neural network trained from inbuild function patternnet in matlab
I am using its weights and preprocess
but i am not getting same output in variable ol and IpredDir = ‘.’;
outputFile = fullfile(Dir, ‘net_test1.mat’);
load(outputFile, ‘TrainedNet’);
%%
ih1w = TrainedNet.IW{ 1, 1 };
h1h2w = TrainedNet.LW{ 2, 1 };
h2ow = TrainedNet.LW{ 3, 2 };
h1b = TrainedNet.b{1};
h2b = TrainedNet.b{2};
ob = TrainedNet.b{3};
%%
maxx = TrainedNet.inputs{1}.processSettings{1,1}.xmax;
minx = TrainedNet.inputs{1}.processSettings{1,1}.xmin;
gain = TrainedNet.inputs{1}.processSettings{1,1}.gain;
rangex = TrainedNet.inputs{1}.processSettings{1,1}.xrange;
offset = TrainedNet.inputs{1}.processSettings{1,1}.xoffset;
TrainedNet.inputs{1}.processSettings{1,1}
%%
function y = tanh(x)
y = (2 / (1 + exp(-2 * x))) – 1;
end
function y = sigmoid(x)
y = 1 / (1 + exp(-x));
end
inputlayer = ones(1,1036);
inputlayer = inputlayer’;
inputlayer_normalized = [];
for x = 1:1036
inputlayer_normalized(x) = (inputlayer(x)-offset(x))*gain(x);
end
% Initialize variables
h1size = size(ih1w, 1);
inputsize = size(ih1w, 2);
h2size = size(h1h2w, 1);
outputsize = size(h2ow, 1);
% First hidden layer computation
hl1 = zeros(1, h1size);
for k = 0:h1size-1
sum = 0;
for i = 0:inputsize-1
sum = sum + (ih1w(k+1, i+1) * inputlayer_normalized(i+1));
end
sum = sum + h1b(k+1);
hl1(k+1) = tanh(sum);
end
% Second hidden layer computation
hl2 = zeros(1, h2size);
for k = 0:h2size-1
hl2(k+1) = 0;
for i = 0:h1size-1
hl2(k+1) = hl2(k+1) + (h1h2w(k+1, i+1) * hl1(i+1));
end
hl2(k+1) = hl2(k+1) + h2b(k+1);
hl2(k+1) = tanh(hl2(k+1));
end
% Output layer computation
ol = zeros(1, outputsize);
for k = 0:outputsize-1
ol(k+1) = 0;
for i = 0:h2size-1
ol(k+1) = ol(k+1) + (h2ow(k+1, i+1) * hl2(i+1));
end
ol(k+1) = ol(k+1) + ob(k+1);
ol(k+1) = sigmoid(ol(k+1));
end
Ipred = TrainedNet(inputlayer);
this is code what i am implementing above neural network trained from inbuild function patternnet in matlab
I am using its weights and preprocess
but i am not getting same output in variable ol and Ipred Dir = ‘.’;
outputFile = fullfile(Dir, ‘net_test1.mat’);
load(outputFile, ‘TrainedNet’);
%%
ih1w = TrainedNet.IW{ 1, 1 };
h1h2w = TrainedNet.LW{ 2, 1 };
h2ow = TrainedNet.LW{ 3, 2 };
h1b = TrainedNet.b{1};
h2b = TrainedNet.b{2};
ob = TrainedNet.b{3};
%%
maxx = TrainedNet.inputs{1}.processSettings{1,1}.xmax;
minx = TrainedNet.inputs{1}.processSettings{1,1}.xmin;
gain = TrainedNet.inputs{1}.processSettings{1,1}.gain;
rangex = TrainedNet.inputs{1}.processSettings{1,1}.xrange;
offset = TrainedNet.inputs{1}.processSettings{1,1}.xoffset;
TrainedNet.inputs{1}.processSettings{1,1}
%%
function y = tanh(x)
y = (2 / (1 + exp(-2 * x))) – 1;
end
function y = sigmoid(x)
y = 1 / (1 + exp(-x));
end
inputlayer = ones(1,1036);
inputlayer = inputlayer’;
inputlayer_normalized = [];
for x = 1:1036
inputlayer_normalized(x) = (inputlayer(x)-offset(x))*gain(x);
end
% Initialize variables
h1size = size(ih1w, 1);
inputsize = size(ih1w, 2);
h2size = size(h1h2w, 1);
outputsize = size(h2ow, 1);
% First hidden layer computation
hl1 = zeros(1, h1size);
for k = 0:h1size-1
sum = 0;
for i = 0:inputsize-1
sum = sum + (ih1w(k+1, i+1) * inputlayer_normalized(i+1));
end
sum = sum + h1b(k+1);
hl1(k+1) = tanh(sum);
end
% Second hidden layer computation
hl2 = zeros(1, h2size);
for k = 0:h2size-1
hl2(k+1) = 0;
for i = 0:h1size-1
hl2(k+1) = hl2(k+1) + (h1h2w(k+1, i+1) * hl1(i+1));
end
hl2(k+1) = hl2(k+1) + h2b(k+1);
hl2(k+1) = tanh(hl2(k+1));
end
% Output layer computation
ol = zeros(1, outputsize);
for k = 0:outputsize-1
ol(k+1) = 0;
for i = 0:h2size-1
ol(k+1) = ol(k+1) + (h2ow(k+1, i+1) * hl2(i+1));
end
ol(k+1) = ol(k+1) + ob(k+1);
ol(k+1) = sigmoid(ol(k+1));
end
Ipred = TrainedNet(inputlayer);
this is code what i am implementing above neural network trained from inbuild function patternnet in matlab
I am using its weights and preprocess
but i am not getting same output in variable ol and Ipred patternnet MATLAB Answers — New Questions