I'm trying to check if the tensorflow tensor rand_int=0 in an if statement. In the below code, the if statement is not executed when rand_int=0.
rand_int = tf.random.uniform((), 0, 2, dtype=tf.int32)
if rand_int == 0:
# Lots of lines of code
...
However, in the below code, the if statement is executed.
rand_int = tf.random.uniform((), 0, 2, dtype=tf.int32)
rand_int = tf.Session().run(rand_int)
if rand_int == 0:
...
How would I execute the if statement in the first block without tf.Session()?
rand_intis aTensortype object which will not equal0or1. That's probably why the contents of theif's true branch are not executed. In the second snippet,rand_intis redefined as the output ofsession.run()and will either be0or1. So, the contents of theif's true branch may execute with 50% probability.one = tf.constant(1, dtype=tf.int32)andzero = tf.constant(0, dtype=tf.int32)may be useful. Sinceoneandzeroare also tensors,is_zero = rand_int == zerowill also be a tensor. However, we may still not be able to executeif is_zeroas expected. We will need something liketf.condhere stackoverflow.com/questions/35833011/…