How to exit a while loop after a certain number of values have been saved to a matrix
19 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I am following pseudocode issued with the task I have been given to train a neural network. We have been told to write the code from scratch and not use the functions/toolboxes already available for MATLAB.
I am having an issue using a while loop. Inside the while loop there is a for loop performing a calculation and saving the value for each iteration to an m x 1 matrix. For this matrix I created a 1400x1 matrix of zeros, and alter the value of each row with each interation of the for loop (could I do this a better way?).
I want to exit the while loop after the final row has been calculated for my matrix. I am not sure what to enter for the while conditions to make this happen. Here is the code I have at the moment:
% Main Loop Data
MaxEpochs = 1400;
MSE = 1000;
MSEData = zeros(MaxEpochs,1);
MSEData(1,1) = MSE;
MSE_threshold = 1e-4;
% Main loop for training NN
while ((MSEData > MSE_threshold) || MaxEpochs reached)) % as written in pseudocode
for MaxEpochs = 1:1400
% Forward pass
for i = 1:1400
x = TrainingSet(i,:)';
% Hidden layer O/P, bipolar sigmoid function(activation function)
u1 = W1*x+b1;
v1 = (1-exp(-u1))./(1+exp(-u1));
% Output layer O/P, bipolar sigmoid function(activation function)
u2 = (W2*v1)+b2;
v2 = (1-exp(-u2))./(1+exp(-u2));
% Backward pass
d2 = (TargetTraining(i,:)' - v2).*((1/2)*(1-(v2.^2)));
d1 = (W2'*d2).*((1/2)*(1-(v1.^2)));
% Calculate new b2 and W2 weights
d2 = d2';
dW2 = (d2.*v1)*0.001;
db2 = d2*0.001;
% Calculate new b1 and W1 weights
d1 = d1';
dW1 = (d1.*x)*(-0.001);
db1 = d1*0.001;
% Updated weights and biases
W2 = W2+dW2';
b2 = b2+db2';
W1 = W1+dW1';
b1 = b1+db1';
Sum_of_ErrorSquared = (sum(TargetTraining(i,:)' - v2)).^2;
end
MSEData(MaxEpochs,1) = Sum_of_ErrorSquared./1400;
end
end
As it stands the above code successfully adds the new data required to the "MSEData" matrix. However, it does not exit the while loop after reaching row 1400, and I havent mangaged to figure out a way to do this. How can I get the loop to do this?
0 comentarios
Respuestas (1)
James Tursa
el 12 de Mayo de 2020
Editada: James Tursa
el 12 de Mayo de 2020
The general form for while loops is typically some variation of these:
while( condition )
% stuff that eventually alters the condition
end
or
while( true )
% stuff that eventually alters the condition
if( condition )
break;
end
end
Or you could combine the two and have a while loop with a main condition and also an "early exit" condition inside the loop.
It looks like you are coding that first syntax since you have conditions to test in the while statement. But then it looks like you have a simple for-loop inside the while loop and you simply want to exit the while loop after this for-loop finishes? And you are using MaxEpochs for two different things ... a maximum epochs value and a for-loop index. It is not clear to me what you really want to do, and why you have both a while-loop and a for-loop. Maybe just put a break after the for-loop? Maybe only have the for-loop?
Can you provide more explanation?
2 comentarios
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!