27

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!')

In the code above, X is clearly not equal to Y. What is tf.control_dependencies doing in this case?

1 Answer 1

50

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.

In your example, you don't add any (TensorFlow) operations in the with block, so the block does nothing.

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.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.