简单计算器主要介绍如何使用TensorFlow的变量来记录图运行过程中的状态,这对后续复杂例子的学习很重要,请务必掌握。
import tensorflow as tf counter = tf.Variable(0, name="counter") one = tf.constant(1) new_value = tf.add(counter, one) update = tf.assign(counter, new_value) init_op = tf.initialize_all_variables() with tf.Session() as sess: sess.run(init_op) print(sess.run(counter)) for _ in range(3): sess.run(update) print(sess.run(counter))