class AverageMeter(object):
"""Computes and stores the average and current value"""
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
# 实例化 AverageMeter 类
avg_meter = AverageMeter()
# 更新数据点
data_points = [10, 15, 20, 25, 30]
for point in data_points:
avg_meter.update(point)
# 输出平均值
print(f"Current Value: {avg_meter.val}")
print(f"Sum of Values: {avg_meter.sum}")
print(f"Total Count: {avg_meter.count}")
print(f"Average of Values: {avg_meter.avg}")
04-19
207
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)