tf.control_dependencies
Stay organized with collections Save and categorize content based on your preferences.
Wrapper for Graph.control_dependencies()
using the default graph.
tf.control_dependencies( control_inputs ) -> Graph._ControlDependenciesController
Used in the notebooks
See tf.Graph.control_dependencies
for more details.
In TensorFlow 2 with eager and/or Autograph, you should not need this method most of the times, as ops execute in the expected order thanks to automatic control dependencies. Only use it to manually control ordering, for example as a workaround to known issues such as tf.function
with tf.debugging.assert*
and tf.py_function
. For example:
@tf.function(
input_signature=[tf.TensorSpec([None, None], tf.float32),
tf.TensorSpec([None, None], tf.float32)])
def my_assert_func_1(x, bias):
# `tf.function` attempts to execute `tf.math.add` in parallel to
# `assert_equal`. As a result an error can get raised from `tf.math.add`
# without triggering the assertion error.
tf.assert_equal(tf.shape(x)[1],
tf.shape(bias)[1],
message='bad shape')
return x + bias
# Error raised in either `add` or `assert`
my_assert_func_1(tf.ones((2, 5)), tf.ones((2, 7)))
Traceback (most recent call last):
InvalidArgumentError: ...
@tf.function(
input_signature=[tf.TensorSpec([None, None], tf.float32),
tf.TensorSpec([None, None], tf.float32)])
def my_assert_func_2(x, bias):
with tf.control_dependencies(
[tf.assert_equal(tf.shape(x)[1],
tf.shape(bias)[1],
message='bad shape')]):
return x + bias
# Error raised in `assert`
my_assert_func_2(tf.ones((2, 5)), tf.ones((2, 7)))
Traceback (most recent call last):
InvalidArgumentError: ...
When eager execution is enabled, any callable object in the control_inputs
list will be called.
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. If eager execution is enabled, any callable object in the control_inputs list will be called. |
Returns |
A context manager that specifies control dependencies for all operations constructed within the context. |
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates. Some content is licensed under the numpy license.
Last updated 2024-04-26 UTC.
[null,null,["Last updated 2024-04-26 UTC."],[],[],null,["# tf.control_dependencies\n\n\u003cbr /\u003e\n\n|-------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/framework/ops.py#L4510-L4582) |\n\nWrapper for [`Graph.control_dependencies()`](../tf/Graph#control_dependencies) using the default graph.\n\n#### View aliases\n\n\n**Compat aliases for migration**\n\nSee\n[Migration guide](https://www.tensorflow.org/guide/migrate) for\nmore details.\n\n[`tf.compat.v1.control_dependencies`](https://www.tensorflow.org/api_docs/python/tf/control_dependencies)\n\n\u003cbr /\u003e\n\n tf.control_dependencies(\n control_inputs\n ) -\u003e Graph._ControlDependenciesController\n\n### Used in the notebooks\n\n| Used in the tutorials |\n|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| - [Bayesian Modeling with Joint Distribution](https://www.tensorflow.org/probability/examples/Modeling_with_JointDistribution) - [TensorFlow Probability Case Study: Covariance Estimation](https://www.tensorflow.org/probability/examples/TensorFlow_Probability_Case_Study_Covariance_Estimation) |\n\nSee [`tf.Graph.control_dependencies`](../tf/Graph#control_dependencies) for more details.\n\nIn TensorFlow 2 with eager and/or Autograph, you should not need this method\nmost of the times, as ops execute in the expected order thanks to automatic\ncontrol dependencies. Only use it to manually control ordering, for example as\na workaround to known issues such as [`tf.function`](../tf/function) with `tf.debugging.assert*`\nand [`tf.py_function`](../tf/py_function).\nFor example: \n\n @tf.function(\n input_signature=[tf.TensorSpec([None, None], tf.float32),\n tf.TensorSpec([None, None], tf.float32)])\n def my_assert_func_1(x, bias):\n # `tf.function` attempts to execute `tf.math.add` in parallel to\n # `assert_equal`. As a result an error can get raised from `tf.math.add`\n # without triggering the assertion error.\n tf.assert_equal(tf.shape(x)[1],\n tf.shape(bias)[1],\n message='bad shape')\n return x + bias\n\n # Error raised in either `add` or `assert`\n my_assert_func_1(tf.ones((2, 5)), tf.ones((2, 7)))\n Traceback (most recent call last):\n\n InvalidArgumentError: ...\n\n @tf.function(\n input_signature=[tf.TensorSpec([None, None], tf.float32),\n tf.TensorSpec([None, None], tf.float32)])\n def my_assert_func_2(x, bias):\n with tf.control_dependencies(\n [tf.assert_equal(tf.shape(x)[1],\n tf.shape(bias)[1],\n message='bad shape')]):\n return x + bias\n\n # Error raised in `assert`\n my_assert_func_2(tf.ones((2, 5)), tf.ones((2, 7)))\n Traceback (most recent call last):\n\n InvalidArgumentError: ...\n\nWhen eager execution is enabled, any callable object in the `control_inputs`\nlist will be called.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `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. If eager execution is enabled, any callable object in the `control_inputs` list will be called. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| A context manager that specifies control dependencies for all operations constructed within the context. ||\n\n\u003cbr /\u003e"]]