with tf.control_dependencies(control_inputs)
??exec_ops
在执行某些 exec_ops 之前,control_inputs得首先被运行。但是对于 Variable 的 identity op 会在 control dependence 后重新计算,但其它 op 都不会重新计算。
x = tf.Variable(0.0)
x_plus_1 = tf.assign_add(x, 1)
with tf.control_dependencies([x_plus_1]):
y = x
init = tf.initialize_all_variables()
with tf.Session() as session:
init.run()
for i in xrange(5):
print(y.eval())
print 0, 0, 0, 0, 0
x = tf.Variable(0.0)
x_plus_1 = tf.assign_add(x, 1)
with tf.control_dependencies([x_plus_1]):
y = tf.identity(x)
init = tf.initialize_all_variables()
with tf.Session() as session:
init.run()
for i in xrange(5):
print(y.eval())
prints 1, 2, 3, 4, 5
返回一个tensor(b),这个tensor的size和shape与输入tensor(a)是相同的。
可以用于CPU和GPU等不同设备之间的数据传输,以及与 control_dependencies 配合使用。
原文:https://www.cnblogs.com/qccz123456/p/12752260.html