CodeCollection2018
10/26/2018 - 12:55 PM

tensorflow.assign赋值操作方法

assign包含技巧

import tensorflow as tf
x = tf.Variable(0)
y = tf.assign(x, 1)
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    print sess.run(x)
    print sess.run(y)
    print sess.run(x)
--------------------- 
In [212]: w = tf.Variable(12)
In [213]: w_new = w.assign(34)
 
In [214]: with tf.Session() as sess:
     ...:     sess.run(w_new)
     ...:     print(w_new.eval())
 
# output
34 
--------------------- 
import tensorflow as tf
x = tf.Variable(0)
sess = tf.Session()
sess.run(tf.global_variables_initializer())
print(sess.run(x))  # Prints 0.
x.load(1, sess)
print(sess.run(x))  # Prints 1.