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 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.
原文由 Aidan Gomez 发布,翻译遵循 CC BY-SA 4.0 许可协议
您可以将值转换为浮点数并计算它们的总和:
tf.reduce_sum(tf.cast(myOtherTensor, tf.float32))
根据您的实际用例,如果您指定调用的缩减维度,您还可以计算每行/列的总和。