所以通常在单标签分类中,我们使用如下
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))
这对于单标签分类很简单。
非常感谢