stark - 4 months ago 48

Python Question

I am a bit confused by how the following code segment runs.

`import tensorflow as tf`

x = tf.Variable(0)

init_op = tf.initialize_all_variables()

modify_op = x.assign(5)

with tf.Session() as sess:

sess.run(init_op)

print(sess.run(x))

x += 3

print(sess.run(x))

sess.run(init_op) # Trying to initialize x once again to 0

print(sess.run(x)) # Gives out 3, which leaves me confused.

print(sess.run(modify_op))

print(sess.run(x)) # Gives out 8, even more confusing

This is the output:

`0`

`3`

`3`

`5`

`8`

Is it that the line

`x += 3`

Answer

Your `x`

variable is being changed by

```
x += 3
```

but not in a way you might expect. The tensorflow library code over-rides `+`

, so that you are effectively swapping the contents `x`

for a new TF tensor (the old one will still be in the graph, just x now points to a new one). Write it out like this:

```
x = tf.Variable(0) + 3
```

and it is clearer what is going on. Also, insert some print statements . . .

```
x = tf.Variable(0)
print(x)
# <tensorflow.python.ops.variables.Variable object at 0x1018f5d68>
x += 3
print(x)
# Tensor("add:0", shape=(), dtype=int32)
```