-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils.py
47 lines (40 loc) · 1.47 KB
/
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
import typing
import torch
from typing import Tuple
class RunningMeanStd(object):
"""
TODO: Move into utils file?
Taken from: https://github.com/semitable/fast-marl
"""
def __init__(self, epsilon: float = 1e-4, shape: Tuple[int, ...] = (), device="cpu"):
"""
https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Parallel_algorithm
"""
self.mean = torch.zeros(shape, dtype=torch.float32, device=device)
self.var = torch.ones(shape, dtype=torch.float32, device=device)
self.count = epsilon
def update(self, arr):
#arr = arr.reshape(-1, arr.size(-1))
batch_mean = torch.mean(arr, dim=1)
batch_var = torch.var(arr, dim=1)
batch_count = arr.shape[1]
self._update_from_moments(batch_mean, batch_var, batch_count)
def _update_from_moments(self, batch_mean, batch_var, batch_count: int):
delta = batch_mean - self.mean
tot_count = self.count + batch_count
new_mean = self.mean + delta * batch_count / tot_count
m_a = self.var * self.count
m_b = batch_var * batch_count
m_2 = (
m_a
+ m_b
+ torch.square(delta)
* self.count
* batch_count
/ (self.count + batch_count)
)
new_var = m_2 / (self.count + batch_count)
new_count = batch_count + self.count
self.mean = new_mean
self.var = new_var
self.count = new_count