import tensorflow as tf
#保证sess.run()能够正常运行
tf.compat.v1.disable_eager_execution()
#变量state
state = tf.Variable(0,name = ‘counter’)
#print(state.name)
#常量one
one = tf.constant(1)
new_value = tf.add(state,one)
update = tf.compat.v1.assign(state, new_value)
#如果有变量,一定要初始化
init = tf.compat.v1.global_variables_initializer()
with tf.compat.v1.Session() as sess:
#要run一下init
sess.run(init)
#下划线做变量名代表不需要这个值,_是只读不写的变量
for _ in range(3):
sess.run(update)
print(sess.run(state))