import tensorflow as tf state = tf.Variable(0, name='counter') # 定义了名字是counter,初始值为0的计数器 print(state.name) one = tf.constant(1) new_value = tf.add(state, one) update = tf.assign(state, new_value) print(new_value) init = tf.initialize_all_variables() # 在tf中定义变量Variable 一定要初始化 with tf.Session() as sess: sess.run(init) #一定要run(init) for _ in range(3): sess.run(update) print(sess.run(state))