In [1]:
import tensorflow as tf
In [9]:
# 创建变量
a = tf.Variable([1, 0])
b = tf.Variable([0, 1])
# sub/add 两个 operation
sub = tf.subtract(a, b)
add = tf.add(a, b)

# 初始化变量
init = tf.global_variables_initializer()
In [10]:
# 执行 Session
with tf.Session() as sess:
    sess.run(init)
    print(sess.run(sub))
    print(sess.run(add))
[ 1 -1]
[1 1]
In [12]:
# Counter
counter = tf.Variable(0, name="counter")
add_one = tf.add(counter, 1)
assign = tf.assign(counter, add_one)
init = tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init)
    for _ in range(5):
        sess.run(assign)
        print(sess.run(counter))
1
2
3
4
5