Variable 变量
定义语法: state = tf.Variable()
import tensorflow as tf
state = tf.Variable(0, name='counter')
# 定义加法步骤 (注: 此步并没有直接计算)
new_value = tf.add(state,1)
# 将 State 更新成 new_value
update = tf.assign(state, new_value)
# 使用 Session
with tf.Session() as sess:
sess.run(tf.global_variables_initializer()
for _ in range(3):
sess.run(update)
print(sess.run(state))
一定要把 sess 的指针指向 state 再进行 print 才能得到想要的结果
Last updated
Was this helpful?