Computes the accuracy over the k top predictions for the specified values of k
(output, target, topk=(1,))
| 128 | |
| 129 | |
| 130 | def accuracy(output, target, topk=(1,)): |
| 131 | """Computes the accuracy over the k top predictions for the specified values of k""" |
| 132 | with torch.no_grad(): |
| 133 | maxk = max(topk) |
| 134 | batch_size = target.size(0) |
| 135 | |
| 136 | _, pred = output.topk(maxk, 1, True, True) |
| 137 | pred = pred.t() |
| 138 | correct = pred.eq(target.reshape(1, -1).expand_as(pred)) |
| 139 | |
| 140 | res = [] |
| 141 | for k in topk: |
| 142 | correct_k = correct[:k].reshape(-1).float().sum(0, keepdim=True) |
| 143 | res.append(correct_k.mul_(100.0 / batch_size)) |
| 144 | return res |
| 145 | |
| 146 | |
| 147 | def mean_per_class(outputs, targets): |
no outgoing calls
no test coverage detected