How to back calculate the ANN fit function using the W and B?

3 views (last 30 days)
I processed an ANN analysis and obtained the fit function given by the nntool. I used one hidden layer and I have got 2 vectors for x and y, having 74 rows for each.
when I calculate the w and b given from the code, and back calculate the predicted y using those for a certain x, it gives me minus and strange values which are completely not correct. The picture below shows my analysis. Activation function in the hidden layer seems to be Sigmoid and then identity in the output (I figured this out from the symbols).
% Solve an Input-Output Fitting problem with a Neural Network
% Script generated by Neural Fitting app
% Created 30-Oct-2022 12:28:36
%
% This script assumes these variables are defined:
%
% in_WC_com - input data.
% out_com_WC - target data.
x = in_WC_com;
t = out_com_WC;
% Choose a Training Function
% For a list of all training functions type: help nntrain
% 'trainlm' is usually fastest.
% 'trainbr' takes longer but may be better for challenging problems.
% 'trainscg' uses less memory. Suitable in low memory situations.
trainFcn = 'trainlm'; % Levenberg-Marquardt backpropagation.
% Create a Fitting Network
hiddenLayerSize = 1;
net = fitnet(hiddenLayerSize,trainFcn);
% Setup Division of Data for Training, Validation, Testing
net.divideParam.trainRatio = 70/100;
net.divideParam.valRatio = 15/100;
net.divideParam.testRatio = 15/100;
% Train the Network
[net,tr] = train(net,x,t);
% Test the Network
y = net(x);
e = gsubtract(t,y);
performance = perform(net,t,y)
ae=mae(e,t,y)
w1 = net.IW{1} %the input-to-hidden layer weights
w2 = net.LW{2} %the hidden-to-output layer weights
b1 = net.b{1} %the input-to-hidden layer bias
b2 = net.b{2} %the hidden-to-output layer bias
% View the Network
view(net)
%back calculating the fit for a given x1 (a certain value from x vector) value
%Sigmoidal calculation
x1=0.5
s1=1/(1+exp(-(w1*x1+b1)))
%full fit
Iden=w2*s1+b2
The values I am getting for the analysis are follows.
But according to the fit curve I have shown, for x1 (a value in x vector)=0.5, output must be around 30. but I am getting only 1.1967
Please help me to figure this out. Thank you
w1 =-3.0996
w2 = 0.4331
b1 = -0.8228
b2 = -0.1324
x1 = 0.5000
s1 =3.0688
Iden =1.1967

Answers (1)

Divyansh
Divyansh on 4 Nov 2022
Hi Sameera,
I understand that you're getting very different response from the fit model when you manually compute the output from learnt weights and biases than the ground truth values from the actual function for a particular input value.
Also, I can see from the figure of Function Fit that your actual function is highly non-linear. You are trying to approximate this highly non-linear function with just 1 hidden layer, which may not be able to induce sufficient non-linearity into the trained model.
I suggest that you should increase the number of hidden layers in the model, while at the same time, be careful that you don't increase the number of layers so much that you overfit your datapoints.
To know more about the Neural Net Fitting tool and about different training functions, please refere to the following link:
If you have additional doubts about this question, feel free to comment down.
Thanks,
Divyansh

Categories

Find more on Sequence and Numeric Feature Data Workflows in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!