如何计算一个热张量

How to count in a one hot tensor

我知道我可以使用此命令将 Tensor 转换为 one-hot:

one_hot_labels = tf.one_hot(labels,depth=3)

现在我想数一数class 0、class 1 和class 2 在one_hot_labels 中有多少个。最简单的计算方法是什么?

示例:

输入:

one_hot_labels = [[1,0,0],[1,0,0],[0,0,1]]
one_hot_labels.count([1,0,0]) # something like this command

输出:

2

像这样的东西应该适合你:

one_hot_labels = np.array([[1,0,0],[1,0,0],[0,0,1]])
count_label = tf.reduce_sum(one_hot_labels, axis=0)
sess = tf.Session()
sess.run(count_label)
# array([2, 0, 1])

现在例如你可以这样做:

count_label = tf.reduce_sum(one_hot_labels, axis=0)[0]
# 2