ckorzhik
ckorzhik

Reputation: 788

Tensorflow: how to assign variables properly

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?

Upvotes: 0

Views: 531

Answers (1)

mrry
mrry

Reputation: 126154

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]

Upvotes: 1

Related Questions