Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count number of "True" values in boolean Tensor

I understand that tf.where will return the locations of True values, so that I could use the result's shape[0] to get the number of Trues.

However, when I try and use this, the dimension is unknown (which makes sense as it needs to be computed at runtime). So my question is, how can I access a dimension and use it in an operation like a sum?

For example:

myOtherTensor = tf.constant([[True, True], [False, True]])
myTensor = tf.where(myOtherTensor)
myTensor.get_shape() #=> [None, 2]
sum = 0
sum += myTensor.get_shape().as_list()[0] # Well defined at runtime but considered None until then.
like image 328
Aidan Gomez Avatar asked Jan 04 '16 19:01

Aidan Gomez


People also ask

How do you determine the number of true values?

To count the number of TRUE entries, which here is 5, the formula =COUNTIF(A1:A6,TRUE) applied to the column should work, but it always returns the result 1. On the other hand, the formula =COUNTIF(A1:A6,FALSE) works correctly on a similar column got by pulling down FALSE. COUNTIF() works properly.

How do you count true boolean values in Python?

Use count_nonzero() to count True elements in NumPy array In Python, False is equivalent to 0 , whereas True is equivalent to 1 i.e. a non-zero value.

How do you count the number of true values in an array?

To count the true values in an array: Use the filter() method to iterate over the array. Check if each value is equal to true and return the result.

How do you count Boolean values in Java?

The countTrue() method of Booleans Class in Guava Library is used to count the number of values that are true in the specified boolean values passed as the parameter. Parameters: This method accepts the boolean values among which the true values are to be count.


3 Answers

You can cast the values to floats and compute the sum on them: tf.reduce_sum(tf.cast(myOtherTensor, tf.float32))

Depending on your actual use case you can also compute sums per row/column if you specify the reduce dimensions of the call.

like image 192
Rafał Józefowicz Avatar answered Oct 23 '22 07:10

Rafał Józefowicz


I think this is the easiest way to do it:

In [38]: myOtherTensor = tf.constant([[True, True], [False, True]])

In [39]: if_true = tf.count_nonzero(myOtherTensor)

In [40]: sess.run(if_true)
Out[40]: 3
like image 25
Lerner Zhang Avatar answered Oct 23 '22 07:10

Lerner Zhang


Rafal's answer is almost certainly the simplest way to count the number of true elements in your tensor, but the other part of your question asked:

[H]ow can I access a dimension and use it in an operation like a sum?

To do this, you can use TensorFlow's shape-related operations, which act on the runtime value of the tensor. For example, tf.size(t) produces a scalar Tensor containing the number of elements in t, and tf.shape(t) produces a 1D Tensor containing the size of t in each dimension.

Using these operators, your program could also be written as:

myOtherTensor = tf.constant([[True, True], [False, True]])
myTensor = tf.where(myOtherTensor)
countTrue = tf.shape(myTensor)[0]  # Size of `myTensor` in the 0th dimension.

sess = tf.Session()
sum = sess.run(countTrue)
like image 8
mrry Avatar answered Oct 23 '22 07:10

mrry