Reputation: 15
i'm new to python and I think there is something I don't understand with how nested loop work. I have this nested loop that should run multiple times until precision eps is reached but it exits after running once. I tried debugging and realized that when the epoch +=1 incrementation line is reached, w and w_last are both modified by the 2nd loop and are equal. I don't understand why this is the case since only w should be modified by the second loop. I have the same problem using a for loop. There is something I don't understand, maybe someone can point it out
while(all(abs(w - w_last)) >= eps) :
w_last = w
sum_error = 0
j = 0
while j < n_data :
y_pred[j] = w[0]+ (w[1]*x[j])
error = y_pred[j] - y[j]
sum_error += error**2
w[0] = w[0] - step * error
w[1] = w[1] - step * error * x[j]
j += 1
epoch += 1
print('>epoch=%d, step=%.4f, error=%.3f' % (epoch, step, sum_error))
print(w)
w is a (2,1) numpy array of weights epoch keeps track of the number of time I run through the data (2nd loop)
Thank you!
Upvotes: 1
Views: 113
Reputation: 57105
w_last = w
makes another reference to w
, not a copy of it. You must use w_last = w.copy()
.
Upvotes: 2