Skip to content
AI360Xpert
Beta

Top-K Accuracy

Top-k accuracy counts a prediction right when the true label lands anywhere in the model's k highest scores, forgiving confusion between near-duplicate classes.

Husky scoring second loses top-1 but wins top-2 and top-5, showing why the cutoff k decides what the metric forgives.
Husky scoring second loses top-1 but wins top-2 and top-5, showing why the cutoff k decides what the metric forgives.

Why Does This Exist?

ImageNet holds 1,000 classes including Siberian husky, Eskimo dog and malamute: three labels for what most people call the same dog. A model ranking the true label second is not wrong in any useful sense, yet top-1 accuracy marks it wrong. Top-k accuracy fixes the metric: the prediction counts as right when the true label appears anywhere in the top kk scores. AlexNet's 2012 breakthrough is quoted in top-5 error, 16% against the previous 26%, precisely because the benchmark designers knew exact-first-place was too strict for a thousand fine-grained breeds and objects.

This page covers the top-k rule, a hand-scored five-class example, and when each kk fits. Box-scoring overlap lives on the mean average precision page.

Think of It Like This

A quiz that accepts five guesses

A quizmaster asking for a dog breed could demand one answer and fail everyone who says malamute instead of husky. Or they could accept any of your top five guesses, rewarding real knowledge while still failing a guess of cat. Top-5 is that generous quizmaster: it forgives confusion inside a lookalike family and still punishes genuine ignorance.

The analogy stops at deployment. A quiz forgives for fun, but a factory gate acting on the single winner cannot accept five guesses, so production must be judged by the kk it consumes.

How It Actually Works

The rule and the notation

For each image the model outputs one score per class. Sort all KK scores descending and check whether the true label yy sits in the first kk positions. Over NN images:

top-k=1N∑i=1N1{yi∈topk(f(xi))}\text{top-}k = \frac{1}{N}\sum_{i=1}^{N} \mathbf{1}\{y_i \in \text{top}_k(f(x_i))\}

where 1{⋅}\mathbf{1}\{\cdot\} is 1 when the condition holds and 0 otherwise. Top-1 is the special case k=1k = 1: ordinary accuracy.

Worked five-class example

Scores read dog 0.50, husky 0.30, cat 0.10, bird 0.06, car 0.04, with true label husky. Sorted, the ranking is dog, husky, cat, bird, car. Top-1 says wrong, since dog won. Top-2 says right, since husky sits second. Top-5 says right trivially, since every class is inside five. One ranking, three verdicts: the cutoff kk decides what counts as confusion versus failure.

When each k fits

Top-1 rules deployments with one action per prediction: a factory gate opens or it does not. Top-5 fits benchmarks and retrieval-style uses where a shortlist reaches a human or a second stage. Report both when classes overlap, because the gap between them diagnoses the error: 78% top-1 beside 94% top-5 says the model knows the right neighbourhood and needs part-level help, not new data. As a sanity anchor, random guessing scores k/Kk / K: top-5 over 1,000 classes is 0.5%, while top-5 over 10 classes is 50%, which is why kk means nothing without the class count beside it.

Code

scores = {"dog": 0.50, "husky": 0.30, "cat": 0.10, "bird": 0.06, "car": 0.04}truth = "husky"ranked = sorted(scores, key=scores.get, reverse=True)
for k in (1, 2, 5):    print(k, truth in ranked[:k])# -> 1 False# -> 2 True# -> 5 True

Watch Out For

Top-5 hides a broken top-1 in production

A model with 95% top-5 and 70% top-1 looks superb on a slide and fails at a gate that acts on the single winner. The symptom is benchmark pride beside deployment complaints. Always evaluate the kk your system actually consumes, usually 1.

Comparing top-k across different class counts

Top-5 over 1,000 classes is far stricter than top-5 over 10, where random guessing already scores 50%. The symptom is meaningless cross-dataset bragging. Quote kk with the class count, and compare top-1 to top-1.

The Quick Version

  • Top-k accuracy counts a prediction right when the true label ranks in the top kk scores.
  • Top-5 forgives near-duplicate-class confusion on benchmarks like ImageNet.
  • Dog 0.50 beats husky 0.30, so top-1 fails where top-2 passes.
  • The top-1 to top-5 gap diagnoses fine-grained confusion versus genuine failure.
  • Deployments acting on one label must be judged by top-1, not top-5.