【发布时间】:2018-03-30 23:10:10
【问题描述】:
所以通常在单标签分类中,我们使用如下
correct_preds = tf.equal(tf.argmax(preds, 1), tf.argmax(self.label, 1))
但我正在处理多标签分类,所以我想知道如何在标签向量中有多个标签的情况下做到这一点。所以到目前为止我所拥有的如下
a = tf.constant([0.2, 0.4, 0.3, 0.1])
b = tf.constant([0,1.0,1,0])
empty_tensor = tf.zeros([0])
for index in range(b.get_shape()[0]):
empty_tensor = tf.cond(tf.equal(b[index],tf.constant(1, dtype =
tf.float32)), lambda: tf.concat([empty_tensor,tf.constant([index],
dtype = tf.float32)], axis = 0), lambda: empty_tensor)
temp, _ = tf.setdiff1d([tf.argmax(a)], tf.cast(empty_tensor, dtype= tf.int64))
output, _ = tf.setdiff1d([tf.argmax(a)], tf.cast(temp, dtype = tf.int64))
所以这给了我 max(preds) 发生的索引,以及 self.label 中 1 的位置。在上面的例子中,它给出 [1],如果 argmax 不匹配,那么我得到 []。
我遇到的问题是我不知道如何从那里继续,因为我想要以下内容
correct_preds = tf.equal(tf.argmax(preds, 1), tf.argmax(self.label, 1))
self.accuracy = tf.reduce_sum(tf.cast(correct_preds, tf.float32))
这对于单标签分类很简单。
非常感谢
【问题讨论】:
-
我的问题更多是关于如何编码预测和真实标签的对应计数(即索引相同的位置)。
-
我不会将此称为完全重复,因为该问题专门针对 Tensorflow。但是,可以将问题标题更改为“Tensorflow 中的多标签分类和准确性”,以便更通用,从而更容易被其他人找到。
标签: tensorflow indices tensor argmax