tamarintech
tamarintech

Reputation: 1992

TensorFlow 1.8.0, eager execution and comparison don't behave as expected

In the TensorFlow programmer guide the Flow Control/FizzBuzz example shows:

num = tf.constant(num)
if num % 3 == 0 and num % 5 == 0:

However, that doesn't work for me.

fiver % 5 == 0
False

The only way I have gotten this to work successfully is by using:

(num % 5).numpy() == 0

Are python comparisons supposed to work with the EagerTensor type? tf.equal() works, of course, but the example shows direct comparisons like == 0.

Upvotes: 2

Views: 194

Answers (1)

P-Gn
P-Gn

Reputation: 24651

This sounds like a bug in the documentation. If you look at the source of the equality operator of the Tensor object,

def __eq__(self, other):
    # Necessary to support Python's collection membership operators
    return id(self) == id(other)

So my_boolean_tensor==True (or False) will always return False because a tensor object is not the True or False object.

If I understand the comment in that operator correctly, this behavior is not likely to change.

Upvotes: 3

Related Questions