my neural network training code goes to an infinite loop

Hi. I am writing a neural network training code, the big problem is that when i run the program it goes to an infinite loop. I debugged the code and i discovered that it isn't comparing two variables correctly. The if loop if (u ~= result(1,i)) seems to be the problem, i look at the code many times and for me its ok. Can anyone tell me what's i am doing wrong, please.
The code is below, and the excel table is attached.
Thank You.
close all;clear all;clc
file = 'amostras.xlsx';
Amostras = xlsread(file)
result = Amostras (1,:)
amostra = Amostras(2:end,:)
y = size(Amostras)
n1 = y(1)
n2 = y(2)
Amostras(1,:) = -1
w = rand (1,n1)
n = (n1*n2)+(n1*n2)
n = 1/n
epoch = 0;
T = 1;
i=1;
for i = 1:n2,
err=1;
while err == 1,
u = w * Amostras(:,i)
des = result(1,i)
if (u ~= result(1,i))
rap = (n*(result(1,i)-u)*Amostras(:,i))
w = w + rap'
err = 1
epoch = epoch +1
T = T
else
err = 0
epoch = epoch
T = T +1
end
end
i = i +1;
end
disp(w)
disp(err)

 Accepted Answer

Sérgio - looking at your Amostras data, the first row (which is your result array) is made up of positive and negative ones. Your condition to change err from one to zero (so that you exit the while loop) is
u ~= result(1,i)
The above inequality if fine for integers, but when comparing floating point numbers it is best to use some sort of tolerance so that if the two numbers being compared are "close enough" then this is sufficient. Try
tolerance = 1.0e-6;
if abs(u - result(1,i)) > tolerance
rap = (n*(result(1,i)-u)*Amostras(:,i))
w = w + rap'
err = 1
epoch = epoch +1
T = T
else
err = 0
epoch = epoch
T = T +1
end

1 Comment

Thank you Geoff, you solved my problem. I was starting to think that the problem was some kind of tolerance fault. Do you recomend any good matlab book to learn those tricks? Thank you again.
Best Regards.
Sérgio

Sign in to comment.

More Answers (0)

Categories

Find more on Deep Learning Toolbox 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!