Home > Blockchain >  How to check if a value inside a tensor is True
How to check if a value inside a tensor is True

Time:01-27

I have a tensor with some values, like [10, 10, 5]. I want to know if some of them are repeating among the tensor. So for that, I use this:

tf.unique_with_counts(
    tf.constant([10, 10, 5])
)[2] > 1

Which gives me the following values: True, False regarding of 10 is repeating and 5 not.

<tf.Tensor: shape=(2,), dtype=bool, numpy=array([ True, False])>

But how can, in a simplified way, check if within this array is there any True value? For now I have a loop but this isn't efficient.

CodePudding user response:

You can use tf.reduce_any, which performs a logical or:

import tensorflow as tf

y = tf.reduce_any(tf.unique_with_counts(
    tf.constant([10, 10, 5]))[2] > 1)
print(y)
# tf.Tensor(True, shape=(), dtype=bool)
  •  Tags:  
  • Related