I am following the tutorial in this link and trying to change the evaluation method for the model (at the bottom). I would like to get a top-5 evaluation and I\'m trying to use
The targets
argument to tf.nn.in_top_k(predictions, targets, k) must be a vector of class IDs (i.e. indices of columns in the predictions
matrix). This means that it only works for single-class classification problems.
If your problem is a single-class problem, then I assume that your y_
tensor is a one-hot encoding of the true labels for your examples (for example because you also pass them to an op like tf.nn.softmax_cross_entropy_with_logits(). In that case, you have two options:
tf.nn.in_top_k()
without converting them to one-hot. (Also, consider using tf.nn.sparse_softmax_cross_entropy_with_logits() as your loss function, because it may be more efficient.)If the labels were originally stored in the one-hot format, you can convert them to integers using tf.argmax()
:
labels = tf.argmax(y_, 1)
topFiver = tf.nn.in_top_k(y, labels, 5)