Top-K accuracy helps us check if the right answer is among the top K guesses of a model, not just the very top guess.
Top-K accuracy in Computer Vision
top_k_accuracy_score(y_true, y_pred_probs, k=5) # y_true: true labels # y_pred_probs: predicted probabilities for each class # k: number of top guesses to consider
The function checks if the true label is in the top K predicted classes.
Higher K means easier to get correct, but less strict accuracy.
top_k_accuracy_score([2, 0, 1], [[0.1, 0.2, 0.7], [0.8, 0.1, 0.1], [0.2, 0.6, 0.2]], k=1)
top_k_accuracy_score([2, 0, 1], [[0.1, 0.2, 0.7], [0.8, 0.1, 0.1], [0.2, 0.6, 0.2]], k=2)
This program shows how often the model's top guess is correct (top-1) and how often the correct label is in the top 2 guesses (top-2).
from sklearn.metrics import top_k_accuracy_score # True labels for 4 images y_true = [0, 1, 2, 3] # Predicted probabilities for 4 classes # Each inner list sums to 1 and shows model confidence y_pred_probs = [ [0.7, 0.1, 0.1, 0.1], # Correct class 0 is top guess [0.2, 0.3, 0.4, 0.1], # Correct class 1 is second guess [0.1, 0.2, 0.5, 0.2], # Correct class 2 is top guess [0.1, 0.1, 0.2, 0.6] # Correct class 3 is top guess ] # Calculate top-1 accuracy (exact match) top1 = top_k_accuracy_score(y_true, y_pred_probs, k=1) # Calculate top-2 accuracy (correct label in top 2 guesses) top2 = top_k_accuracy_score(y_true, y_pred_probs, k=2) print(f"Top-1 accuracy: {top1:.2f}") print(f"Top-2 accuracy: {top2:.2f}")
Top-K accuracy is useful when multiple guesses are acceptable.
Choosing K depends on your task and how many guesses you want to consider.
Top-K accuracy is common in image recognition challenges with many classes.
Top-K accuracy checks if the true label is within the top K model guesses.
It helps measure model performance beyond just the single best guess.
Use it when multiple possible answers are useful or when classes are many.
Practice
Top-K accuracy measure in a classification model?Solution
Step 1: Understand the definition of Top-K accuracy
Top-K accuracy checks if the correct label is within the top K guesses made by the model, not just the top 1.Step 2: Compare options with the definition
Only If the true label is among the top K predicted labels correctly states that Top-K accuracy measures if the true label is in the top K predictions.Final Answer:
If the true label is among the top K predicted labels -> Option AQuick Check:
Top-K accuracy = True label in top K predictions [OK]
- Confusing Top-K accuracy with single-label accuracy
- Thinking it measures prediction speed
- Assuming it counts total classes
topk method on model outputs outputs and true labels labels?Solution
Step 1: Understand PyTorch topk usage
Thetopk(k, dim=1)returns top k values and indices along dimension 1 (classes). We want indices for predictions.Step 2: Match predictions with labels
Reshape labels to (-1,1) to compare with top-k predictions and count matches witheqandsum.Final Answer:
_, pred = outputs.topk(3, dim=1); correct = pred.eq(labels.view(-1,1)).sum().item() -> Option CQuick Check:
Use topk with dim=1 and compare with labels reshaped [OK]
- Using max instead of topk for multiple predictions
- Wrong dimension in topk call
- Not reshaping labels for comparison
import torch
outputs = torch.tensor([[0.1, 0.8, 0.05, 0.05],
[0.4, 0.3, 0.2, 0.1],
[0.25, 0.25, 0.25, 0.25]])
labels = torch.tensor([1, 2, 3])
_, pred = outputs.topk(2, dim=1)
correct = pred.eq(labels.view(-1,1)).sum().item()
print(correct)Solution
Step 1: Identify top 2 predictions per sample
For each row: - Row 1: top 2 indices are [1, 0] (0.8, 0.1) - Row 2: top 2 indices are [0, 1] (0.4, 0.3) - Row 3: top 2 indices are [0, 1] (both 0.25, tie broken by index)Step 2: Check if true label is in top 2 predictions
Labels are [1, 2, 3]: - Sample 1: label 1 in [1,0] -> yes - Sample 2: label 2 in [0,1] -> no - Sample 3: label 3 in [0,1] -> noFinal Answer:
1 -> Option BQuick Check:
Only one label in top 2 predictions [OK]
- Assuming all labels are in top 2
- Ignoring tie-breaking in topk
- Not reshaping labels for comparison
_, pred = outputs.topk(5) correct = pred.eq(labels).sum().item()
Solution
Step 1: Check topk usage without dimension
Callingtopk(5)withoutdimdefaults to dim=0, which is incorrect for class predictions along dim=1.Step 2: Understand effect on predictions and comparison
Wrong dimension means predicted indices do not align with labels, sopred.eq(labels)never matches, resulting in zero correct.Final Answer:
Missing dimension argument in topk causes wrong axis selection -> Option AQuick Check:
Always specify dim=1 for class predictions in topk [OK]
- Forgetting dim argument in topk
- Converting labels unnecessarily
- Misunderstanding sum().item() output
Solution
Step 1: Understand correct usage of topk for Top-K accuracy
Top-5 accuracy requirestopk(5, dim=1)to get top 5 class indices per sample. Labels must be reshaped to compare with these indices.Step 2: Compute Top-1 accuracy separately
Top-1 accuracy is checking if label equals the top prediction (max or topk with k=1). This is done by comparing labels with top prediction indices.Final Answer:
Use topk(5, dim=1) on model outputs, compare with labels reshaped, then compute Top-1 by checking if label equals top prediction -> Option DQuick Check:
Top-K needs topk(dim=1) and label reshape; Top-1 is top prediction check [OK]
- Using wrong dimension in topk
- Mixing up Top-1 and Top-5 calls
- Not reshaping labels for comparison
- Using loops instead of vectorized operations
Ready to prove it?
Pass the test and this pattern moves unknown
