函数 control_dependencies 有什么作用? [英] What does the function control_dependencies do?
问题描述
我想用一个例子来说明函数 tf.control_dependencies
.例如,我想创建两个张量 X
和 Y
,如果它们相等,则执行或打印某些内容.
I would like to have an example illustrating the use of the function tf.control_dependencies
. For example, I want to create two tensors X
and Y
and if they are equal do or print something.
import tensorflow as tf
session = tf.Session()
X = tf.constant(5)
Y = tf.constant(50)
with tf.control_dependencies([tf.assert_equal(X, Y)]):
print('X and Y are equal!')
在上面的代码中,X
显然不等于Y
.tf.control_dependencies
在这种情况下做什么?
In the code above, X
is clearly not equal to Y
. What is tf.control_dependencies
doing in this case?
推荐答案
control_dependencies
不是条件.它是一种向您在 with
块中创建的任何操作添加依赖项的机制.更具体地说,您在 control_dependencies
的参数中指定的内容确保在您在 with
块中定义的任何内容之前进行评估.
control_dependencies
is not a conditional. It is a mechanism to add dependencies to whatever ops you create in the with
block. More specifically, what you specify in the argument to control_dependencies
is ensured to be evaluated before anything you define in the with
block.
在您的示例中,您没有在 with
块中添加任何 (TensorFlow) 操作,因此该块什么都不做.
In your example, you don't add any (TensorFlow) operations in the with
block, so the block does nothing.
这个答案有一个关于如何使用 control_dependencies
的例子,它用于确保在评估批规范操作之前进行分配.
This answer has an example of how to use control_dependencies
, where it is used to make sure the assignments happen before the batchnorm operations are evaluated.
这篇关于函数 control_dependencies 有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!