Jessica
Jessica

Reputation: 31

Python program,assign_add, the result might be a little confusing

enter image description here

   b= tf.ones([3,3],dtype=tf.float32)
   c= tf.Variable(tf.random_normal([3,3],mean=1,stddev=2,dtype=tf.float32),name="c")
   d=tf.assign_add(c,b)
   init_op= tf.global_variables_initializer()
   with tf.Session() as sess:
     sess.run(init_op)
     x,y,z= sess.run([c,b,d])
     print(x,"\n!!\n",y,"\n!!\n",z,"\n!!")
     print(sess.run(d))

the third array output result should be added 'b' and 'c', but the result did not print as expected. Can you help me? Many thanks.

I expect the third Array output result should be the same as the fourth Array. I am confused at the third Array result.

Upvotes: 1

Views: 116

Answers (1)

xdurch0
xdurch0

Reputation: 10474

It seems like contrary to how I understand the docs on assign_add, this op might actually return the value of the variable before the adding is done, not after.

So in this example c is the variable with value [[4.723362, ...]]. In the first sess.run, you are printing c which has the pre-assign_add value since the update is done after, b which looks as expected and d which also returns the pre-assign_add value because that's presumably how the op works.
After this, c is updated to now contain values [[5.723362, ...]]. Now you run d (the assign_add op) again, which updates c but once again returns the pre-update value, which is [[5.723362, ...]].

In conclusion, if we accept that the return value of tf.assign_add is the variable before adding, and that running c along with d in the first "parallel" run call will return c before doing the update, this makes sense. You could verify this by adding a sess.run(c) at the very end, which should return [[6.723362,...].

Upvotes: 1

Related Questions