-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdemonstrations.py
executable file
·210 lines (147 loc) · 8.88 KB
/
demonstrations.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import numpy as np
import torch
from replay_buffer import ReplayBuffer
import utils
import pdb
class Demonstrations(ReplayBuffer):
"""Buffer to store demonstrations in the format (s, a, s', a')"""
def __init__(self, obs_shape, action_shape, capacity, device):
super().__init__(obs_shape, action_shape, capacity, device)
self.next_actions = np.empty((capacity, *action_shape), dtype=np.float32)
self.recent_idx = None
self.init_idx = False
self.last_idx = 0
def __len__(self):
return self.capacity if self.full else self.idx
def add(self, obs, action, reward, next_obs, next_action, done, done_no_max):
np.copyto(self.obses[self.idx], obs)
np.copyto(self.actions[self.idx], action)
np.copyto(self.rewards[self.idx], reward)
np.copyto(self.next_obses[self.idx], next_obs)
np.copyto(self.next_actions[self.idx], next_action)
np.copyto(self.not_dones[self.idx], not done)
np.copyto(self.not_dones_no_max[self.idx], not done_no_max)
self.idx = (self.idx + 1) % self.capacity
self.full = self.full or self.idx == 0
def sample(self, batch_size):
self.recent_idx = idxs = np.random.randint(0, self.capacity if self.full else self.idx,
size=batch_size)
obses = torch.as_tensor(self.obses[idxs], device=self.device).float()
actions = torch.as_tensor(self.actions[idxs], device=self.device)
rewards = torch.as_tensor(self.rewards[idxs], device=self.device)
next_obses = torch.as_tensor(self.next_obses[idxs], device=self.device).float()
next_actions = torch.as_tensor(self.next_actions[idxs], device=self.device)
not_dones = torch.as_tensor(self.not_dones[idxs], device=self.device)
not_dones_no_max = torch.as_tensor(self.not_dones_no_max[idxs], device=self.device)
return obses, actions, rewards, next_obses, next_actions, not_dones, not_dones_no_max
def sample_3d(self, batch_size, horizon):
if self.full:
assert self.capacity > horizon
else:
assert self.idx > horizon
self.recent_idx = idxs = np.random.randint(0,
self.capacity - horizon if self.full else self.idx - horizon,
size=(batch_size, 1))
idxs = np.concatenate([idxs + i for i in range(horizon)], axis=1)
obses = torch.as_tensor(self.obses[idxs], device=self.device).float()
actions = torch.as_tensor(self.actions[idxs], device=self.device)
rewards = torch.as_tensor(self.rewards[idxs], device=self.device)
next_obses = torch.as_tensor(self.next_obses[idxs], device=self.device).float()
next_actions = torch.as_tensor(self.next_actions[idxs], device=self.device).float()
not_dones = torch.as_tensor(self.not_dones[idxs], device=self.device)
not_dones_no_max = torch.as_tensor(self.not_dones_no_max[idxs], device=self.device)
return obses, actions, rewards, next_obses, next_actions, not_dones, not_dones_no_max
def sample_initial(self, batch_size, init_tolerance=10, episode_len=1000):
buffer_size = self.capacity if self.full else self.idx
episode_len = 1000
if not self.init_idx:
self.init_idx = [init_idx for init_idx in range(buffer_size) if init_idx % episode_len < init_tolerance]
elif (not self.full) and (self.idx > self.last_idx):
for new_idx in range(self.last_idx, self.idx):
if new_idx % episode_len < init_tolerance:
self.init_idx.append(new_idx)
self.last_idx = self.idx
idxs = np.random.choice(self.init_idx, size=batch_size, replace=True)
obses = torch.as_tensor(self.obses[idxs], device=self.device).float()
actions = torch.as_tensor(self.actions[idxs], device=self.device)
rewards = torch.as_tensor(self.rewards[idxs], device=self.device)
next_obses = torch.as_tensor(self.next_obses[idxs], device=self.device).float()
next_actions = torch.as_tensor(self.next_actions[idxs], device=self.device).float()
not_dones = torch.as_tensor(self.not_dones[idxs], device=self.device)
not_dones_no_max = torch.as_tensor(self.not_dones_no_max[idxs], device=self.device)
return obses, actions, rewards, next_obses, next_actions, not_dones, not_dones_no_max
def sample_recent(self, batch_size, recent_horizon=100000):
buffer_size = self.capacity if self.full else self.idx
assert not self.full
# assert buffer_size > recent_horizon
recent_idx = list(range(np.max([0, buffer_size - recent_horizon]), buffer_size))
idxs = np.random.choice(recent_idx, size=batch_size, replace=True)
obses = torch.as_tensor(self.obses[idxs], device=self.device).float()
actions = torch.as_tensor(self.actions[idxs], device=self.device)
rewards = torch.as_tensor(self.rewards[idxs], device=self.device)
next_obses = torch.as_tensor(self.next_obses[idxs], device=self.device).float()
next_actions = torch.as_tensor(self.next_actions[idxs], device=self.device).float()
not_dones = torch.as_tensor(self.not_dones[idxs], device=self.device)
not_dones_no_max = torch.as_tensor(self.not_dones_no_max[idxs], device=self.device)
return obses, actions, rewards, next_obses, next_actions, not_dones, not_dones_no_max
def sample_random_seed(self, batch_size, num_seed=200000):
buffer_size = self.capacity if self.full else self.idx
assert not self.full
assert buffer_size >= num_seed
seed_idx = list(range(num_seed))
idxs = np.random.choice(seed_idx, size=batch_size, replace=True)
obses = torch.as_tensor(self.obses[idxs], device=self.device).float()
actions = torch.as_tensor(self.actions[idxs], device=self.device)
rewards = torch.as_tensor(self.rewards[idxs], device=self.device)
next_obses = torch.as_tensor(self.next_obses[idxs], device=self.device).float()
next_actions = torch.as_tensor(self.next_actions[idxs], device=self.device).float()
not_dones = torch.as_tensor(self.not_dones[idxs], device=self.device)
not_dones_no_max = torch.as_tensor(self.not_dones_no_max[idxs], device=self.device)
return obses, actions, rewards, next_obses, next_actions, not_dones, not_dones_no_max
def get_all_obs_acs(self):
obses = torch.as_tensor(self.next_obses, device=self.device).float()
actions = torch.as_tensor(self.next_actions, device=self.device)
return obses, actions
# TODO: check that this function is correct again
def get_subset(self, num_trajectories, traj_len=1000):
# check that this is a demonstration set, and thus the buffer is full
demo_num_traj = int(self.capacity / traj_len)
assert self.full
assert demo_num_traj >= num_trajectories
assert self.capacity % traj_len == 0
traj_idxs = traj_len * np.random.choice(demo_num_traj,
size=num_trajectories,
replace=False)
idxs = [idx + j for idx in traj_idxs for j in range(traj_len)]
self.obses = self.obses[idxs]
self.next_obses = self.next_obses[idxs]
self.actions = self.actions[idxs]
self.next_actions = self.next_actions[idxs]
self.rewards = self.rewards[idxs]
self.not_dones = self.not_dones[idxs]
self.not_dones_no_max = self.not_dones_no_max[idxs]
self.capacity = num_trajectories * traj_len
self.idx = 0
def update_irl_rewards(self, reward_func):
assert isinstance(reward_func, torch.nn.Module)
all_obs = torch.as_tensor(self.obses, device=self.device).float()
all_acs = torch.as_tensor(self.actions, device=self.device)
param_dim = reward_func.get_param_dim()
# TODO: confirm that eval_mode is the right thing here
with utils.eval_mode(reward_func):
self.irl_rewards = utils.to_np(reward_func(all_obs, all_acs))
self.irl_grads = utils.to_np(reward_func.get_param_grad(all_obs, all_acs))
assert self.irl_rewards.shape == (self.capacity, 1)
assert self.irl_grads.shape == (self.capacity, param_dim)
def sample_irl_rewards(self, sample_idx=None):
if sample_idx is not None:
# Use user specified sampling idices
idxs = sample_idx
else:
# Use the indices for the most recently sampled transitions
assert self.recent_idx is not None
assert hasattr(self, 'irl_rewards') and hasattr(self, 'irl_grads')
idxs = self.recent_idx
irl_rewards = torch.as_tensor(self.irl_rewards[idxs], device=self.device)
irl_grads = torch.as_tensor(self.irl_grads[idxs], device=self.device)
return irl_rewards, irl_grads