参考这里点击打开链接的信息我们可以知道,TF可以协调多个数据流,在存在依赖的节点下非常有用,例如节点B要读取模型参数值V更新后的值,而节点A负责更新参数V,所以节点B就要等节点A执行完成后再执行,不然读到的就是更新以前的数据。这时候就需要个运算控制器tf.control_dependencies。
参考官方说明文档
format:control_dependencies(self, control_inputs)
arguments:control_inputs: A list of `Operation` or `Tensor` objects which must be executed or computed before running the operations defined in the context. (注意这里control_inputs是list)
return: A context manager that specifies control dependencies for all operations constructed within the context.(返回所有在环境中的控制依赖的上下文管理器)
其实用法很简单,只有在 control_inputs被执行以后,上下文管理器中的操作才会被执行。例如
with tf.control_dependencies([a, b, c]):
# `d` and `e` will only run after `a`, `b`, and `c` have executed.
d = ...
e = ...
只有[a,b,c]都被执行了才会执行d和e操作,这样就实现了流的控制。
当然,官方文档里还介绍了嵌套多个流控制
with tf.control_dependencies([a, b]):
# Ops constructed here run after `a` and `b`.
with tf.control_dependencies([c, d]):
# Ops constructed here run after `a`, `b`, `c`, and `d`
也能通过参数None清除控制依赖例如
with g.control_dependencies([a, b]):
# Ops constructed here run after `a` and `b`.
with g.control_dependencies(None):
# Ops constructed here run normally, not waiting for either `a` or `b`.
with g.control_dependencies([c, d]):
# Ops constructed here run after `c` and `d`, also not waiting
# for either `a` or `b`.