Python 计算布尔张量中“真”值的数量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34598371/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 15:14:48  来源:igfitidea点击:

Count number of "True" values in boolean Tensor

pythontensorflow

提问by Aidan Gomez

I understand that tf.wherewill return the locations of Truevalues, so that I could use the result's shape[0]to get the number of Trues.

我知道这tf.where将返回True值的位置,以便我可以使用结果shape[0]来获取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.

采纳答案by Rafa? Józefowicz

You can cast the values to floats and compute the sum on them: tf.reduce_sum(tf.cast(myOtherTensor, tf.float32))

您可以将值转换为浮点数并计算它们的总和: 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.

根据您的实际用例,如果您指定调用的减少维度,您还可以计算每行/列的总和。

回答by mrry

Rafal's answer is almost certainly the simplest way to count the number of trueelements in your tensor, but the other part of your question asked:

Rafal 的答案几乎可以肯定是计算true张量中元素数量的最简单方法,但问题的另一部分提出了:

[H]ow can I access a dimension and use it in an operation like a sum?

[H]如何访问维度并将其用于求和等运算?

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 Tensorcontaining the number of elements in t, and tf.shape(t)produces a 1D Tensorcontaining the size of tin each dimension.

为此,您可以使用 TensorFlow 的形状相关操作,它们作用于张量的运行时值。例如,tf.size(t)生成Tensor包含 中元素数量的标量t,并tf.shape(t)生成Tensor包含t每个维度中大小的1D 。

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)

回答by aboettcher

There is a tensorflow function to count non-zero values tf.count_nonzero. The function also accepts an axisand keep_dimsarguments.

有一个 tensorflow 函数来计算非零值tf.count_nonzero。该函数还接受 anaxiskeep_dims参数。

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

回答by Lerner Zhang

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