import tensorflow as tf
#P11 例子 https://www.bilibili.com/video/av16001891/?p=11
state = tf.Variable(0,name='counter')
print(state.name)
one = tf.constant(1)
new_value = tf.add(state,one)
#把new_value的值赋值给state
update = tf.assign(state,new_value)
#如果定义了variable一定要初始化
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
网友评论