8

Trying to find a similar operation to .any(), .all() methods that will work on a tensor. Here is a scenario:

a = tf.Variable([True, False, True], dtype=tf.bool)

# this is how I do it right now
has_true = a.reduce_sum(tf.cast(a, tf.float32)) 
# this is what I'm looking for
has_true = a.any()

Currently converting my boolean tensor into int using reduce_sum to see if there are any truths in it. Is there a cleaner way to perform this operation?

nikolaevra
  • 369
  • 2
  • 9

1 Answers1

9

There are tf.reduce_any and tf.reduce_all methods:

sess = tf.Session()

a = tf.Variable([True, False, True], dtype=tf.bool)
sess.run(tf.global_variables_initializer())

sess.run(tf.reduce_any(a))
# True
sess.run(tf.reduce_all(a))
# False
Psidom
  • 209,562
  • 33
  • 339
  • 356