目录
tf.control_dependencies 依赖关系配置
此函数指定某些操作执行的依赖关系
返回一个控制依赖的上下文管理器,使用 with 关键字可以让在这个上下文环境中的操作都在 control_inputs 执行
with tf.control_dependencies([a, b]):
c = ....
d = ...
在执行完 a,b 操作之后,才能执行 c,d 操作。意思就是 c,d 操作依赖 a,b 操作
with tf.control_dependencies([train_step, variable_averages_op]):
train_op = tf.no_op(name='train')
tf.no_op()表示执行完 train_step, variable_averages_op 操作之后什么都不做
update_ops
global_norm
colocate_with
with ops.colocate_with(self.global_step):
self.train = state_ops.assign_add(self.global_step, 1).op
It's a context manager to make sure that the operation or tensor you're about to create will be placed on the same device the reference operation is on. Consider this piece of code (tested):
import tensorflow as tf
with tf.device("/cpu:0"):
a = tf.constant(0.0, name="a")
with tf.device("/gpu:0"):
b = tf.constant(0.0, name="b")
with tf.colocate_with(a):
c = tf.constant(0.0, name="c")
d = tf.constant(0.0, name="d")
for operation in tf.get_default_graph().get_operations():
print(operation.name, operation.device)
Outputs:
(u'a', u'/device:CPU:0')
(u'b', u'/device:GPU:0')
(u'c', u'/device:CPU:0')
(u'd', u'/device:GPU:0')
- 参考资料