Computes the accuracy over the k top predictions for the specified values of k
(output, target, topk=(1,))
| 23 | self.avg = self.sum / self.count |
| 24 | |
| 25 | def accuracy(output, target, topk=(1,)): |
| 26 | """Computes the accuracy over the k top predictions for the specified values of k""" |
| 27 | with torch.no_grad(): |
| 28 | maxk = max(topk) |
| 29 | batch_size = target.size(0) |
| 30 | _, pred = output.topk(maxk, 1, True, True) |
| 31 | pred = pred.t() |
| 32 | correct = pred.eq(target.view(1, -1).expand_as(pred)) |
| 33 | res = [] |
| 34 | for k in topk: |
| 35 | correct_k = correct[:k].reshape(-1).float().sum(0, keepdim=True) |
| 36 | res.append(correct_k.mul_(100.0 / batch_size)) |
| 37 | return res |
| 38 | |
| 39 | def load_pretrained_weights(model, checkpoint): |
| 40 | """Load pretrianed weights to model |
no outgoing calls
no test coverage detected