tf.Graph.control_dependencies(control_inputs)
用在上下文管理器中,参数control_inputs是op或tensor的list,其运行先于上下文管理器中的op或tensor。也可以为None,此时取消了控制依赖关系。
with tf.Session() as sess:
a = tf.Variable(0)
b = tf.Variable(0)
c = tf.add(a, b)
tf.global_variables_initializer().run()
with tf.control_dependencies([c]):
a = tf.assign(a, 5)
b = tf.assign(b, 3)
print(sess.run([a, b, c]))
# =>[5, 3, 0]
with tf.control_dependencies([c]):
a = tf.assign(a, 0)
print(sess.run([a, b, c]))
# =>[0, 3, 8]