函数control_dependencies有什么作用?

27

我希望能够得到一个示例,演示如何使用函数tf.control_dependencies。例如,我想创建两个张量XY,如果它们相等,则执行或打印某些内容。

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是做什么的?

1个回答

50

control_dependencies并不是一个条件语句,而是一种机制,可以向您在with代码块中创建的所有操作添加依赖项。更具体地说,您在 control_dependencies 参数中指定的内容将确保在您定义的任何内容之前进行评估。

在您的示例中,在with代码块中未添加任何( TensorFlow) 操作,因此该代码块不执行任何操作。

这个答案提供了一个使用control_dependencies的示例,其中它用于确保分配在批量归一化操作之前发生。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接