-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
93 lines (74 loc) · 2.73 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#######################################################################
# Copyright (C) 2017 Shangtong Zhang([email protected]) #
# Permission given to modify the code as long as you keep this #
# declaration at the top #
#######################################################################
# the original version comes from:
# https://github.com/ShangtongZhang/DeepRL
# below code has been modified to my suit my needs
import numpy as np
from baselines.common.running_mean_std import RunningMeanStd
import torch
from config import Config
class MeanStdNormalizer:
def __init__(self, read_only=False, clip=10.0, epsilon=1e-8):
self.read_only = read_only
self.rms = None
self.clip = clip
self.epsilon = epsilon
def __call__(self, x):
x = np.asarray(x)
if self.rms is None:
self.rms = RunningMeanStd(shape=(1, ) + x.shape[1:])
if not self.read_only:
self.rms.update(x)
return np.clip((x - self.rms.mean) / np.sqrt(self.rms.var + self.epsilon),
-self.clip, self.clip)
class RescaleNormalizer:
def __init__(self, coef=1.0):
self.coef = coef
def __call__(self, x):
x = np.asarray(x)
return self.coef * x
class ImageNormalizer(RescaleNormalizer):
def __init__(self):
RescaleNormalizer.__init__(self, 1.0 / 255)
class Storage:
def __init__(self, size, keys=None):
if keys is None:
keys = []
keys = keys + ['s', 'a', 'r', 'm',
'v', 'q', 'pi', 'log_pi', 'ent',
'adv', 'ret', 'q_a', 'log_pi_a',
'mean']
self.keys = keys
self.size = size
self.reset()
def add(self, data):
for k, v in data.items():
assert k in self.keys
getattr(self, k).append(v)
def placeholder(self):
for k in self.keys:
v = getattr(self, k)
if len(v) == 0:
setattr(self, k, [None] * self.size)
def reset(self):
for key in self.keys:
setattr(self, key, [])
def cat(self, keys):
data = [getattr(self, k)[:self.size] for k in keys]
return map(lambda x: torch.cat(x, dim=0), data)
def random_sample(indices, batch_size):
indices = np.asarray(np.random.permutation(indices))
batches = indices[:len(indices) // batch_size * batch_size].reshape(-1, batch_size)
for batch in batches:
yield batch
r = len(indices) % batch_size
if r:
yield indices[-r:]
def tensor(x):
if isinstance(x, torch.Tensor):
return x
x = torch.tensor(x, device=Config.DEVICE, dtype=torch.float32)
return x