-
Notifications
You must be signed in to change notification settings - Fork 0
/
normalization.py
53 lines (40 loc) · 1.38 KB
/
normalization.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
import copy
import torch
class RunningMeanStd(object):
def __init__(self, shape):
self.n = 0
self.mean = torch.zeros(shape)
self.S = torch.zeros(shape)
self.std = torch.sqrt(self.S)
def update(self, x):
x = torch.tensor(x)
self.n += 1
if self.n == 1:
self.mean = x
self.std = x
else:
old_mean = copy.copy(self.mean)
self.mean = old_mean + (x - old_mean) / self.n
self.S = self.S + (x - old_mean) * (x - self.mean)
self.std = torch.sqrt(self.S / self.n)
class Normalization(object):
def __init__(self, shape):
self.running_ms = RunningMeanStd(shape=shape)
def __call__(self, x, update=True):
if update:
self.running_ms.update(x)
x = (x - self.running_ms.mean) / (self.running_ms.std + 1e-8)
return x
class RewardScaling(object):
def __init__(self, shape, gamma):
self.shape = shape
self.gamma = gamma
self.running_ms = RunningMeanStd(shape=self.shape)
self.R = torch.zeros(self.shape)
def __call__(self, x):
self.R = self.gamma * self.R + x
self.running_ms.update(self.R)
x = x / (self.running_ms.std + 1e-8)
return x
def reset(self):
self.R = torch.zeros(self.shape)