Top-K Accuracy Computation
Implement top-k accuracy metric used for evaluating classification models.
Top-k accuracy checks if the correct class is among the k highest-probability predictions:
Top-k Acc=N1​∑i=1N​1[yi​∈topk​(y^​i​)]
For ImageNet, top-5 accuracy is standard (correct class in top 5 predictions).
Implementation:
- For each sample, get indices of k largest logits (argsort descending)
- Check if true label is in these k indices
- Average across all samples
Example:
logits = [[0.1, 0.2, 0.7], [0.8, 0.1, 0.1]] # 2 samples, 3 classes labels = [2, 0] # True classes k = 2
1.0
Sample 1: Top-2 predictions are classes [2, 1], true label 2 ✓ Sample 2: Top-2 predictions are classes [0, 1], true label 0 ✓
Both correct → 2/2 = 1.0 accuracy
Constraints:
- logits: Tensor (batch_size, num_classes) - raw model outputs
- labels: Tensor (batch_size,) - ground truth class indices
- k: Number of top predictions to consider
- Return: Top-k accuracy as float (0-1)
Top-K Accuracy Computation: Background & Strategy
Background Knowledge
Top-k accuracy is a relaxed evaluation metric for multi-class classification that measures whether the correct class appears among the model's k highest-confidence predictions. Unlike top-1 accuracy (which requires the highest-probability prediction to be correct), top-k accuracy allows the model k attempts to identify the true class. This metric is particularly valuable in scenarios with class ambiguity or large label spaces—ImageNet's 1,000 classes make top-5 accuracy a standard benchmark because distinguishing between similar object categories is inherently difficult.
The mathematical formulation uses an indicator function: for each sample i, you check if the true label yi​ belongs to the set of k classes with the highest predicted probabilities. The metric then averages these binary correctness indicators across all N samples. This approach naturally handles the trade-off between model confidence and practical utility: a model might not confidently predict the exact correct class, but placing it in the top-k predictions is often sufficient for downstream applications.
Key insight: Top-k accuracy is fundamentally about ranking. Rather than comparing absolute probability values, you're checking whether the correct class ranks within the top k positions when all classes are sorted by their predicted scores (logits or probabilities). This ranking perspective is crucial for efficient implementation.
Algorithm/Approach
The solution follows a straightforward three-step pattern:
- Rank the predictions: For each sample, identify which k classes have the highest predicted scores
- Check membership: Determine if the true label appears in this top-k set
- Aggregate: Compute the proportion of samples where the check succeeded
The key algorithmic operation is partial sorting or argsort: you need the indices of the k largest values, not necessarily all values sorted. This distinction matters for efficiency when k is small relative to the total number of classes.
Step-by-Step Strategy
Step 1: Understand the input shapes
- Predictions: shape [N,C] where N is batch size and C is number of classes (logits or probabilities)
- True labels: shape [N] containing class indices (0 to C-1)
- k: integer specifying how many top predictions to consider
Continue the full explanation
You're reading the free preview. Unlock the complete walkthrough, the code editor, test runner and reference solution with Premium.
Editor locked
The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.