-
Notifications
You must be signed in to change notification settings - Fork 2
/
data.py
60 lines (45 loc) · 1.95 KB
/
data.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
import numpy as np
class ReplayBuffer(object):
def __init__(self):
self.storage = []
# Expects tuples of (state, next_state, action, reward, done)
def add(self, data):
self.storage.append(data)
def save_traj(self, filename='trajectory', dirr=None):
if dirr==None:
print('saving at common folder')
np.save('{}.npy'.format(filename), self.storage)
else:
np.save('{}/{}.npy'.format(dirr,filename), self.storage)
def sample(self, batch_size=100):
ind = np.random.randint(0, len(self.storage), size=batch_size)
x, y, u, r, d = [], [], [], [], []
for i in ind:
X, Y, U, R, D = self.storage[i]
x.append(np.array(X, copy=False))
y.append(np.array(Y, copy=False))
u.append(np.array(U, copy=False))
r.append(np.array(R, copy=False))
d.append(np.array(D, copy=False))
return np.array(x), np.array(y), np.array(u), np.array(r).reshape(
-1, 1), np.array(d).reshape(-1, 1)
class ReplayBufferIRL(object):
def __init__(self):
self.storage = []
# Expects tuples of (state, next_state, action, reward, done)
def add(self, data):
self.storage.append(data)
def sample(self, batch_size=100):
ind = np.random.randint(0, len(self.storage), size=batch_size)
x, y, u, r, d ,e = [], [], [], [], [], []
for i in ind:
X, Y, U, R, D ,E = self.storage[i]
x.append(np.array(X, copy=False))
y.append(np.array(Y, copy=False))
u.append(np.array(U, copy=False))
r.append(np.array(R, copy=False))
d.append(np.array(D, copy=False))
e.append(np.array(E, copy=False))
# state, next_state, action, lprob, reward, done
return np.array(x), np.array(y), np.array(u), np.array(r).reshape(
-1, 1), np.array(d).reshape(-1, 1), np.array(e).reshape(-1, 1)