Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add window size to AverageMeter #112

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions utils.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
import numpy as np
import re
import functools
from collections import deque

class AverageMeter(object):
"""Computes and stores the average and current value"""
def __init__(self):
def __init__(self, window_size=20):
self.deque = deque(maxlen=window_size)
self.initialized = False
self.val = None
self.avg = None
self.sum = None
self.count = None

def initialize(self, val, weight):
self.val = val
self.avg = val
self.sum = val * weight
self.count = weight
self.deque.append(val)
self.initialized = True

def update(self, val, weight=1):
Expand All @@ -28,14 +29,16 @@ def add(self, val, weight):
self.val = val
self.sum += val * weight
self.count += weight
self.avg = self.sum / self.count
self.deque.append(val)

def value(self):
return self.val

def average(self):
return self.avg
return np.mean(self.deque)

def median(self):
return np.median(self.deque)

def unique(ar, return_index=False, return_inverse=False, return_counts=False):
ar = np.asanyarray(ar).flatten()
Expand Down