如何计算张量中的正值?

时间:2018-05-25 09:28:22

标签: python tensorflow count tensor

我的问题在标题中。

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

感谢您的帮助。

1 个答案:

答案 0 :(得分:3)

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