Skip to content
DeepForgeby svx
All problems
EasyDeep Learningdl-061

Top-3 Accuracy

Problem

Compute top-3 accuracy: the fraction of rows whose target index is among the three largest logits. Break ties in favor of the smaller index.

The signature is top3_accuracy(logits, targets). logits is a 2D matrix and targets has one id per row.

Hint

Sort indices by descending logit with index as the deterministic tie-breaker, then keep the first three.

Test cases

case 1
in: [[[1,2,3],[3,2,1]],[2,0]]
expected: 1
case 2
in: [[[1,2,3,4],[4,3,2,1]],[0,0]]
expected: 0.5
case 3
in: [[[0,0,0]],[2]]
expected: 1
case 4
in: [[[5]],[0]]
expected: 1
Starter codePython
def top3_accuracy(logits, targets):
    # Your code here
    pass

4 test cases · run your code against them in the editor