MCPcopy Index your code
hub / github.com/HobbitLong/PyContrast / accuracy

Function accuracy

pycontrast/learning/util.py:24–38  ·  view source on GitHub ↗

Computes the accuracy over the k top predictions for the specified values of k

(output, target, topk=(1,))

Source from the content-addressed store, hash-verified

22
23
24def accuracy(output, target, topk=(1,)):
25 """Computes the accuracy over the k top predictions for the specified values of k"""
26 with torch.no_grad():
27 maxk = max(topk)
28 batch_size = target.size(0)
29
30 _, pred = output.topk(maxk, 1, True, True)
31 pred = pred.t()
32 correct = pred.eq(target.view(1, -1).expand_as(pred))
33
34 res = []
35 for k in topk:
36 correct_k = correct[:k].view(-1).float().sum(0, keepdim=True)
37 res.append(correct_k.mul_(100.0 / batch_size))
38 return res

Callers 3

accMethod · 0.85
trainMethod · 0.85
validateMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected