Source code for torchmate.utils.running_average

[docs] 1class RunningAverage: 2 """A simple class that maintains the running average of a quantity 3 4 **Example usage:** 5 6 .. code-block:: python 7 8 loss_avg = RunningAverage() 9 loss_avg.update(2) 10 loss_avg.update(4) 11 loss_avg() = 3 ## 2+4/2=3 12 13 """ 14 15 def __init__(self): 16 self.steps = 0 17 self.total = 0 18
[docs] 19 def update(self, val: int) -> None: 20 self.total += val 21 self.steps += 1
22 23 def __call__(self): 24 if self.steps: 25 avg = self.total / (float(self.steps)) 26 else: 27 avg = 0.0 28 return avg