class AverageMeter(object):
时间: 2024-05-17 20:16:33 浏览: 144
class AverageMeter(object):
"""
Computes and stores the average and current value
Imported from https://github.com/pytorch/examples/blob/master/imagenet/main.py#L247-L262
Example usage:
>>> loss = 0.12345
>>> acc = 0.98765
>>> avg_loss = AverageMeter()
>>> avg_acc = AverageMeter()
>>> avg_loss.update(loss)
>>> avg_acc.update(acc)
>>> print(avg_loss, avg_acc)
(0.12345, 0.98765)
"""
def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
def __repr__(self):
return f'{self.avg:.4f}'
该类是一个计算器,用于计算平均值和当前值。它在 PyTorch 官方仓库中的示例代码中被导入,在计算机视觉领域中经常使用。它的用途是跟踪训练过程中的损失值、准确率等指标,并计算它们的平均值。使用示例见该类的注释。
阅读全文