Following the discussion on TensorFlow 2.0 AutoGraphs, I've been playing around and noticed that inequality comparisons such as > and < are specified directly, whereas equality comparisons are represented using tf.equal.
Here's an example to demonstrate. This function uses > operator and works well when called:
@tf.function
def greater_than_zero(value):
return value > 0
greater_than_zero(tf.constant(1))
# <tf.Tensor: id=1377, shape=(), dtype=bool, numpy=True>
greater_than_zero(tf.constant(-1))
# <tf.Tensor: id=1380, shape=(), dtype=bool, numpy=False>
Here is another function that uses equality comparison, but does not work:
@tf.function
def equal_to_zero(value):
return value == 0
equal_to_zero(tf.constant(1))
# <tf.Tensor: id=1389, shape=(), dtype=bool, numpy=False> # OK...
equal_to_zero(tf.constant(0))
# <tf.Tensor: id=1392, shape=(), dtype=bool, numpy=False> # WHAT?
If I change the == equality comparison to tf.equal, it will work.
@tf.function
def equal_to_zero2(value):
return tf.equal(value, 0)
equal_to_zero2(tf.constant(0))
# <tf.Tensor: id=1402, shape=(), dtype=bool, numpy=True>
My question is: Why does using inequality comparison operators work inside tf.function functions, whereas equality comparisons do not?