-
Notifications
You must be signed in to change notification settings - Fork 2
/
get_dataset.py
290 lines (245 loc) · 10.6 KB
/
get_dataset.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
import numpy as np
def qlearning_dataset(dataset, terminate_on_end=False):
"""
Returns datasets formatted for use by standard Q-learning algorithms,
with observations, actions, next_observations, rewards, and a terminal
flag.
"""
N = dataset['rewards'].shape[0]
obs_ = []
next_obs_ = []
action_ = []
reward_ = []
done_ = []
episode_step = 0
for i in range(N-1):
obs = dataset['observations'][i].astype(np.float32)
new_obs = dataset['observations'][i+1].astype(np.float32)
action = dataset['actions'][i].astype(np.float32)
reward = dataset['rewards'][i].astype(np.float32)
done_bool = bool(dataset['terminals'][i])
final_timestep = dataset['timeouts'][i]
if (not terminate_on_end) and final_timestep:
# Skip this transition and don't apply terminals on the last step of an episode
episode_step = 0
continue
if done_bool or final_timestep:
episode_step = 0
obs_.append(obs)
next_obs_.append(new_obs)
action_.append(action)
reward_.append(reward)
done_.append(done_bool)
episode_step += 1
return {
'observations': np.array(obs_),
'actions': np.array(action_),
'next_observations': np.array(next_obs_),
'rewards': np.array(reward_),
'terminals': np.array(done_),
}
def dataset_setting1(dataset1, dataset2, split_x, exp_num=10):
"""
Returns D_e and D_o of setting 1 in the paper.
"""
dataset_o = dataset_T_trajs(dataset2, 1000)
dataset_o['flag'] = np.zeros_like(dataset_o['terminals'])
dataset_e, dataset_o_extra = dataset_split_expert(dataset1, split_x, exp_num)
dataset_e['flag'] = np.ones_like(dataset_e['terminals'])
dataset_o_extra['flag'] = np.ones_like(dataset_o_extra['terminals'])
for key in dataset_o.keys():
dataset_o[key] = np.concatenate([dataset_o[key], dataset_o_extra[key]], 0)
return dataset_e, dataset_o
def dataset_setting2(dataset1, split_x):
"""
Returns D_e and D_o of setting 2 in the paper.
"""
dataset_e, dataset_o = dataset_split_replay(dataset1, split_x)
dataset_e['flag'] = np.ones_like(dataset_e['terminals'])
dataset_o['flag'] = np.zeros_like(dataset_o['terminals'])
return dataset_e, dataset_o
def dataset_setting_demodice(dataset1, dataset2, num_e=1, num_o_e=10, num_o_o=1000):
"""
Returns D_e and D_o of setting in demodice.
"""
dataset_o = dataset_T_trajs(dataset2, num_o_o)
dataset_o['flag'] = np.zeros_like(dataset_o['terminals'])
dataset_e, dataset_o_extra = dataset_split_expert(dataset1, num_o_e, num_e+num_o_e)
dataset_e['flag'] = np.ones_like(dataset_e['terminals'])
dataset_o_extra['flag'] = np.ones_like(dataset_o_extra['terminals'])
for key in dataset_o.keys():
dataset_o[key] = np.concatenate([dataset_o[key], dataset_o_extra[key]], 0)
return dataset_e, dataset_o
def dataset_split_replay(dataset, split_x, terminate_on_end=False):
"""
Returns D_e and D_o from replay datasets.
"""
N = dataset['rewards'].shape[0]
return_traj = []
obs_traj = [[]]
next_obs_traj = [[]]
action_traj = [[]]
reward_traj = [[]]
done_traj = [[]]
for i in range(N-1):
obs_traj[-1].append(dataset['observations'][i].astype(np.float32))
next_obs_traj[-1].append(dataset['observations'][i+1].astype(np.float32))
action_traj[-1].append(dataset['actions'][i].astype(np.float32))
reward_traj[-1].append(dataset['rewards'][i].astype(np.float32))
done_traj[-1].append(bool(dataset['terminals'][i]))
final_timestep = dataset['timeouts'][i] | dataset['terminals'][i]
if (not terminate_on_end) and final_timestep:
# Skip this transition and don't apply terminals on the last step of an episode
return_traj.append(np.sum(reward_traj[-1]))
obs_traj.append([])
next_obs_traj.append([])
action_traj.append([])
reward_traj.append([])
done_traj.append([])
# select top 5% return trajectories
inds_all = np.argsort(return_traj)[::-1]
succ_num = int(len(inds_all) * 0.05)
inds_top5 = inds_all[:succ_num]
inds_e = inds_top5[1::split_x]
# inds_e = inds_top5[:split_x]
inds_e = list(inds_e)
inds_all = list(inds_all)
inds_o = set(inds_all) - set(inds_e)
inds_o = list(inds_o)
print('# select {} trajs in mixed dataset as D_e, mean is {}'.format(len(inds_e), np.array(return_traj)[inds_e].mean()))
print('# select {} trajs in mixed dataset as D_o, mean is {}'.format(len(inds_o), np.array(return_traj)[inds_o].mean()))
obs_traj_e = [obs_traj[i] for i in inds_e]
next_obs_traj_e = [next_obs_traj[i] for i in inds_e]
action_traj_e = [action_traj[i] for i in inds_e]
reward_traj_e = [reward_traj[i] for i in inds_e]
done_traj_e = [done_traj[i] for i in inds_e]
obs_traj_o = [obs_traj[i] for i in inds_o]
next_obs_traj_o = [next_obs_traj[i] for i in inds_o]
action_traj_o = [action_traj[i] for i in inds_o]
reward_traj_o = [reward_traj[i] for i in inds_o]
done_traj_o = [done_traj[i] for i in inds_o]
def concat_trajectories(trajectories):
return np.concatenate(trajectories, 0)
dataset_e = {
'observations': concat_trajectories(obs_traj_e),
'actions': concat_trajectories(action_traj_e),
'next_observations': concat_trajectories(next_obs_traj_e),
'rewards': concat_trajectories(reward_traj_e),
'terminals': concat_trajectories(done_traj_e),
}
dataset_o = {
'observations': concat_trajectories(obs_traj_o),
'actions': concat_trajectories(action_traj_o),
'next_observations': concat_trajectories(next_obs_traj_o),
'rewards': concat_trajectories(reward_traj_o),
'terminals': concat_trajectories(done_traj_o),
}
return dataset_e, dataset_o
def dataset_split_expert(dataset, split_x, exp_num, terminate_on_end=False):
"""
Returns D_e and expert data in D_o of setting 1 in the paper.
"""
N = dataset['rewards'].shape[0]
return_traj = []
obs_traj = [[]]
next_obs_traj = [[]]
action_traj = [[]]
reward_traj = [[]]
done_traj = [[]]
for i in range(N-1):
obs_traj[-1].append(dataset['observations'][i].astype(np.float32))
next_obs_traj[-1].append(dataset['observations'][i+1].astype(np.float32))
action_traj[-1].append(dataset['actions'][i].astype(np.float32))
reward_traj[-1].append(dataset['rewards'][i].astype(np.float32))
done_traj[-1].append(bool(dataset['terminals'][i]))
final_timestep = dataset['timeouts'][i] | dataset['terminals'][i]
if (not terminate_on_end) and final_timestep:
# Skip this transition and don't apply terminals on the last step of an episode
return_traj.append(np.sum(reward_traj[-1]))
obs_traj.append([])
next_obs_traj.append([])
action_traj.append([])
reward_traj.append([])
done_traj.append([])
# select 10 trajectories
inds_all = list(range(len(obs_traj)))
inds_succ = inds_all[:exp_num]
inds_o = inds_succ[-split_x:]
inds_o = list(inds_o)
inds_succ = list(inds_succ)
inds_e = set(inds_succ) - set(inds_o)
inds_e = list(inds_e)
print('# select {} trajs in expert dataset as D_e'.format(len(inds_e)))
print('# select {} trajs in expert dataset as expert data in D_o'.format(len(inds_o)))
obs_traj_e = [obs_traj[i] for i in inds_e]
next_obs_traj_e = [next_obs_traj[i] for i in inds_e]
action_traj_e = [action_traj[i] for i in inds_e]
reward_traj_e = [reward_traj[i] for i in inds_e]
done_traj_e = [done_traj[i] for i in inds_e]
obs_traj_o = [obs_traj[i] for i in inds_o]
next_obs_traj_o = [next_obs_traj[i] for i in inds_o]
action_traj_o = [action_traj[i] for i in inds_o]
reward_traj_o = [reward_traj[i] for i in inds_o]
done_traj_o = [done_traj[i] for i in inds_o]
def concat_trajectories(trajectories):
return np.concatenate(trajectories, 0)
dataset_e = {
'observations': concat_trajectories(obs_traj_e),
'actions': concat_trajectories(action_traj_e),
'next_observations': concat_trajectories(next_obs_traj_e),
'rewards': concat_trajectories(reward_traj_e),
'terminals': concat_trajectories(done_traj_e),
}
dataset_o = {
'observations': concat_trajectories(obs_traj_o),
'actions': concat_trajectories(action_traj_o),
'next_observations': concat_trajectories(next_obs_traj_o),
'rewards': concat_trajectories(reward_traj_o),
'terminals': concat_trajectories(done_traj_o),
}
return dataset_e, dataset_o
def dataset_T_trajs(dataset, T, terminate_on_end=False):
"""
Returns T trajs from dataset.
"""
N = dataset['rewards'].shape[0]
return_traj = []
obs_traj = [[]]
next_obs_traj = [[]]
action_traj = [[]]
reward_traj = [[]]
done_traj = [[]]
for i in range(N-1):
obs_traj[-1].append(dataset['observations'][i].astype(np.float32))
next_obs_traj[-1].append(dataset['observations'][i+1].astype(np.float32))
action_traj[-1].append(dataset['actions'][i].astype(np.float32))
reward_traj[-1].append(dataset['rewards'][i].astype(np.float32))
done_traj[-1].append(bool(dataset['terminals'][i]))
final_timestep = dataset['timeouts'][i] | dataset['terminals'][i]
if (not terminate_on_end) and final_timestep:
# Skip this transition and don't apply terminals on the last step of an episode
return_traj.append(np.sum(reward_traj[-1]))
obs_traj.append([])
next_obs_traj.append([])
action_traj.append([])
reward_traj.append([])
done_traj.append([])
# select T trajectories
inds_all = list(range(len(obs_traj)))
inds = inds_all[:T]
inds = list(inds)
print('# select {} trajs in the dataset'.format(T))
obs_traj = [obs_traj[i] for i in inds]
next_obs_traj = [next_obs_traj[i] for i in inds]
action_traj = [action_traj[i] for i in inds]
reward_traj = [reward_traj[i] for i in inds]
done_traj = [done_traj[i] for i in inds]
def concat_trajectories(trajectories):
return np.concatenate(trajectories, 0)
return {
'observations': concat_trajectories(obs_traj),
'actions': concat_trajectories(action_traj),
'next_observations': concat_trajectories(next_obs_traj),
'rewards': concat_trajectories(reward_traj),
'terminals': concat_trajectories(done_traj),
}