tensorflow-tf.control_dependencies()作用及用法
tensorflow tf.control_dependencies()作用及用法
在有些机器学习程序中我们想要指定某些操作执行的依赖关系,这时我们可以使用tf.control_dependencies()
来实现。
control_dependencies(control_inputs)
返回一个控制依赖的上下文管理器,使用with关
键字可以让在这个上下文环境中的操作都在control_inputs
执行。
原型分析
arguments:control_inputs: A list of
Operation
orTensor
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.
1with g.control_dependencies([a, b, c]):
2 # `d` and `e` will only run after `a`, `b`, and `c` have executed.
3 d = ...
4 e = ...
可以嵌套control_dependencies
使用
1with g.control_dependencies([a, b]):
2 # Ops constructed here run after `a` and `b`.
3 with g.control_dependencies([c, d]):
4 # Ops constructed here run after `a`, `b`, `c`, and `d`.
可以传入None
来消除依赖:
1with g.control_dependencies([a, b]):
2 # Ops constructed here run after `a` and `b`.
3 with g.control_dependencies(None):
4 # Ops constructed here run normally, not waiting for either `a` or `b`.
5 with g.control_dependencies([c, d]):
6 # Ops constructed here run after `c` and `d`, also not waiting
7 # for either `a` or `b`.
注意:控制依赖只对那些在上下文环境中建立的操作有效,仅仅在context中使用一个操作或张量是没用的
1# WRONG
2def my_func(pred, tensor):
3 t = tf.matmul(tensor, tensor)
4 with tf.control_dependencies([pred]):
5 # The matmul op is created outside the context, so no control
6 # dependency will be added.
7 return t
8
9# RIGHT
10def my_func(pred, tensor):
11 with tf.control_dependencies([pred]):
12 # The matmul op is created in the context, so a control dependency
13 # will be added.
14 return tf.matmul(tensor, tensor)
例子:在训练模型时我们每步训练可能要执行两种操作,op a, b 这时我们就可以使用如下代码:
1with tf.control_dependencies([a, b]):
2 c= tf.no_op(name='train')#tf.no_op;什么也不做
3sess.run(c)
4
5# 在这样简单的要求下,可以将上面代码替换为:
6c= tf.group([a, b])
7sess.run(c)
其他关于tf.identity()
的奇怪操作可见https://blog.csdn.net/u012436149/article/details/72084744
使用tf.no_op()
是一个占位符,表示什么都不做,但是会返回一个operation,用以保证tf.control_dependencies()
被执行,和tf.group
操作类似。
版权声明:本文为CSDN博主「PKU_Jade」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/PKU_Jade/article/details/73498753