CoolFace
Apppublic

Saahil-doryu/Nested-Co-teaching

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
utils.py26 linesDownload Raw Back to root
1import torch
2
3class AverageMeter(object):
4    """Computes and stores the average and current value"""
5    def __init__(self):
6        self.reset()
7
8    def reset(self):
9        self.val = 0
10        self.avg = 0
11        self.sum = 0
12        self.count = 0
13
14    def update(self, val, n=1):
15        self.val = val
16        self.sum += val * n
17        self.count += n
18        self.avg = self.sum / self.count
19
20def accuracy(output, target):
21    """Computes the Top-1 accuracy for a single prediction"""
22    with torch.no_grad():
23        pred = output.argmax(dim=1)  # Get the highest scoring class
24        correct = pred.eq(target).sum().item()  # Compare with the actual target
25        return correct * 100.0  # Convert to percentage
26