0

It's not duplicate of How to assign value to a tensorflow variable?

I was trying to do simpliest thing: just swap variables Tensorflow: how to swap variables between scopes and set variables in scope from another, and I still can't do it.

BUT now I know that assign changes even copy of tensor which I get with tf.identity. I don't want this. I need copy of variable for swapping.

In [10]: a = tf.Variable(1)

In [11]: b = tf.identity(a)

In [12]: a += 1

In [14]: sess.run(a)
Out[14]: 2

In [15]: sess.run(b)
Out[15]: 1

In [16]: a = tf.Variable(1)

In [17]: b = tf.identity(a)

In [18]: assign_t = a.assign(2)

In [20]: sess.run(tf.initialize_all_variables())

In [21]: sess.run(a)
Out[21]: 1

In [22]: sess.run(assign_t)
Out[22]: 2

In [23]: sess.run(a)
Out[23]: 2

In [24]: sess.run(b)
Out[24]: 2

How can I assign value to a without changing b?

Community
  • 1
  • 1
ckorzhik
  • 758
  • 2
  • 7
  • 21

1 Answers1

1

The tf.identity() operation is stateless. When you have a tf.Variable called a, the value of tf.identity(a) will always be the same as the value of a. If you want b to remember a previous value of a, you should create b as a tf.Variable as well:

a = tf.Variable(1)
b = tf.Variable(a.initialized_value())

sess.run(tf.global_variables_initializer())

# Initially, variables `a` and `b` have the same value.
print(sess.run([a, b])) ==> [1, 1]

# Update the value of `a` to 2.
assign_op = a.assign(2)
sess.run(assign_op)

# Now, `a` and `b` have different values.
print(sess.run([a, b])) ==> [2, 1]
mrry
  • 125,488
  • 26
  • 399
  • 400
  • Thanks! But what about first case, when `a+=1` not equal to `identity(a)`? – ckorzhik Jan 31 '17 at 20:22
  • Oh, I see: `a+=1` make another tensor with different name. – ckorzhik Jan 31 '17 at 20:26
  • That's right. `a += 1` is equivalent to `new_a = a + 1; a = new_a`, which just updates the Python binding for `a` (and not the value in the `tf.Variable`). – mrry Jan 31 '17 at 20:39