Difference between tf.assign and assignment operator (=)

First, the anwser is not really precise. IMO, there's no distinguish between python object and tf object. They are all memory objects managed by python GC.

If you change second a to b, and print vars out,

In [2]: g = tf.Graph()

In [3]: with g.as_default():
   ...:     a = tf.Variable(1, name='a')
   ...:     b = a + 1
   ...:

In [4]: print(a)
<tf.Variable 'a:0' shape=() dtype=int32_ref>

In [5]: print(b)
Tensor("add:0", shape=(), dtype=int32)

In [6]: id(a)
Out[6]: 140253111576208

In [7]: id(b)
Out[7]: 140252306449616

a and b are not referring the same object in memory.

Draw the computation graph, or memory graph

first-line,

# a = tf.Varaible(...
a -> var(a)

second line,

# b = a + 1
b -> add - var(a)
      |
       \-- 1

now if you replace it back to your b = a + 1 to a = a + 1, the a after assign operation is pointing to an tf.add object instead of the variable a incremented by 1.

When you run sess.run, you are fetching the result by that add operator with no side effect to the original a variable.

tf.assign, on the other hand, will have the side effect of updating the state of the graph under the session.


The main confusion here is that doing a = a + 1 will reassign the Python variable a to the resulting tensor of the addition operation a + 1. tf.assign, on the other hand, is an operation for setting the value of a 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 2nd snippet using (=), when I have sess.run(a), it seems I'm running an assign op. So does "a = a+1" internally create an assignment op like assign_op = tf.assign(a, a+1)? [...]

It might look so, but not true. As explained above, this will only reassign 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'm not sure how to explain the 3rd snippet. Why the two evals increment a, but the two evals in the 2nd snippet doesn't?

That's because calling eval() on the assignment tensor in the third snippet also triggers the variable assignment (note that this isn't much different from doing session.run(a) with the current session).

Tags:

Tensorflow