43
votes

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.
4
What does myTensor look like? - erip
@erip I updated with a more explicit example. - Aidan Gomez
Is your expected output 2? - erip
@erip in the case above it would be three as there are 3 Trues - Aidan Gomez

4 Answers

54
votes

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.

19
votes

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
8
votes

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)
3
votes

There is a tensorflow function to count non-zero values tf.count_nonzero. The function also accepts an axis and keep_dims arguments.

Here is a simple example:

import numpy as np
import tensorflow as tf
a = tf.constant(np.random.random(100))
with tf.Session() as sess:
    print(sess.run(tf.count_nonzero(tf.greater(a, 0.5))))