tf.control_dependencies()作用及用法
来源:互联网 发布:js简繁体切换 编辑:程序博客网 时间:2024/05/22 04:58
在有些机器学习程序中我们想要指定某些操作执行的依赖关系,这时我们可以使用tf.control_dependencies()
来实现。 control_dependencies(control_inputs)
返回一个控制依赖的上下文管理器,使用with
关键字可以让在这个上下文环境中的操作都在control_inputs
执行。
with g.control_dependencies([a, b, c]): # `d` and `e` will only run after `a`, `b`, and `c` have executed. d = ... e = ...
可以嵌套control_dependencies
使用
with g.control_dependencies([a, b]): # Ops constructed here run after `a` and `b`. with g.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`.
注意:
控制依赖只对那些在上下文环境中建立的操作有效,仅仅在context中使用一个操作或张量是没用的
# WRONGdef my_func(pred, tensor): t = tf.matmul(tensor, tensor) with tf.control_dependencies([pred]): # The matmul op is created outside the context, so no control # dependency will be added. return t# RIGHTdef my_func(pred, tensor): with tf.control_dependencies([pred]): # The matmul op is created in the context, so a control dependency # will be added. return tf.matmul(tensor, tensor)
例子:
在训练模型时我们每步训练可能要执行两种操作,op a, b
这时我们就可以使用如下代码:
with tf.control_dependencies([a, b]): c= tf.no_op(name='train')#tf.no_op;什么也不做sess.run(c)
在这样简单的要求下,可以将上面代码替换为:
c= tf.group([a, b])sess.run(c)
阅读全文
2 0
- tf.control_dependencies()作用及用法
- tf.control_dependencies()
- tensorflow API:tf.control_dependencies
- tf.control_dependencies()和tf.identity()
- tf.control_dependencies与tf.identity组合详解
- tf.cond 与 tf.control_dependencies 的控制问题
- @Override用法及作用
- SQLite作用及用法
- strdup用法及作用
- instanceof 用法及作用
- makefile用法及作用
- @XmlRootElement作用及用法
- Ajax作用及用法
- tf.unstack的作用
- tf.identity的作用
- 注册表的作用及用法
- sqlparameter 的用法及作用
- DOCTYPE作用及用法详解
- UVa10935
- avformat的简单应用
- android开发之Glide加载RelativeLayout背景图
- 四. 就绪列表
- NSMutaleArray Quick Sort
- tf.control_dependencies()作用及用法
- OGNL
- nginx接受post数据,并在日志里记录数据
- lib 和 dll 的区别、生成以及使用详解
- 盒子阴影-box-shadow
- 万物生长 项目jar包越少越好,不然会导致eclipse进入断点延迟高,很慢. Spring+quartz 实现定时任务job集群配置
- 为django配置404和502错误页面
- JavaScript之this解密
- ERROR JDBCExceptionReporter:234