1

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!

Amsterdoom
  • 15
  • 4
  • 1
    Can't test atm but my suspicion is that `w_last = w.copy()` might fix the issue, in which case we can direct you to the right resources. – roganjosh Jan 24 '18 at 18:07
  • 1
    Possible duplicate of [How to clone or copy a list?](https://stackoverflow.com/questions/2612802/how-to-clone-or-copy-a-list) – Aran-Fey Jan 24 '18 at 18:14
  • As a side note, assuming that `y_pred`, `y`, and `x` are numpy arrays, too, you do not need the inner loop at all. – DYZ Jan 24 '18 at 18:19
  • I don't understand why I wouldn't need the inner loop, it's a stochastic gradient descent so I need to compute the weight variation on each sample and keep track of the epoch numbers (number of time I loop through the entire dataset). Outer loop is basically my stopping criteria, might be a better way to implement. – Amsterdoom Mar 01 '18 at 17:15

1 Answers1

2

w_last = w makes another reference to w, not a copy of it. You must use w_last = w.copy().

DYZ
  • 55,249
  • 10
  • 64
  • 93