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 第一次總結
以上是生活随笔為你收集整理的what does tf.no_op do and tf.control_dependencies work?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: tf.nn.sparse_softmax
- 下一篇: tf.Variable()、tf.get