Computes the accuracy over the k top predictions for the specified values of k
(output, target, topk=(1,))
| 160 | |
| 161 | |
| 162 | def accuracy(output, target, topk=(1,)): |
| 163 | """Computes the accuracy over the k top predictions for the specified values of k""" |
| 164 | with torch.no_grad(): |
| 165 | maxk = max(topk) |
| 166 | batch_size = target.size(0) |
| 167 | |
| 168 | _, pred = output.topk(maxk, 1, True, True) |
| 169 | pred = pred.t() |
| 170 | # print(target[None]) |
| 171 | correct = pred.eq(target[None]) |
| 172 | |
| 173 | res = [] |
| 174 | for k in topk: |
| 175 | correct_k = correct[:k].flatten().sum(dtype=torch.float32) |
| 176 | res.append(correct_k * (100.0 / batch_size)) |
| 177 | return res |
| 178 | |
| 179 | |
| 180 | def mkdir(path): |