tensorflow可以协调多个数据流,在存在依赖的节点下非常有用,例如节点B要读取模型参数值V更新后的值,而节点A负责更新参数V,所以节点B就要等节点A执行完成后再执行,不然读到的就是更新以前的数据。这时候就需要个运算控制器tf.control_dependencies。
使用默认图形包装graph.control_dependencies()。
tf.control_dependencies(control_inputs)
当启用紧急执行时,control_input列表中的任何可调用对象都将被调用。
参数:
返回值:
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`.
原链接:https://tensorflow.google.cn/api_docs/python/tf/control_dependencies?hl=en