Skip to content

Instantly share code, notes, and snippets.

@wassname
Created May 31, 2019 04:05
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save wassname/69b9a633e5b2d59087de3a58ebb59b58 to your computer and use it in GitHub Desktop.
runningmean.py
class RunningMean(object):
def __init__(self, sum=0, i=0):
self.sum = sum
self.i = i
def __add__(self, other):
return RunningMean(self.sum+other, self.i+1)
def add(self, loss):
self.sum += loss
self.i += 1
def mean(self):
return self.sum/(self.i+1e-5)
def __repr__(self):
return '<RunningMean(mean={:2.4f}, sum={:2.4f}, n={})>'.format(self.mean(), self.sum, self.i)
def __str__(self):
return 'mean={:2.4f}'.format(self.mean())
# test
import random
m = RunningMean()
for i in range(10):
m += random.random()
print(m)
m
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment