tensorflow学习之tf.assign

tf.assign(ref, value, validate_shape=None, use_locking=None, name=None),

函数功能是将value赋值给ref

ref必须是tf.Variable创建的tensor,如果ref=tf.constant()就会报错,而且默认情况下ref的shape和value的shape是相同的

 

import tensorflow as tf
state = tf.Variable(0,name='counter')
print(state.name)
one = tf.constant(1)
new_value = tf.add(state,one)
update = tf.assign(state,new_value)
init = tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init)
    for __ in range(3):
        sess.run(update)
        print(sess.run(state))


#counter:0
1
2
3

 

上一篇:TENSORFLOW:GRAPH 和 SESSION续


下一篇:TensorFlow学习笔记0-安装TensorFlow环境