前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >tf.control_dependencies()

tf.control_dependencies()

作者头像
狼啸风云
修改2022-09-04 21:37:00
1.1K0
修改2022-09-04 21:37:00
举报

tensorflow可以协调多个数据流,在存在依赖的节点下非常有用,例如节点B要读取模型参数值V更新后的值,而节点A负责更新参数V,所以节点B就要等节点A执行完成后再执行,不然读到的就是更新以前的数据。这时候就需要个运算控制器tf.control_dependencies。

使用默认图形包装graph.control_dependencies()。

tf.control_dependencies(control_inputs)

当启用紧急执行时,control_input列表中的任何可调用对象都将被调用。

参数:

  • control_input:在运行上下文中定义的操作之前必须执行或计算的操作或张量对象的列表。也可以是None来清除控件依赖项。如果启用了立即执行,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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019年07月27日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档