我知道 tf.where
将返回 True
值的位置,以便我可以使用结果的 shape[0]
来获取 True
的数量。
但是,当我尝试使用它时,维度是未知的(这是有道理的,因为它需要在运行时计算)。所以我的问题是,我如何访问一个维度并将其用于求和之类的操作中?
例如:
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.
最佳答案
您可以将值转换为 float 并计算它们的总和:
tf.reduce_sum(tf.cast(myOtherTensor, tf.float32))
根据您的实际用例,如果您指定调用的缩减维度,您还可以计算每行/列的总和。
关于python - bool 张量中 "True"值的计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34598371/