Difference between tf.assign and assignment operator (=)

I am trying to understand the difference between tf.assign and the assignment operator (=). I have three sets of code

First using simple tf.assign

import tensorflow as tf with tf.Graph().as_default(): a = tf.Variable(1, name="a") assign_op = tf.assign(a, tf.add(a,1)) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) print sess.run(assign_op) print a.eval() print a.eval() 

The conclusion is expected to be

 2 2 2 

Secondly, using the assignment operator

 import tensorflow as tf with tf.Graph().as_default(): a = tf.Variable(1, name="a") a = a + 1 with tf.Session() as sess: sess.run(tf.global_variables_initializer()) print sess.run(a) print a.eval() print a.eval() 

The results are still 2, 2, 2.

Thirdly, I use both tf.assign and the assignment operator

 import tensorflow as tf with tf.Graph().as_default(): a = tf.Variable(1, name="a") a = tf.assign(a, tf.add(a,1)) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) print sess.run(a) print a.eval() print a.eval() 

Now the output becomes 2, 3, 4.

My questions

  • In the second snippet using (=), when I have sess.run (a), it seems like I'm running an op assignment. So, "a = a + 1" internally creates an op assignment, for example assign_op = tf.assign (a, a + 1)? Is op a running session really just assign_op? But when I run a.eval (), it does not continue to increment a, so eval appears, evaluating the β€œstatic” variable.

  • I am not sure how to explain the third fragment. Why are two evals increasing a, but the two ratings in the second fragment are not?

Thanks.

+5
source share
1 answer

The main confusion here is that doing a = a + 1 reassigns the Python variable a to the resulting tensor of the addition operation a + 1 . tf.assign , on the other hand, is an operation to set the value of the TensorFlow variable.

 a = tf.Variable(1, name="a") a = a + 1 

This is equivalent to:

 a = tf.add(tf.Variable(1, name="a"), 1) 

With that in mind:

In the second snippet, using (=) when I have sess.run (a), it seems like I'm running an op assignment. So, "a = a + 1" internally creates an op assignment, for example assign_op = tf.assign (a, a + 1)? [...]

It may look like this, but not like that. As explained above, this only reassigns the Python variable. And without tf.assign or any other operation that changes the variable, it stays with the value 1. Each time a is evaluated, the program will always calculate a + 1 => 1 + 1 .

I am not sure how to explain the third fragment. Why do two evals increase a, but these two ratings are not in the second fragment?

This is because calling eval() in the assignment tender in the third fragment also triggers the assignment of the variable (note that this is not much different from executing session.run(a) with the current session).

+1
source

Source: https://habr.com/ru/post/1271026/


All Articles