Computes the precision@k for the specified values of k.
(output, target, topk=(1, ))
| 590 | |
| 591 | @torch.no_grad() |
| 592 | def accuracy(output, target, topk=(1, )): |
| 593 | """Computes the precision@k for the specified values of k.""" |
| 594 | if target.numel() == 0: |
| 595 | return [torch.zeros([], device=output.device)] |
| 596 | maxk = max(topk) |
| 597 | batch_size = target.size(0) |
| 598 | |
| 599 | _, pred = output.topk(maxk, 1, True, True) |
| 600 | pred = pred.t() |
| 601 | correct = pred.eq(target.view(1, -1).expand_as(pred)) |
| 602 | |
| 603 | res = [] |
| 604 | for k in topk: |
| 605 | correct_k = correct[:k].view(-1).float().sum(0) |
| 606 | res.append(correct_k.mul_(100.0 / batch_size)) |
| 607 | return res |
| 608 | |
| 609 | |
| 610 | def interpolate(input, |
no outgoing calls
no test coverage detected