python: variable in while loop -
i'm running following code in python:
w= np.random.rand(3) w_old=np.zeros((3,)) while (np.linalg.norm(w - w_old)) / np.linalg.norm(w) > 1e-5: w_old=w print w print w_old w[0]-=eta*de1i w[1]-=eta*de2i w[2]-=eta*de3i print w print w_old
the results prints :
[ 0.22877423 0.59402658 0.16657174] [ 0.22877423 0.59402658 0.16657174]
and
[ 0.21625852 0.5573612 0.123111 ] [ 0.21625852 0.5573612 0.123111 ]
i'm wondering why value of w_old
has been changed? shouldn't updated after going beginning of while loop? how can fix this?
just using
w_old = w
doesn't copy w
, using =
tells python want name whatever stored in w
. every in-place change w
change w_old
. there nice blog post in case want more details ned batchelder: "facts , myths python names , values"
you can explicitly copy numpy array, example, using copy
method:
w_old = w.copy()
Comments
Post a Comment