MCPcopy Index your code
hub / github.com/geekcomputers/Python / top_k_accuracy

Function top_k_accuracy

ML/src/python/neuralforge/utils/metrics.py:161–168  ·  view source on GitHub ↗
(output, target, k=5)

Source from the content-addressed store, hash-verified

159 return 100.0 * correct / total if total > 0 else 0.0
160
161def top_k_accuracy(output, target, k=5):
162 with torch.no_grad():
163 maxk = min(k, output.size(1))
164 _, pred = output.topk(maxk, 1, True, True)
165 pred = pred.t()
166 correct = pred.eq(target.view(1, -1).expand_as(pred))
167 correct_k = correct[:k].reshape(-1).float().sum(0, keepdim=True)
168 return correct_k.mul_(100.0 / target.size(0)).item()

Callers

nothing calls this directly

Calls 1

sizeMethod · 0.80

Tested by

no test coverage detected