Computes the accuracy over the k top predictions for the specified values of k
(output, target, topk=(1,))
| 170 | |
| 171 | |
| 172 | def accuracy(output, target, topk=(1,)): |
| 173 | """Computes the accuracy over the k top predictions for the specified values of k""" |
| 174 | with torch.no_grad(): |
| 175 | maxk = max(topk) |
| 176 | batch_size = target.size(0) |
| 177 | |
| 178 | _, pred = output.topk(maxk, 1, True, True) |
| 179 | pred = pred.t() |
| 180 | correct = pred.eq(target.view(1, -1).expand_as(pred)) |
| 181 | |
| 182 | res = [] |
| 183 | for k in topk: |
| 184 | correct_k = correct[:k].view(-1).float().sum(0, keepdim=True) |
| 185 | res.append(correct_k.mul_(100.0 / batch_size)) |
| 186 | return res |
| 187 | |
| 188 | |
| 189 | def seed_torch(seed=1029): |
nothing calls this directly
no outgoing calls
no test coverage detected