what does tf.no_op do and tf.control_dependencies work?

- 控制依赖

with tf.control_dependencies([train_step, variables_averages_op]):
    train_op = tf.no_op(name='train') #train_op does nothin

或者

train_op = tf.group(train_step, variables_averages_op)

贴上stackflow上大神对train_op的回答:

As the documentation says, tf.no_op() does nothing. However, when you create a tf.no_op() inside a with tf.control_dependencies([x, y, z]): block, the op will gain control dependencies on ops x, y, and z. Therefore it can be used to group together a set of side effecting ops, and give you a single op to pass to sess.run() in order to run all of them in a single step.

- tf.control_dependencies

tf.control_dependencies(control_inputs)
control_inputs: A list of Operation or Tensor objects, which must be executed or computed before running the operations defined in the context.
returns:A context manager that specifies control dependencies for all operations constructed within the context

该函数的参数是一个包含Op或者tensor的列表,列表里的操作的执行顺序先于context里面定义的Op和tensor;
该函数返回一个上下文管理器

 

代码示例

with g.control_dependencies([a, b, c]):
      # `d` and `e` will only run after `a`, `b`, and `c` have executed.
      d = ...
      e = ...

#也可以嵌套使用
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中使用一个操作或张量是没用的

# WRONG
def 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

# RIGHT
def 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)

参考

https://stackoverflow.com/questions/46801154/what-does-tf-no-op-do
https://blog.csdn.net/PKU_Jade/article/details/73498753
https://blog.csdn.net/u012436149/article/details/72084744

记录时间

2018/9/13 12:07 第一次

你可能感兴趣的:(tensorflow,tf.no_op)