Is it possible to get expression/formula of Neural Network Toolbox?

16 vues (au cours des 30 derniers jours)
Rokas Bondzinskas
Rokas Bondzinskas le 23 Oct 2014
Commenté : omar alrumayh le 22 Mai 2019
Hi all,
I have a dependency P(x,y,z), thus, this is 4D function. My goal is to find an analytic expression of P as a function of x,y,z. I used Neural Network Toolbox to analyse my data (train, validated and so on). It seems that it gives very good fit with MSE of 1e-7 and R-square of 0.997. I am wondering if it is possible to get an expression where I could manually plug in x,y,z and get P values.
Best Regards Rokas,
  1 commentaire
omar alrumayh
omar alrumayh le 22 Mai 2019
I am trying to do the same. However, I could not get the same answer from the function that represent the NN.
x=randi([0 1],10000,1).'; %INPUT
y= (x./2); %OUTPUT
input= {x};
target= {y};
%--------------------------------------
net= fitnet(1);
net= train(net,input,target);
test= net(x);
view(net);
%---------------------------------------Here I want to get the weight and bias from the trained neural network
obtainedIW= cell2mat(net.IW); %input weight
obtainedLW= cell2mat(net.LW); %Layer weight
obtainedb= cell2mat(net.b); %Biases
n=(obtainedIW.*0.5) + obtainedb(1); %My input here is 0.5
yCal= ((tansig(n)*obtainedLW +obtainedb(2))) ;

Connectez-vous pour commenter.

Réponse acceptée

Greg Heath
Greg Heath le 24 Oct 2014
The equation for fitnet is
y = b2 + LW*tanh(b1+IW*x);
where
b1 = net.b{1};
b2 = net.b{2};
IW = net.IW{1,1};
LW = net.LW{2,1};
Hope this helps.
Thank you for formally accepting my answer
Greg
  1 commentaire
Rokas Bondzinskas
Rokas Bondzinskas le 24 Oct 2014
Thank you for your answer. Now I am experimenting with 2D data just to see how NN toolbox works. Can you tell me what are the variables b, IW, LW related to the code below?
% This script assumes these variables are defined:
%
% x - input data.
% y - target data.
x = x;
t = y;
% Choose a Training Function
trainFcn = 'trainlm'; % Levenberg-Marquardt
% Create a Fitting Network
hiddenLayerSize = 10;
net = fitnet(hiddenLayerSize,trainFcn);
% Choose Input and Output Pre/Post-Processing Functions
% For a list of all processing functions type: help nnprocess
net.input.processFcns = {'removeconstantrows','mapminmax'};
net.output.processFcns = {'removeconstantrows','mapminmax'};
% Setup Division of Data for Training, Validation, Testing
% For a list of all data division functions type: help nndivide
net.divideFcn = 'dividerand'; % Divide data randomly
net.divideMode = 'sample'; % Divide up every sample
net.divideParam.trainRatio = 90/100;
net.divideParam.valRatio = 5/100;
net.divideParam.testRatio = 5/100;
% Choose a Performance Function
% For a list of all performance functions type: help nnperformance
net.performFcn = 'mse'; % Mean squared error
% Choose Plot Functions
% For a list of all plot functions type: help nnplot
net.plotFcns = {'plotperform','plottrainstate','ploterrhist', ...
'plotregression', 'plotfit'};
% Train the Network
[net,tr] = train(net,x,t);
% Test the Network
y = net(x);
e = gsubtract(t,y);
performance = perform(net,t,y)
% Recalculate Training, Validation and Test Performance
trainTargets = t .* tr.trainMask{1};
valTargets = t .* tr.valMask{1};
testTargets = t .* tr.testMask{1};
trainPerformance = perform(net,trainTargets,y)
valPerformance = perform(net,valTargets,y)
testPerformance = perform(net,testTargets,y)

Connectez-vous pour commenter.

Plus de réponses (1)

Greg Heath
Greg Heath le 26 Oct 2014
Eliminating statements that assign default values yields
x = input;
t = target; % Reserve the variable name y for the output
MSE00 = mean(var(t',1)) % Reference MSE
net = fitnet; % Uses defaults
% Train the Network
[ net, tr, y0, e ] = train(net, x, t); % e = t-y0
b1 = net.b{1};
b2 = net.b{2};
IW = net.IW{1,1};
LW = net.LW{2,1};
% Test the Network
y = b2 + LW*tanh(b1+IW*x);
diffy = max(abs(y-y0))
performance = mse(e)
% Recalculate Training, Validation and Test Performance
trainTargets = t .* tr.trainMask{1};
valTargets = t .* tr.valMask{1};
testTargets = t .* tr.testMask{1};
trainPerformance = perform(net,trainTargets,y)
valPerformance = perform(net,valTargets,y)
testPerformance = perform(net,testTargets,y)
  1 commentaire
Rokas Bondzinskas
Rokas Bondzinskas le 4 Nov 2014
Okay. First of all, this code is not working. The error says: Error using + Matrix dimensions must agree.
Error in model (line 59)
y = b2 + LW*tanh(b1+IW*x);
Where did you get this kind of information about these coefficients and so on? I could use some literature to figure out how it works.

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by