-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathlogging_utils.py
59 lines (49 loc) · 1.83 KB
/
logging_utils.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
import numpy as np
def get_mean_std_max_min_dict(array, prefix):
res = {}
res[prefix + "/mean"] = np.mean(array)
res[prefix + "/std"] = np.std(array)
res[prefix + "/min"] = np.amin(array)
res[prefix + "/max"] = np.amax(array)
return res
class Metrics:
"""Object keeping running average/latest of relevant metrics to log."""
def __init__(self, *args):
self.metrics = {arg: 0 for arg in args}
self.latest_metrics = {arg: 0 for arg in args}
self.samples = {arg: 1e-8 for arg in args}
self.logged_metrics = [arg for arg in args]
def reset(self):
for arg in self.metrics:
self.metrics[arg] = 0
self.samples[arg] = 1e-8
def add(self, *args):
for arg in args:
if arg not in self.metrics:
self.logged_metrics.append(arg)
self.metrics[arg] = 0
self.latest_metrics[arg] = 0
self.samples[arg] = 1e-8
def update(self, **kwargs):
for arg, val in kwargs.items():
if arg not in self.metrics:
self.logged_metrics += arg
self.metrics[arg] = 0
self.latest_metrics[arg] = 0
self.samples[arg] = 1e-8
self.metrics[arg] += val
self.samples[arg] += 1
def set(self, **kwargs):
for arg, val in kwargs.items():
if arg not in self.metrics:
self.logged_metrics += arg
self.metrics[arg] = val
self.samples[arg] = 1
self.metrics[arg] = val
self.samples[arg] = 1
def get(self):
for arg, metric_agg in self.metrics.items():
samples = self.samples[arg]
if samples >= 1:
self.latest_metrics[arg] = metric_agg / samples
return self.latest_metrics