Computes the precision@k for the specified values of k
(output, target, topk=(1,))
| 481 | |
| 482 | |
| 483 | def accuracy(output, target, topk=(1,)): |
| 484 | """Computes the precision@k for the specified values of k""" |
| 485 | maxk = max(topk) |
| 486 | batch_size = target.size(0) |
| 487 | _, pred = output.topk(maxk, 1, largest=True, sorted=True) |
| 488 | pred = pred.t() |
| 489 | correct = pred.eq(target.view(1, -1).expand_as(pred)) |
| 490 | |
| 491 | res = [] |
| 492 | for k in topk: |
| 493 | correct_k = correct[:k].reshape(-1).float().sum(0, keepdim=True) |
| 494 | res.append(correct_k.mul_(100.0 / batch_size)) |
| 495 | return res |
| 496 | |
| 497 | |
| 498 | def setup_dirs(args: APNamespace) -> Tuple[Path, Path, Path, Path, Path | None]: |
no outgoing calls
no test coverage detected