Safely check if tf.Tensor satisfies some condition

Viewed 138

If I want to check that all the elements in tnsr = tf.constant(...) are greater than 3, I can tf.reduce_all(tnsr > 3) to get a scalar boolean tensor. If I'm using eager execution, or @tf.function, I can then use that as a normal bool:

@tf.function
def foo(tnsr):
    if tf.reduce_all(tnsr > 3):
        ...

but this doesn't work with autograph=False. What do I do then?

Other things I've tried:

  • tf.cond returns a tf.Tensor even for true_fn=lambda: True
  • bool(tf.reduce_all(tnsr > 3)) fails for the same reason as above
0 Answers
Related