Skip to content

Argmax, fetching top 5 probabilities #319

Closed

Description

@RamaneekGill

I'm having an issue with computing the accuracy of my model. I want to see if the top 5 predictions contains the actual label when comparing against the test set.

correct_prediction = tf.equal(tf.argmax(activation, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
print ("ACCURACY IS:")
print sess.run(accuracy, feed_dict={x: data.test_set.inputs(), y: data.test_set.targets()})

From what I've read in the documentation TensorFlow doesn't seem to have something like fetching the top 5 from activation and seeing if one of the items is contained in argmax(y,1)

Something like this would be nice:

correct_prediction = tf.contains(tf.argmax(activation, dim, N_VALUES_TO_FETCH), tf.argmax(y, dim)

Can anyone help me with the problem I am facing? Thank you.

Activity

  1. kofd commented on Nov 21, 2015

    @kofd

    Check out tf.nn.top_k(input, k, name=None) and tf.nn.in_top_k(predictions, targets, k, name=None).

  2. vrv commented on Nov 24, 2015

    @vrv

    (let us know if those aren't sufficient)

  3. neopostmodern commented on Dec 1, 2015

    @neopostmodern

    I would suggest linking to the tf.nn.top_k function in the documentation right after argmin and argmax, potentially under the name of argsort (which is what Numpy users would search for).

  4. Summer12333 commented on Jul 19, 2018

    @Summer12333

    I've just met this question and I've found one function that is exactly what I want.

    tf.nn.in_top_k(predictions, targets, k, name=None)
    Says whether the targets are in the top K predictions.
    This outputs a batch_size bool array, an entry out[i] is true if the prediction for the target class is among the top k predictions among all predictions for example i. Note that the behavior of InTopK differs from the TopK op in its handling of ties; if multiple classes have the same prediction value and straddle the top-k boundary, all of those classes are considered to be in the top k.

  5. added a commit that references this issue on May 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions