Computes the precision@k for the specified values of k
(output, target, topk=(1,))
| 430 | |
| 431 | @torch.no_grad() |
| 432 | def accuracy(output, target, topk=(1,)): |
| 433 | """Computes the precision@k for the specified values of k""" |
| 434 | if target.numel() == 0: |
| 435 | return [torch.zeros([], device=output.device)] |
| 436 | maxk = max(topk) |
| 437 | batch_size = target.size(0) |
| 438 | |
| 439 | _, pred = output.topk(maxk, 1, True, True) |
| 440 | pred = pred.t() |
| 441 | correct = pred.eq(target.view(1, -1).expand_as(pred)) |
| 442 | |
| 443 | res = [] |
| 444 | for k in topk: |
| 445 | correct_k = correct[:k].view(-1).float().sum(0) |
| 446 | res.append(correct_k.mul_(100.0 / batch_size)) |
| 447 | return res |
| 448 | |
| 449 | |
| 450 | def interpolate(input, size=None, scale_factor=None, mode="nearest", align_corners=None): |