Created
May 31, 2019 04:05
-
-
Save wassname/69b9a633e5b2d59087de3a58ebb59b58 to your computer and use it in GitHub Desktop.
runningmean.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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