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 Ipred

 采纳的回答

Hi Ajay,
My suggestion would be revising the computation of the output layer in the neural network. Here is the code snippet as an example for the output layer computation
% Output layer computation ol = zeros(1, outputsize); for k = 1:outputsize sum = 0; for i = 1:h2size sum = sum + (h2ow(k, i) * hl2(i)); end ol(k) = sum + ob(k); ol(k) = sigmoid(ol(k)); end
So, the loop indices start from 1 instead of 0 to align with MATLAB indexing. Then, sum calculation is corrected to accumulate the weighted inputs from the previous layer. Afterwards, the bias term is added after the weighted sum calculation. Finally, the output is passed through the sigmoid activation function to obtain the final output value.
Let me know if you need further assistance.

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Deep Learning Toolbox 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by