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!

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. Commented Jan 24, 2018 at 18:07
  • 1
    Possible duplicate of How to clone or copy a list? Commented Jan 24, 2018 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. Commented Jan 24, 2018 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. Commented Mar 1, 2018 at 17:15

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

I think w_last = w[:] also works, albeit being less readable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.