-
Notifications
You must be signed in to change notification settings - Fork 2
/
train_PEBBLE_with_actual_human_labeller.py
362 lines (305 loc) · 14.7 KB
/
train_PEBBLE_with_actual_human_labeller.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
import argparse
import os
import time
from collections import deque
import gym
import numpy as np
import torch
import utils
from agent.sac import SACAgent
from config.PEBBLE import PEBBLEConfig
from logger import Logger
from replay_buffer import ReplayBuffer
from reward_model import RewardModel, set_device
# https://openai.com/index/learning-from-human-preferences/
def reward_fn(a, ob):
backroll = -ob[7]
height = ob[0]
vel_act = a[0] * ob[8] + a[1] * ob[9] + a[2] * ob[10]
backslide = -ob[5]
return backroll * (1.0 + .3 * height + .1 * vel_act + .05 * backslide)
class Workspace:
def __init__(self, cfg):
self.work_dir = os.path.join(os.getcwd(), 'PEBBLE_human', cfg.env)
self.cfg = cfg
self.logger = Logger(
self.work_dir,
save_tb=cfg.log_save_tb,
log_frequency=cfg.log_frequency,
agent=cfg.agent_name,
train_log_name=cfg.train_log_name,
eval_log_name=cfg.eval_log_name)
utils.set_seed_everywhere(cfg.seed)
self.device = torch.device(cfg.device)
# make env
self.env = gym.make('Hopper-v3',
exclude_current_positions_from_observation=False,
terminate_when_unhealthy=False)
env_record = gym.make('Hopper-v3',
exclude_current_positions_from_observation=False,
terminate_when_unhealthy=False)
self.env.seed(cfg.seed)
env_record.seed(cfg.seed)
self.log_success = False
obs_dim = self.env.observation_space.shape[0]
action_dim = self.env.action_space.shape[0]
action_range = [
float(self.env.action_space.low.min()),
float(self.env.action_space.high.max())
]
self.agent = SACAgent(
obs_dim, action_dim, action_range, cfg
)
self.replay_buffer = ReplayBuffer(
self.env.observation_space.shape,
self.env.action_space.shape,
int(cfg.replay_buffer_capacity),
self.device)
# for logging
self.total_feedback = 0
self.labeled_feedback = 0
self.step = 0
# instantiating the reward model
self.reward_model = RewardModel(
self.env.observation_space.shape[0],
self.env.action_space.shape[0],
ensemble_size=cfg.ensemble_size,
size_segment=cfg.segment,
activation=cfg.activation,
lr=cfg.reward_lr,
mb_size=cfg.reward_batch,
large_batch=cfg.large_batch,
label_margin=cfg.label_margin,
teacher_beta=cfg.teacher_beta,
teacher_gamma=cfg.teacher_gamma,
teacher_eps_mistake=cfg.teacher_eps_mistake,
teacher_eps_skip=cfg.teacher_eps_skip,
teacher_eps_equal=cfg.teacher_eps_equal,
env=env_record,
)
def evaluate(self):
average_episode_reward = 0
average_true_episode_reward = 0
success_rate = 0
num_eval_episodes = self.cfg.num_eval_episodes
for episode in range(num_eval_episodes):
obs = self.env.reset()
self.agent.reset()
done = False
episode_reward = 0
true_episode_reward = 0
if self.log_success:
episode_success = 0
while not done:
with utils.eval_mode(self.agent):
action = self.agent.act(obs, sample=False)
obs, reward, done, extra = self.env.step(action)
episode_reward += reward
true_episode_reward += reward
if self.log_success:
episode_success = max(episode_success, extra['success'])
average_episode_reward += episode_reward
average_true_episode_reward += true_episode_reward
if self.log_success:
success_rate += episode_success
average_episode_reward /= num_eval_episodes
average_true_episode_reward /= num_eval_episodes
if self.log_success:
success_rate /= num_eval_episodes
success_rate *= 100.0
self.logger.log('eval/episode_reward', average_episode_reward,
self.step)
self.logger.log('eval/true_episode_reward', average_true_episode_reward,
self.step)
self.logger.log('eval/num_eval_episodes', num_eval_episodes,
self.step)
if self.log_success:
self.logger.log('eval/success_rate', success_rate,
self.step)
self.logger.log('train/true_episode_success', success_rate,
self.step)
self.logger.dump(self.step)
def learn_reward(self, first_flag=0):
# get feedbacks
labeled_queries = 0
# get feedbacks
if first_flag == 1:
labeled_queries = self.reward_model.uniform_sampling_with_human_labeller()
else:
labeled_queries = self.reward_model.disagreement_sampling_with_human_labeller()
self.total_feedback += self.reward_model.mb_size
self.labeled_feedback += labeled_queries
train_acc = 0
if self.labeled_feedback > 0:
# update reward
for epoch in range(self.cfg.reward_update):
if self.cfg.label_margin > 0 or self.cfg.teacher_eps_equal > 0:
train_acc = self.reward_model.train_soft_reward()
else:
train_acc = self.reward_model.train_reward()
total_acc = np.mean(train_acc)
if total_acc > 0.97:
break
print("Reward function is updated!! ACC: " + str(total_acc))
def run(self):
episode, episode_reward, done = 0, 0, True
if self.log_success:
episode_success = 0
true_episode_reward = 0
# store train returns of recent 10 episodes
avg_train_true_return = deque([], maxlen=10)
start_time = time.time()
interact_count = 0
while self.step < self.cfg.num_train_steps:
# if done, log & evaluate & reset
if done:
if self.step > 0:
self.logger.log('train/duration', time.time() - start_time, self.step)
start_time = time.time()
self.logger.dump(
self.step, save=(self.step > self.cfg.num_seed_steps))
# evaluate agent periodically
if self.step > 0 and self.step % self.cfg.eval_frequency == 0:
self.logger.log('eval/episode', episode, self.step)
self.evaluate()
self.logger.log('train/episode_reward', episode_reward, self.step)
self.logger.log('train/true_episode_reward', true_episode_reward, self.step)
self.logger.log('train/total_feedback', self.total_feedback, self.step)
self.logger.log('train/labeled_feedback', self.labeled_feedback, self.step)
if self.log_success:
self.logger.log('train/episode_success', episode_success,
self.step)
self.logger.log('train/true_episode_success', episode_success,
self.step)
obs = self.env.reset()
self.agent.reset()
done = False
episode_reward = 0
avg_train_true_return.append(true_episode_reward)
true_episode_reward = 0
if self.log_success:
episode_success = 0
episode_step = 0
episode += 1
self.logger.log('train/episode', episode, self.step)
# sample action for data collection
if self.step < self.cfg.num_seed_steps:
action = self.env.action_space.sample()
else:
with utils.eval_mode(self.agent):
action = self.agent.act(obs, sample=True)
# run training update
if self.step == (self.cfg.num_seed_steps + self.cfg.num_unsup_steps):
# update schedule
if self.cfg.reward_schedule == 1:
frac = (self.cfg.num_train_steps-self.step) / self.cfg.num_train_steps
if frac == 0:
frac = 0.01
elif self.cfg.reward_schedule == 2:
frac = self.cfg.num_train_steps / (self.cfg.num_train_steps-self.step +1)
else:
frac = 1
self.reward_model.change_batch(frac)
# update margin --> not necessary / will be updated soon
new_margin = np.mean(avg_train_true_return) * (self.cfg.segment / self.env._max_episode_steps)
self.reward_model.set_teacher_thres_skip(new_margin)
self.reward_model.set_teacher_thres_equal(new_margin)
# first learn reward
self.learn_reward(first_flag=1)
# relabel buffer
self.replay_buffer.relabel_with_predictor(self.reward_model)
# reset Q due to unsuperivsed exploration
self.agent.reset_critic()
# update agent
self.agent.update_after_reset(
self.replay_buffer, self.logger, self.step,
gradient_update=self.cfg.reset_update,
policy_update=True)
# reset interact_count
interact_count = 0
# 3 differences from above: first_flag, corner case, update method (reset critic)
elif self.step > self.cfg.num_seed_steps + self.cfg.num_unsup_steps:
# update reward function
if self.total_feedback < self.cfg.max_feedback:
if interact_count == self.cfg.num_interact:
# update schedule
if self.cfg.reward_schedule == 1:
frac = (self.cfg.num_train_steps-self.step) / self.cfg.num_train_steps
if frac == 0:
frac = 0.01
elif self.cfg.reward_schedule == 2:
frac = self.cfg.num_train_steps / (self.cfg.num_train_steps-self.step +1)
else:
frac = 1
self.reward_model.change_batch(frac)
# update margin --> not necessary / will be updated soon
new_margin = np.mean(avg_train_true_return) * (self.cfg.segment / self.env._max_episode_steps)
self.reward_model.set_teacher_thres_skip(new_margin * self.cfg.teacher_eps_skip)
self.reward_model.set_teacher_thres_equal(new_margin * self.cfg.teacher_eps_equal)
# corner case: new total feed > max feed
if self.reward_model.mb_size + self.total_feedback > self.cfg.max_feedback:
self.reward_model.set_batch(self.cfg.max_feedback - self.total_feedback)
self.learn_reward()
self.replay_buffer.relabel_with_predictor(self.reward_model)
interact_count = 0
self.agent.update(self.replay_buffer, self.logger, self.step, 1)
# unsupervised exploration
elif self.step > self.cfg.num_seed_steps:
self.agent.update_state_ent(self.replay_buffer, self.logger, self.step,
gradient_update=1, K=self.cfg.topK)
next_obs, _, done, extra = self.env.step(action)
reward = reward_fn(action, obs[1:])
reward_hat = self.reward_model.r_hat(np.concatenate([obs, action], axis=-1))
# allow infinite bootstrap
done = float(done)
done_no_max = 0 if episode_step + 1 == self.env._max_episode_steps else done
episode_reward += reward_hat
true_episode_reward += reward
if self.log_success:
episode_success = max(episode_success, extra['success'])
# adding data to the reward training data
self.reward_model.add_data(obs, action, reward, done)
self.replay_buffer.add(
obs, action, reward_hat,
next_obs, done, done_no_max)
obs = next_obs
episode_step += 1
self.step += 1
interact_count += 1
agent_save_dir = os.path.join(self.work_dir, 'agent')
reward_save_dir = os.path.join(self.work_dir, 'reward')
os.makedirs(agent_save_dir, exist_ok=True)
os.makedirs(reward_save_dir, exist_ok=True)
self.agent.save(agent_save_dir, self.step)
self.reward_model.save(reward_save_dir, self.step)
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--seed', type=int)
parser.add_argument('--device', type=str)
parser.add_argument('--eps_equal', type=float)
parser.add_argument('--eps_skip', type=float)
parser.add_argument('--teacher_gamma', type=float)
parser.add_argument('--env', type=str)
parser.add_argument('--actor_lr', type=float)
parser.add_argument('--critic_lr', type=float)
parser.add_argument('--unsup_steps', type=int)
parser.add_argument('--steps', type=int)
parser.add_argument('--num_interact', type=int)
parser.add_argument('--max_feedback', type=int)
parser.add_argument('--reward_batch', type=int)
parser.add_argument('--reward_update', type=int)
parser.add_argument('-b', '--batch_size', type=int)
parser.add_argument('--critic_hidden_dim', type=int)
parser.add_argument('--actor_hidden_dim', type=int)
parser.add_argument('--critic_hidden_depth', type=int)
parser.add_argument('--actor_hidden_depth', type=int)
parser.add_argument('--eps_mistake', type=float)
parser.add_argument('--feed_type', type=int)
parser.add_argument('--ensemble_size', type=int)
args = parser.parse_args()
cfg = PEBBLEConfig(args)
set_device(cfg.device)
workspace = Workspace(cfg)
workspace.run()
if __name__ == '__main__':
main()