tf.control_dependencies(control_inputs)

  • 此函数指定某些操作执行的依赖关系

  • 返回一个控制依赖的上下文管理器,使用 with 关键字可以让在这个上下文环境中的操作都在 control_inputs 执行

# 在执行完 a,b 操作之后,才能执行 c,d 操作。意思就是 c,d 操作依赖 a,b 操作
with tf.control_dependencies([a, b]):
    c = ....
    d = ...
# tf.no_op()表示执行完 train_step, variable_averages_op 操作之后什么都不做
with tf.control_dependencies([train_step, variable_averages_op]):
    train_op = tf.no_op(name='train')
  • 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.no_op()什么都不做。 但是,当你在tf.control_dependencies([x,y,z]):block中创建一个tf.no_op()时,op将获得对ops x,y和z的控制依赖性。 因此,它可以用于将一组副作用ops组合在一起,并为您提供单个op传递给sess.run(),以便在一个步骤中运行所有这些操作。
  • 所以我们在块中编写no_op的原因是激活control_dependencies块以按指定的顺序运行ops x,y,z?
  • 这几乎是正确的:我们需要在control_dependencies块中创建一个op,以便添加这些依赖项。 然而,顺序稍微更微妙:(假设它们之间没有其他边缘)x,y和z可以相对于彼此以任何顺序运行; 而control_dependencies只是确保它们都发生在no_op之前。
    参考1
    参考2
    参考3
发布了59 篇原创文章 · 获赞 17 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/Miracle_520/article/details/93494228