tf.control_dependencies
Обёртка для Graph.control_dependencies(), использующая граф по умолчанию.
tf.control_dependencies(
control_inputs
) -> Graph._ControlDependenciesController
Используется в блокнотах
| Используется в учебных материалах |
|---|
Подробнее см. в разделе tf.Graph.control_dependencies.
В TensorFlow 2 с eager execution и/или Autograph этот метод обычно не требуется, поскольку операции выполняются в ожидаемом порядке благодаря автоматическим управляющим зависимостям. Используйте его только для ручного управления порядком выполнения, например, в качестве обходного решения известных проблем, таких как tf.function с tf.debugging.assert* и tf.py_function. Например:
@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: ...
Если включено eager execution, будет вызван любой вызываемый объект в списке control_inputs.
| Аргументы | |
|---|---|
control_inputs | Список объектов типа Operation или Tensor, которые должны быть выполнены или вычислены перед запуском операций, определённых в контексте. Также может иметь значение None для сброса управляющих зависимостей. Если включено eager execution, будет вызван любой вызываемый объект в списке control_inputs. |
| Возвращает | |
|---|---|
| Менеджер контекста, задающий управляющие зависимости для всех операций, созданных внутри контекста. |
© 2022 The TensorFlow Authors. All rights reserved.
Licensed under the Creative Commons Attribution License 4.0.
Code samples licensed under the Apache 2.0 License.
https://www.tensorflow.org/api_docs/python/tf/control_dependencies