Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the function control_dependencies do?

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?

like image 840
stevGates Avatar asked Feb 07 '17 16:02

stevGates


1 Answers

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.

like image 86
etarion Avatar answered Oct 04 '22 01:10

etarion