简体   繁体   English

使用tensorflow进行多标签分类验证

[英]Multilabel classification validation using tensorflow

I have a multi-class classification problem where I would like to evaluate a correct prediction as if the models best prediction matches any of the labels in the image it's correct (True) otherwise incorrect (False). 我有一个多类分类问题,我想评估一个正确的预测,好像模型最佳预测匹配图像中的任何标签正确(True)否则不正确(False)。 As an example here is the line from the (expert) MNIST example I would like to change: 作为一个例子,这里是我想改变的(专家)MNIST示例中的行:

# returns list of True or False values if the prediction matches the label
correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(y_,1))

I am looking to change it to something like this: 我希望将其更改为以下内容:

correct = tf.constant(1.0, tf.float32)
correct_prediction = tf.equal(y_[tf.argmax(y_conv, 1)], correct)

Which would get the best prediction from the model and use that to access the column of the label and finally check the label is correct however slicing doesn't seem to work. 哪个会从模型中获得最佳预测,并使用它来访问标签的列,最后检查标签是否正确,但切片似乎不起作用。 Has anybody got any insight? 有没有人有任何见解?

Edit: added example 编辑:添加示例

Test using the following code: 使用以下代码进行测试:

import tensorflow as tf
NUM_CLASSES = 4
sess = tf.InteractiveSession()

# 2 output examples
y_conv = tf.constant([[0.1, 0.2, 0.8, 0.2],[0.9, 0.1, 0.3, 0.2]])

# 2 labels examples
y_ = tf.constant([[0., 0., 1., 1.], [0, 1., 1., 1.]])

Expecting: 期待:

correct_prediction = [ True, False ]

Thanks. 谢谢。

So there are several ways of doing this, I think this is one of the most simple solutions: 所以有几种方法可以做到这一点,我认为这是最简单的解决方案之一:

prediction = tf.one_hot(tf.argmax(y_conv, 1), NUM_CLASSES)
correct_prediction = tf.not_equal(tf.argmax(tf.mul(prediction, y_), 1), False)

Thanks to Kendall Weihe and Peter Hawkins comments 感谢Kendall Weihe和Peter Hawkins的评论

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM