Source code for catalyst.utils.meters.averagevaluemeter

import numpy as np

from . import meter


[docs]class AverageValueMeter(meter.Meter): def __init__(self): super(AverageValueMeter, self).__init__() self.reset() self.val = 0
[docs] def add(self, value, n=1): self.val = value self.sum += value self.var += value * value self.n += n if self.n == 0: self.mean, self.std = np.nan, np.nan elif self.n == 1: self.mean = 0.0 + self.sum # Force a copy in torch/numpy self.std = np.inf self.mean_old = self.mean self.m_s = 0.0 else: self.mean = self.mean_old + (value - n * self.mean_old) / float(self.n) self.m_s += (value - self.mean_old) * (value - self.mean) self.mean_old = self.mean self.std = np.sqrt(self.m_s / (self.n - 1.0))
[docs] def value(self): return self.mean, self.std
[docs] def reset(self): self.n = 0 self.sum = 0.0 self.var = 0.0 self.val = 0.0 self.mean = np.nan self.mean_old = 0.0 self.m_s = 0.0 self.std = np.nan