简体   繁体   中英

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). As an example here is the line from the (expert) MNIST example I would like to change:

# 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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