Azzera filtri
Azzera filtri

Variable updation inside a for loop

2 visualizzazioni (ultimi 30 giorni)
Niraj Reginald
Niraj Reginald il 18 Mag 2018
Modificato: Walter Roberson il 18 Mag 2018

Hi all, i have written the following for loop which calculates an error through 3 set of input data. how do i update the value until the error reaches a threshold value. I want to add E3 to E1 at the start of each iteration.

%STEP 01 INITIALIZATION
k = 1;
neta = 0.2;%learning rate 
Lamda = 1;
%ActF = 1/(1+exp(-1*Lamda*Tot));%Activation Function
E = 0;%Current Error
Emax = 0;%Maximum Error
Epoch = 5000; % We will do 100 iterations for this Assignment
x = [0.3 0.4 -1;0.1 0.6 -1;0.9 0.4 -1];
t = [0.88 0.82 0.57];
A = [0.2 0.2 0.2;0.2 0.2 0.2];
Whi = A';%weights from input to hidden layer
B = [0.2 0.2 0.2];
Who = B';%%weights from hidden to output layer
WeightsO3 = Whi(:,1);
WeightsO4 = Whi(:,2);
x1  = x(1,:);
x2  = x(2,:);
x3  = x(3,:);
t1  = t(:,1);
t2  = t(:,2);
t3  = t(:,3);
E(Iter) = E;
for Iter = 1:Epoch
O3net_1stSet = x1*WeightsO3;%summation of Node 3 for 1st input
O3_1stSet=sigmf(O3net_1stSet,[1 0]);
O4net_1stSet = x1*WeightsO4;
O4_1stSet=sigmf(O4net_1stSet,[1 0]);
O5_1stSet = -1;
Hidden_Layer_Outputs = [O3_1stSet O4_1stSet O5_1stSet];
O6_1stSet = Hidden_Layer_Outputs*Who;
O6_1stSet = sigmf(O6_1stSet,[1 0]);
E1 = ((t1-O6_1stSet)^2)/2 + E3
Delta6 = O6_1stSet*(1-O6_1stSet)*(t1-O6_1stSet);
DeltaW = neta*Delta6*[O3_1stSet O4_1stSet O5_1stSet];%Weight difference input to hidden layer
Delta3 = O3_1stSet*(1-O3_1stSet)*Who(1,1)*Delta6;%Hidden Layer Weight difference update
Delta4 = O4_1stSet*(1-O4_1stSet)*Who(2,1)*Delta6;
DeltaWO3 = neta*Delta3*[x1(1,1) x1(1,2) x1(1,3)];
DeltaWO4 = neta*Delta4*[x1(1,1) x1(1,2) x1(1,3)];
Who = Who + DeltaW';
WeightsO3 = WeightsO3 + DeltaWO3';%updated weights of input to Node 3
WeightsO4 = WeightsO4 + DeltaWO4';%updated weights of input to Node 3
O3net_2ndSet = x2*WeightsO3;%summation of Node 3 for 2nd input
O3_2ndSet=sigmf(O3net_2ndSet,[1 0]);
O4net_2ndSet = x2*WeightsO4;
O4_2ndSet=sigmf(O4net_2ndSet,[1 0]);
O5_2ndSet = -1;
Hidden_Layer_Outputs = [O3_2ndSet O4_2ndSet O5_2ndSet];
O6net_2ndSet = Hidden_Layer_Outputs*Who;
O6_2ndSet = sigmf(O6net_2ndSet,[1 0]);
E2 = ((t2-O6_2ndSet)^2)/2+E1
Delta6 = O6_2ndSet*(1-O6_2ndSet)*(t2-O6_2ndSet);
DeltaW = neta*Delta6*[O3_2ndSet O4_2ndSet O5_2ndSet];%Weight difference input to hidden layer
Delta3 = O3_2ndSet*(1-O3_2ndSet)*Who(1,1)*Delta6;%Hidden Layer Weight difference update
Delta4 = O4_2ndSet*(1-O4_2ndSet)*Who(2,1)*Delta6;
DeltaWO3 = neta*Delta3*[x2(1,1) x2(1,2) x2(1,3)];
DeltaWO4 = neta*Delta4*[x2(1,1) x2(1,2) x2(1,3)];
Who = Who + DeltaW';
WeightsO3 = WeightsO3 + DeltaWO3';%updated weights of input to Node 3
WeightsO4 = WeightsO4 + DeltaWO4';%updated weights of input to Node 3
O3net_3rdSet = x3*WeightsO3;%summation of Node 3 for 2nd input
O3_3rdSet=sigmf(O3net_3rdSet,[1 0]);
O4net_3rdSet = x3*WeightsO4;
O4_3rdSet=sigmf(O4net_3rdSet,[1 0]);
O5_3rdSet = -1;
Hidden_Layer_Outputs = [O3_3rdSet O4_3rdSet O5_3rdSet];
O6net_3rdSet = Hidden_Layer_Outputs*Who;
O6_3rdSet = sigmf(O6net_3rdSet,[1 0]);
E3 = ((t3-O6_3rdSet)^2)/2+E2
Delta6 = O6_3rdSet*(1-O6_3rdSet)*(t3-O6_3rdSet);
DeltaW = neta*Delta6*[O3_3rdSet O4_3rdSet O5_2ndSet];%Weight difference input to hidden layer
Delta3 = O3_3rdSet*(1-O3_3rdSet)*Who(1,1)*Delta6;%Hidden Layer Weight difference update
Delta4 = O4_3rdSet*(1-O4_3rdSet)*Who(2,1)*Delta6;
DeltaWO3 = neta*Delta3*[x3(1,1) x3(1,2) x3(1,3)];
DeltaWO4 = neta*Delta4*[x3(1,1) x3(1,2) x3(1,3)];
Who = Who + DeltaW';
WeightsO3 = WeightsO3 + DeltaWO3';%updated weights of input to Node 3
WeightsO4 = WeightsO4 + DeltaWO4';%updated weights of input to Node 3
if E < 0.01
    break 
else
   continue 
end
end
  1 Commento
Image Analyst
Image Analyst il 18 Mag 2018
Modificato: Image Analyst il 18 Mag 2018
"how do i update the value" -- WHAT value?
"until the error reaches a threshold value" What variable is the error? Is it E, E1, E2, or E3 or some other value?
Get rid of this line:
E(Iter) = E;
"I want to add E3 to E1 at the start of each iteration." So do this right after the for line:
E1 = E1 + E3;

Accedi per commentare.

Risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by