tf.control_dependencies()

参考这里点击打开链接的信息我们可以知道,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`.




  • 4
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值