- Neural Network Subobject Properties: https://www.mathworks.com/help/deeplearning/ug/neural-network-subobject-properties.html
- “cellfun”: https://www.mathworks.com/help/matlab/ref/cellfun.html
- “bsxfun”: https://www.mathworks.com/help/matlab/ref/bsxfun.html
- “eval”: https://www.mathworks.com/help/matlab/ref/eval.html
How can I check the neural network is correct?
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I have some data (p is the input and t the output). The net works well but I want to ckeck it manually. When I compute it, it doesn't work. I explain it better in the code:
p=[4.21470484282808,4.61178342351945,4.92103025244236,5.16780470857077,5.36888616086475,5.53567693961333,5.67614306410908,5.79599161759301,5.89941107438887,5.98953800357575
0.0841938476119945,0.0688748497618962,0.0506266219880575,0.0313984679864031,0.0122666193874076,-0.00620394471778739,-0.0237367071204724,-0.0402208466896825,-0.0556358572482061,-0.0700100771974078
0.624790077024891,0.933828019025972,1.24834383939203,1.55431911928733,1.84468199477835,2.11642821064707,2.3688169191345,2.60230677876707,2.81795118085545,3.01705857857006
0.00257893224675201,0.00368824779406364,0.00453083959629498,0.00489009192682788,0.0046033702524108,0.00357272498883625,0.00175843692227414,-0.000832738095460411,-0.00416162543741231,-0.00816576040624318
];
t=[2.83606065756777,3.31180314379271,3.70377438426516,4.02978774606033,4.30397305613069,4.53713048911324,4.7374656190205,4.91123610026158,5.06326477923633,5.19730529195449
0.0485161358312315,0.0461344489481127,0.0391584214613128,0.0290027475982773,0.016808673107218,0.00342380868886163,-0.0105496421711672,-0.0246959627873845,-0.0387354315195166,-0.0524841437666582
0.930574382489153,1.30350896221403,1.66272401320841,1.99818564751866,2.30664368564685,2.58817825301552,2.84437665551212,3.07741070830855,3.28958545610536,3.48311553793172
0.00580490407681004,0.00759806971982089,0.00862898201442169,0.00863448538934149,0.0074887953804546,0.00517928895558281,0.00176746424529477,-0.00263950757984306,-0.00791199822691147,-0.0139137399633852
];
n=5;
logsig=@(x) 1./(1+exp(-x));
net = fitnet(n,'trainlm');
net.layers{1}.transferFcn='logsig'
[net,tr]=train(net,p,t);
nntraintool
Y_pred=net(p)
error=abs(t-Y_pred)
plotregression(t,Y_pred)
W1 = net.IW{1}
W2 = net.LW{2}
b1 = net.b{1}
b2 = net.b{2}
a1=logsig(W1*p+b1)
a2=W2*a1+b2
% my doubt is: why a2 is not the same that Y_pred? What am I doind bad?
0 commentaires
Réponses (1)
Lokesh
le 10 Oct 2023
Hi Bernardino,
I understand that you want to compute the output of a neural network manually.
In your code, there seems to be a mismatch between the variable "a2" and "Y_pred" due to missing normalization parameters.
To compute the output of neural network manually, we should first determine whether the input data needs to be normalized using “mapminmax”. If normalization is required, we can proceed with the normalization process. Similarly, we should also check if the output needs to be reverse normalized using "mapminmax” and perform the reverse normalization if necessary.
Please refer to the following code for manually computing the output of a neural network:
%% Set “p” and “t” to input and output respectively
p=[]; t=[];
n=5;
logsig=@(x) 1./(1+exp(-x));
net = fitnet(n,'trainlm');
net.layers{1}.transferFcn='logsig'
[net,tr]=train(net,p,t);
Y_pred=net(p);
error=abs(t-Y_pred);
plotregression(t,Y_pred);
%% Code to compute output manually
w = cellfun(@transpose,[net.IW{1},net.LW(2:size(net.LW,1)+1:end)],'UniformOutput',false); %% Extract Weights
b = cellfun(@transpose,net.b','UniformOutput',false); %% Extract Biases
tf = cellfun(@(x)x.transferFcn,net.layers','UniformOutput',false);
%% mapminmax on inputs
if strcmp(net.Inputs{1}.processFcns{:},'mapminmax')
xoffset = net.Inputs{1}.processSettings{1}.xoffset;
gain = net.Inputs{1}.processSettings{1}.gain;
ymin = net.Inputs{1}.processSettings{1}.ymin;
In0 = bsxfun(@plus,bsxfun(@times,bsxfun(@minus,p,xoffset),gain),ymin); %% Normalize Inputs
else
In0 = inputs;
end
In = cell(1,length(w));
Out = In;
In{1} = In0'*w{1}+b{1};
Out{1} = eval([tf{1},'(In{1})']);
for i=2:length(w)
In{i} = Out{i-1}*w{i}+b{i};
Out{i} = eval([tf{i},'(In{',num2str(i),'})']); %% Calculate Output of each layer
end
%% reverse mapminmax on outputs
if strcmp(net.Outputs{end}.processFcns{:},'mapminmax')
gain = net.outputs{end}.processSettings{:}.gain;
ymin = net.outputs{end}.processSettings{:}.ymin;
xoffset = net.outputs{end}.processSettings{:}.xoffset;
output = bsxfun(@plus,bsxfun(@rdivide,bsxfun(@minus,Out{end}',ymin),gain),xoffset) %% Reverse Normalization
else
output = Out{end}
end
%% output matches Y_pred
Please refer to the following MATLAB documentation links to know more about the concepts and functions used in the code:
I hope you find this helpful.
Best Regards,
Lokesh
0 commentaires
Voir également
Catégories
En savoir plus sur Sequence and Numeric Feature Data Workflows dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!