-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathutils.py
61 lines (50 loc) · 1.85 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
#######################################################################
# 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
import torch
from config import Config
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