-
Notifications
You must be signed in to change notification settings - Fork 5
/
running_stats.py
58 lines (46 loc) · 1.69 KB
/
running_stats.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
import torch
class RunningStats(object):
"""Computes running mean and standard deviation"""
def __init__(self, device, dim, n=0., m=None, s=None):
self.n = n
self.m = m
self.s = s
self.dim = dim
self.device = device
def clear(self):
self.n = 0.
def push(self, x):
if type(x) is not torch.Tensor:
x = torch.tensor(x, dtype=torch.float, device=self.device)
self.update_params(x)
def update_params(self, x):
self.n += 1
if self.n == 1:
self.m = x
self.s = torch.zeros(self.dim, dtype=torch.float, device=self.device)
else:
prev_m = self.m.clone()
self.m += (x - self.m) / self.n
self.s += (x - prev_m) * (x - self.m)
def __add__(self, other):
if isinstance(other, RunningStats):
return RunningStats(self.n+other.n, self.n+other.n, self.s+other.s)
else:
self.push(other)
return self
@property
def mean(self):
return self.m if self.n else torch.zeros(self.dim, dtype=torch.float, device=self.device)
def variance(self):
return self.s / self.n if self.n else torch.zeros(self.dim, dtype=torch.float, device=self.device)
@property
def std(self):
return torch.sqrt(self.variance())
def normalize(self, x):
if type(x) is not torch.Tensor:
x = torch.tensor(x, dtype=torch.float, device=self.device)
return (x - self.mean) / self.std
def unnormalize(self, x):
if type(x) is not torch.Tensor:
x = torch.tensor(x, dtype=torch.float, device=self.device)
return x * self.std + self.mean