tf.control_dependencies和tf.Graph.control_dependencies

简介:
用来控制计算流图的,给图中的某些计算指定顺序
control_dependencies(control_inputs)
    Wrapper (封装)for `Graph.control_dependencies()` using the default graph.
    
    See @{tf.Graph.control_dependencies}
    for more details.
    
    Args:
      control_inputs: A list of `Operation` or `Tensor` objects which
        must be executed or computed before running the operations
        defined in the context.  Can also be `None` to clear the control
        dependencies.
    
    Returns:
     A context manager that specifies control dependencies for all
     operations constructed within the context.


control_dependencies(self, control_inputs)
    Returns a context manager that specifies control dependencies.
    
    Use with the `with` keyword to specify that all operations constructed
    within the context should have control dependencies on
    `control_inputs`. For example:
    
    ```python
    with g.control_dependencies([a, b, c]):
      # `d` and `e` will only run after `a`, `b`, and `c` have executed.
      d = ...
      e = ...
    ```
    
    Multiple calls to `control_dependencies()` can be nested, and in
    that case a new `Operation` will have control dependencies on the union
    of `control_inputs` from all active contexts.
    
    ```python
    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`.
    ```
    
    You can pass None to clear the control dependencies:
    
    ```python
    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`.
    ```
    
    *N.B.* The control dependencies context applies *only* to ops that
    are constructed within the context. Merely using an op or tensor
    in the context does not add a control dependency. The following
    example illustrates this point:
    
    ```python
    # 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)
    ```
    
    Args:
      control_inputs: A list of `Operation` or `Tensor` objects which
        must be executed or computed before running the operations
        defined in the context.  Can also be `None` to clear the control
        dependencies.
    
    Returns:
     A context manager that specifies control dependencies for all
     operations constructed within the context.
    
    Raises:
      TypeError: If `control_inputs` is not a list of `Operation` or
        `Tensor` objects.
目录
相关文章
|
2月前
|
TensorFlow 算法框架/工具
【Tensorflow】解决A `Concatenate` layer should be called on a list of at least 2 inputs
在TensorFlow 2.0中,使用Concatenate函数时出现错误,可以通过替换为tf.concat 来解决。
24 4
|
算法框架/工具
成功解决INFO: pip is looking at multiple versions of keras-preprocessing to determine which version is c
成功解决INFO: pip is looking at multiple versions of keras-preprocessing to determine which version is c
|
API 数据格式
TensorFlow2._:model.summary() Output Shape为multiple解决方法
TensorFlow2._:model.summary() Output Shape为multiple解决方法
255 0
TensorFlow2._:model.summary() Output Shape为multiple解决方法
|
机器学习/深度学习 算法
【RLchina第五讲】Control as Inference(下)
【RLchina第五讲】Control as Inference(下)
|
机器学习/深度学习
【RLchina第五讲】Control as Inference(上)
【RLchina第五讲】Control as Inference(上)
|
数据格式
ValueError: This model has not yet been built. Build the model first by calling `build()` or calling
ValueError: This model has not yet been built. Build the model first by calling `build()` or calling
191 0
ValueError: This model has not yet been built. Build the model first by calling `build()` or calling
|
并行计算
ERROR: cuda requested, but not all dependencies are satisfied: ffnvcodec
ERROR: cuda requested, but not all dependencies are satisfied: ffnvcodec
182 0
【解决方案】The opset version of the onnx model is 12, only model with opset_version 10/11 is supported.
【解决方案】The opset version of the onnx model is 12, only model with opset_version 10/11 is supported.
695 0
|
计算机视觉
成功解决sklearn\cross_validation.py:41: DeprecationWarning: This module was deprecated in version 0.18 i
成功解决sklearn\cross_validation.py:41: DeprecationWarning: This module was deprecated in version 0.18 i