Computes the precision@k for the specified values of k
(output, target, topk=(1,))
| 520 | |
| 521 | @torch.no_grad() |
| 522 | def accuracy(output, target, topk=(1,)): |
| 523 | """Computes the precision@k for the specified values of k""" |
| 524 | if target.numel() == 0: |
| 525 | return [torch.zeros([], device=output.device)] |
| 526 | maxk = max(topk) |
| 527 | batch_size = target.size(0) |
| 528 | |
| 529 | _, pred = output.topk(maxk, 1, True, True) |
| 530 | pred = pred.t() |
| 531 | correct = pred.eq(target.view(1, -1).expand_as(pred)) |
| 532 | |
| 533 | res = [] |
| 534 | for k in topk: |
| 535 | correct_k = correct[:k].view(-1).float().sum(0) |
| 536 | res.append(correct_k.mul_(100.0 / batch_size)) |
| 537 | return res |
| 538 | |
| 539 | |
| 540 | def interpolate(input, size=None, scale_factor=None, mode="nearest", align_corners=None): |