Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to calculate the average of elements above a certain threshold in tensorflow

I want to calculate the mean of the elements that are greater than a fixed threshold (here 0.8).

Task in numpy :

X = np.array([[0.11,0.99,0.70]])
print(np.nanmean(X[X>0.8]))
Out : 0.99

What is the equivalent in tensorflow without converting the tensor c to a numpy array?

Example:

c = tf.constant([[0.11,0.99,0.70]])
tf.reduce_mean(tf.where(tf.greater(c,(tf.constant(0.8, dtype=tf.float32)))))

the output is equal to 0!

Output : <tf.Tensor: shape=(), dtype=int64, numpy=0>
like image 389
KKK07 Avatar asked Dec 20 '25 19:12

KKK07


2 Answers

You don't need tf.greater and tf.where.

c = tf.constant([[0.11,0.99,0.70]])
# Or : tf.reduce_mean(c[c > 0.8])
tf.reduce_mean(c[c > tf.constant(0.8, dtype=tf.float32)])

You can use tf.boolean_mask as an alternative:

c = tf.constant([[0.11,0.99,0.70]])
mask = c > 0.8
tf.reduce_mean(tf.boolean_mask(tensor, mask))

Output : <tf.Tensor: shape=(), dtype=float32, numpy=0.99>

like image 159
I'mahdi Avatar answered Dec 24 '25 10:12

I'mahdi


You could also use a boolean_mask instead of tensor indexing:

c = tf.constant([[0.11,0.99,0.70]])
tf.reduce_mean(tf.boolean_mask(c, c > 0.8))
<tf.Tensor: shape=(), dtype=float32, numpy=0.99>
like image 35
AloneTogether Avatar answered Dec 24 '25 11:12

AloneTogether



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!