-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtrack.py
59 lines (45 loc) · 1.4 KB
/
track.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
class Tracker():
""" Track training statistics for different variables and view intermediate results through monitors. """
def __init__(self, **monitors):
self.monitors = monitors
self.data = {k: [] for k in self.monitors.keys()}
def new_epoch(self):
for epochs in self.data.values():
epochs.append([])
for monitor in self.monitors.values():
monitor.reset()
def update(self, key, value):
# store value in the current epoch
self.data[key][-1].append(value)
# notify the corresponding monitor about the update
monitor = self.monitors[key]
return monitor.update(value)
class Identity():
def __init__(self):
pass
def update(self, value):
return value
def reset(self):
pass
class Mean():
def __init__(self):
self.reset()
def reset(self):
self.value = 0
self.n = 0
def update(self, value):
self.n += 1
self.value += (value - self.value) / self.n
return self.value
class ExpMean():
def __init__(self, momentum=0.9):
self.momentum = momentum
self.reset()
def reset(self):
self.value = 0
self.debias = 1
def update(self, value):
m = self.momentum
self.debias *= m
self.value = m * self.value + (1 - m) * value
return self.value / (1 - self.debias)