How to exit a while loop after a certain number of values have been saved to a matrix

16 views (last 30 days)
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?

Answers (1)

James Tursa
James Tursa on 12 May 2020
Edited: James Tursa on 12 May 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 Comments
MarkM
MarkM on 12 May 2020
Edited: MarkM on 13 May 2020
Sum_of_ErrorSquared = (sum(TargetTraining(i,:)' - v2)).^2;
end
MSEData(MaxEpochs,1) = Sum_of_ErrorSquared./1400;
So this part of the loop adds the result of the sum of error squared/max number of epochs to the matrix MSEData. The MSEData should be filled 1400 times, which is my max number of epochs, then I should exit the while loop after the final row of MSEData has been filled by this equation.
The number of training sets for the NN is also 1400, which is why I loop through 1400 twice.
I have tried setting exit conditons for the while loop but it doesnt seem to acknowledge them and the internal loop continues, refilling the MSEData from the start again.
MarkM
MarkM on 13 May 2020
Maybe I should rephrase my question - how can I save the MSEdata as a single column matrix, and then exit the while loop after 1400 rows have been added to MSEdata?

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!