Neural Network: how can I get the correct output answer without using the function "sim", neural network function "sim" vs my calculation with trained network's weight and bias
Afficher commentaires plus anciens
I want to calculate the Neural network output with weight produced by neural network toolbox. but my caclulated output is different from the sim(net,X)
1. I made input data and target data
M = [1:1:10];
M = [M,M,M,M,M].*rand();
M = [M,M].*rand();
M = [M,M,M,M,M].*10;
M = [M,M].*10;
a=M.*rand().*2^rand()+5*rand()-5*rand();
b=M.*rand().*2^rand()+5*rand()-5*rand();
c=M.*rand().*2^rand()+5*rand()-5*rand();
n=rand(1,1000)*0.05;
y = 5*a + b.*c + 7*c + n;
x=[a; b; c];
t=y;
and set the FF Neural network
hiddenLayerSize = 4;
net = feedforwardnet(hiddenLayerSize);
net.divideFcn = 'dividerand'; % Split random data
net.divideMode = 'sample';
net.divideParam.trainRatio = 70/100;
net.divideParam.valRatio = 15/100;
net.divideParam.testRatio = 15/100;
net = train(net,x,t);
And output of trained network with intput data X = [22,25,21]' is
X = [22,25,21]'
y_sim=sim(net,X)
This procedure is the result using the function "sim".
Next, i will calculate output with above network's weight parameters.
weight parameter like as,
b1 = net.b{1};
b2 = net.b{2};
IW = net.IW{1,1};
LW = net.LW{2,1};
and calculate the output with input X = [22,25,21]'
X = [22,25,21]'
y_my = b2 + LW * tanh(b1 + (IW * X))
I really don't know why these two output is different. y_my and y_sim is different.
this is full codes.
clc
clear all
rng(4151945);
M = [1:1:10];
M = [M,M,M,M,M].*rand();
M = [M,M].*rand();
M = [M,M,M,M,M].*10;
M = [M,M].*10;
a=M.*rand().*2^rand()+5*rand()-5*rand();
b=M.*rand().*2^rand()+5*rand()-5*rand();
c=M.*rand().*2^rand()+5*rand()-5*rand();
n=rand(1,1000)*0.05;
y = 5*a + b.*c + 7*c + n;
x=[a; b; c];
t=y;
% Setting the sample size
hiddenLayerSize = 4;
net = feedforwardnet(hiddenLayerSize);
net.divideFcn = 'dividerand'; % Split random data
net.divideMode = 'sample';
net.divideParam.trainRatio = 70/100;
net.divideParam.valRatio = 15/100;
net.divideParam.testRatio = 15/100;
net = train(net,x,t);
% syms p q r real
% X = [p,q,r]';
X = [22,25,21]'
b1 = net.b{1};
b2 = net.b{2};
IW = net.IW{1,1};
LW = net.LW{2,1};
y_my = b2 + LW * tanh(b1 + (IW * X))
y_sim = sim(net,X)
y1compare = 5*X(1) + X(2)*X(3) + 7*X(3)
Is there a calculation process in the function "sim" I do not know? What do i miss? Please let me know.
2. This quastion is different from above. I thought the output would be more accurate if the number of neurons in hidden layer was large. But in my case, the more hidden layers, the worse the performance. How to find the appropriate number of hidden layers? please give me some tips.
Thanks.
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Deep Learning Toolbox dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!