tf.control_dependencies

简单来说,tf.control_dependencies([,]) 是用来保证with下的op执行之前,先执行传入的op

tf.identify() 则是返回一个与传入tensor一模一样新的tensor的op,这会增加一个新节点到gragh中。

1
2
3
4
5
6
7
8
9
10
11
12
x = tf.Variable(1.0)
y = tf.Variable(0.0)
x_plus_1 = tf.assign_add(x, 1) # 一个operation, 简称op

with tf.control_dependencies([x_plus_1]): # 这里传入op的列表
y = tf.identity(x) # 这里必须也是op,否则control无效
init = tf.initialize_all_variables()

with tf.Session() as session:
init.run()
for i in xrange(5):
print(y.eval()) # 2,3,4,5,6

参考