如何计算张量中的正值?

How to count positive values in a tensor?

如何在TENSORFLOW中计算张量中正值的个数?

感谢您的帮助。

res = tf.count_nonzero(tf.greater_equal(x, 0.))
                       # or using tf.greater() if you want to exclude 0.

示例:

import tensorflow as tf

x = tf.random_uniform((10,), -2, 2)
res = res = tf.count_nonzero(tf.greater_equal(x, 0.))

with tf.Session() as sess:
    x_val, res_val = sess.run([x, res])
    print(x_val)
    # [-1.5928602 - 0.95617723  1.0444398   0.65296173  1.2118826 - 0.69604445 - 1.3821526   1.3174543 - 1.2171302 - 1.5908141]
    print(res_val)
    # 4