Count number of “True” values in boolean Tensor

后端 未结 4 1393
粉色の甜心
粉色の甜心 2020-12-29 01:58

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 <

4条回答
  •  攒了一身酷
    2020-12-29 02:49

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

提交回复
热议问题