MCPcopy Create free account
hub / github.com/Kitware/COAT / SmoothedValue

Class SmoothedValue

utils/utils.py:24–85  ·  view source on GitHub ↗

Track a series of values and provide access to smoothed values over a window or the global series average.

Source from the content-addressed store, hash-verified

22# Logger #
23# -------------------------------------------------------- #
24class SmoothedValue(object):
25 """
26 Track a series of values and provide access to smoothed values over a
27 window or the global series average.
28 """
29
30 def __init__(self, window_size=20, fmt=None):
31 if fmt is None:
32 fmt = "{median:.4f} ({global_avg:.4f})"
33 self.deque = deque(maxlen=window_size)
34 self.total = 0.0
35 self.count = 0
36 self.fmt = fmt
37
38 def update(self, value, n=1):
39 self.deque.append(value)
40 self.count += n
41 self.total += value * n
42
43 def synchronize_between_processes(self):
44 """
45 Warning: does not synchronize the deque!
46 """
47 if not is_dist_avail_and_initialized():
48 return
49 t = torch.tensor([self.count, self.total], dtype=torch.float64, device="cuda")
50 dist.barrier()
51 dist.all_reduce(t)
52 t = t.tolist()
53 self.count = int(t[0])
54 self.total = t[1]
55
56 @property
57 def median(self):
58 d = torch.tensor(list(self.deque))
59 return d.median().item()
60
61 @property
62 def avg(self):
63 d = torch.tensor(list(self.deque), dtype=torch.float32)
64 return d.mean().item()
65
66 @property
67 def global_avg(self):
68 return self.total / self.count
69
70 @property
71 def max(self):
72 return max(self.deque)
73
74 @property
75 def value(self):
76 return self.deque[-1]
77
78 def __str__(self):
79 return self.fmt.format(
80 median=self.median,
81 avg=self.avg,

Callers 2

train_one_epochFunction · 0.90
log_everyMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected