Computes and stores the average and current value
| 17 | |
| 18 | |
| 19 | class AverageMeter: |
| 20 | """Computes and stores the average and current value""" |
| 21 | |
| 22 | def __init__(self): |
| 23 | self.reset() |
| 24 | |
| 25 | def reset(self): |
| 26 | # local |
| 27 | self._val = 0 |
| 28 | self._sum = 0 |
| 29 | self._count = 0 |
| 30 | # global |
| 31 | self._history_avg = 0 |
| 32 | self._history_count = 0 |
| 33 | self._avg = None |
| 34 | |
| 35 | def update(self, val, n=1): |
| 36 | self._val = val |
| 37 | self._sum += val * n |
| 38 | self._count += n |
| 39 | self._avg = None |
| 40 | |
| 41 | @property |
| 42 | def val(self): |
| 43 | return self._val |
| 44 | |
| 45 | @property |
| 46 | def count(self): |
| 47 | return self._count + self._history_count |
| 48 | |
| 49 | @property |
| 50 | def avg(self): |
| 51 | if self._avg is None: |
| 52 | # compute avg |
| 53 | r = self._history_count / max(1, self._history_count + self._count) |
| 54 | _avg = self._sum / max(1, self._count) |
| 55 | self._avg = r * self._history_avg + (1.0 - r) * _avg |
| 56 | return self._avg |
| 57 | |
| 58 | def sync(self): |
| 59 | buf = torch.tensor([self._sum, self._count], |
| 60 | dtype=torch.float32).cuda() |
| 61 | buf = reduce_tensor(buf, 1) |
| 62 | _sum, _count = buf.tolist() |
| 63 | _avg = _sum / max(1, _count) |
| 64 | r = self._history_count / max(1, self._history_count + _count) |
| 65 | |
| 66 | self._history_avg = r * self._history_avg + (1.0 - r) * _avg |
| 67 | self._history_count += _count |
| 68 | |
| 69 | self._sum = 0 |
| 70 | self._count = 0 |
| 71 | |
| 72 | self._avg = None |