-
Notifications
You must be signed in to change notification settings - Fork 0
/
ppo_lstm.py
307 lines (272 loc) · 8.88 KB
/
ppo_lstm.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
#######################################################################
# Copyright (C) 2017 Shangtong Zhang(zhangshangtong.cpp@gmail.com) #
# Permission given to modify the code as long as you keep this #
# declaration at the top #
#######################################################################
from deep_rl import *
def set_tasks(config):
if config.game == 'dm-walker':
tasks = ['walk', 'run']
elif config.game == 'dm-walker-1':
tasks = ['squat', 'stand']
config.game = 'dm-walker'
elif config.game == 'dm-walker-2':
tasks = ['walk', 'backward']
config.game = 'dm-walker'
elif config.game == 'dm-finger':
tasks = ['turn_easy', 'turn_hard']
elif config.game == 'dm-reacher':
tasks = ['easy', 'hard']
elif config.game == 'dm-cartpole-b':
tasks = ['balance', 'balance_sparse']
config.game = 'dm-cartpole'
elif config.game == 'dm-cartpole-s':
tasks = ['swingup', 'swingup_sparse']
config.game = 'dm-cartpole'
elif config.game == 'dm-fish':
tasks = ['upright', 'downleft']
elif config.game == 'dm-hopper':
tasks = ['stand', 'hop']
elif config.game == 'dm-acrobot':
tasks = ['swingup', 'swingup_sparse']
elif config.game == 'dm-manipulator':
tasks = ['bring_ball', 'bring_peg']
elif config.game == 'dm-cheetah':
tasks = ['run', 'backward']
else:
raise NotImplementedError
games = ['%s-%s' % (config.game, t) for t in tasks]
config.tasks = [Task(g, num_envs=config.num_workers) for g in games]
config.game = games[0]
def ppo_continuous(**kwargs):
generate_tag(kwargs)
kwargs.setdefault('log_level', 0)
config = Config()
config.merge(kwargs)
config.num_workers = 9 # must greater than 3
config.single_process = True
config.task_fn = lambda: Task(
config.game,
num_envs=config.num_workers,
single_process=config.single_process)
config.eval_env = Task(config.game)
config.network_fn = lambda: GaussianActorCriticNet(
config.state_dim,
config.action_dim,
actor_body=FCBody(config.state_dim, gate=torch.tanh),
critic_body=FCBody(config.state_dim, gate=torch.tanh))
config.optimizer_fn = lambda params: torch.optim.Adam(params, 3e-4, eps=1e-5)
config.discount = 0.99
config.use_gae = True
config.gae_tau = 0.95
config.gradient_clip = 0.5
config.rollout_length = 2048
config.optimization_epochs = 10
config.mini_batch_size = 64
config.ppo_ratio_clip = 0.2
config.log_interval = 2048
config.max_steps = 1e6
config.state_normalizer = MeanStdNormalizer()
run_steps(PPOAgent(config))
# PPOC
def ppoc_continuous(**kwargs):
kwargs['algo_tag'] = 'PPOC'
generate_tag(kwargs)
kwargs.setdefault('log_level', 0)
kwargs.setdefault('num_o', 4)
kwargs.setdefault('gate', nn.ReLU())
kwargs.setdefault('entropy_weight', 0.01)
kwargs.setdefault('tasks', False)
kwargs.setdefault('max_steps', 2e6)
config = Config()
config.merge(kwargs)
if config.tasks:
set_tasks(config)
if 'dm-humanoid' in config.game:
hidden_units = (128, 128)
else:
hidden_units = (64, 64)
config.num_workers = 9 # must greater than 3
config.single_process = True
config.task_fn = lambda: Task(
config.game,
num_envs=config.num_workers,
single_process=config.single_process)
config.eval_env = Task(config.game)
config.network_fn = lambda: OptionGaussianActorCriticNet(
config.state_dim,
config.action_dim,
num_options=config.num_o,
actor_body=FCBody(
config.state_dim, hidden_units=hidden_units, gate=config.gate),
critic_body=FCBody(
config.state_dim, hidden_units=hidden_units, gate=config.gate),
option_body_fn=lambda: FCBody(
config.state_dim, hidden_units=hidden_units, gate=config.gate),
)
config.optimizer_fn = lambda params: torch.optim.Adam(params, 3e-4, eps=1e-5)
config.beta_reg = 0.01
config.discount = 0.99
config.use_gae = True
config.gae_tau = 0.95
config.gradient_clip = 0.5
config.rollout_length = 30
config.optimization_epochs = 10
config.mini_batch_size = 64
config.ppo_ratio_clip = 0.2
config.log_interval = 2048
config.state_normalizer = MeanStdNormalizer()
run_steps(PPOCLSTMAgent(config))
def ppoc_lstm_continuous(**kwargs):
# debug:
# kwargs={'game':game}
kwargs['algo_tag'] = 'PPOC_LSTM'
generate_tag(kwargs)
kwargs.setdefault('log_level', 0)
kwargs.setdefault('learning', 'all')
kwargs.setdefault('tasks', False)
config = Config()
config.merge(kwargs)
config.num_workers = 9
config.single_process = True
config.task_fn = lambda: Task(
config.game,
num_envs=config.num_workers,
single_process=config.single_process)
config.eval_env = Task(config.game)
config.num_o = 4
if 'dm-humanoid' in config.game:
hidden_units = (128, 128)
else:
hidden_units = (64, 64)
config.gate = nn.ReLU()
# lstm parameters
config.debug=False
config.hid_dim = 64
config.num_lstm_layers = 1
config.lstm_to_fc_feat_dim = config.num_lstm_layers * config.hid_dim
config.bi_direction = True
if config.bi_direction:
config.lstm_to_fc_feat_dim = config.lstm_to_fc_feat_dim * 2
config.lstm_dropout = 0
config.network_fn = lambda: OptionLstmGaussianActorCriticNet(
config.state_dim,
config.action_dim,
num_options=config.num_o,
hid_dim=config.hid_dim,
phi_body=DummyBody(config.state_dim),
option_body_fn=lambda: FCBody(
config.state_dim,
hidden_units=hidden_units,
gate=config.gate),
config=config)
config.optimizer_fn = lambda params: torch.optim.Adam(params, 3e-4, eps=1e-5)
config.gradient_clip = 0.5
config.discount = 0.99
config.rollout_length = 32
config.max_steps = 1e9
config.state_normalizer = MeanStdNormalizer()
config.log_interval = 2048
# PPO params
# training params
config.optimization_epochs = 10
config.mini_batch_size = 64
# model params
config.use_gae = True
config.gae_tau = 0.95
config.ppo_ratio_clip = 0.2
config.entropy_weight = 0.01
# OC params
config.beta_reg = 0.01
config.delib_cost = 0.01
run_steps(PPOCLSTMAgent(config))
def ppo_lstm_continuous(**kwargs):
kwargs['algo_tag'] = 'PPO_LSTM'
generate_tag(kwargs)
kwargs.setdefault('log_level', 0)
config = Config()
config.merge(kwargs)
config.num_workers = 9 # must greater than 3
config.single_process = True
config.task_fn = lambda: Task(
config.game,
num_envs=config.num_workers,
single_process=config.single_process)
config.eval_env = Task(config.game)
# lstm parameters
config.hid_dim = 64
config.num_lstm_layers = 1
config.lstm_to_fc_feat_dim = config.num_lstm_layers * config.hid_dim
config.bi_direction = True
if config.bi_direction:
config.lstm_to_fc_feat_dim = config.lstm_to_fc_feat_dim * 2
config.lstm_dropout = 0
config.network_fn = lambda: LstmActorCriticNet(
config.state_dim,
config.action_dim,
config.hid_dim,
actor_body=FCBody(config.lstm_to_fc_feat_dim, gate=torch.tanh),
critic_body=FCBody(config.lstm_to_fc_feat_dim, gate=torch.tanh),
config=config)
config.optimizer_fn = lambda params: torch.optim.Adam(params, 3e-4, eps=1e-5)
config.gradient_clip = 0.5
config.discount = 0.99
config.rollout_length = 32
config.max_steps = 1e9
config.state_normalizer = MeanStdNormalizer()
config.log_interval = 2048
# PPO params
# training params
config.optimization_epochs = 10
config.mini_batch_size = 64
# model params
config.use_gae = True
config.gae_tau = 0.95
config.ppo_ratio_clip = 0.2
config.entropy_weight = 0.01
run_steps(PPOAgent(config))
# if __name__ == '__main__':
if True:
# use tuna to profile:
# python -m cProfile -o program.prof run_ppoc.py
mkdir('log/oc')
mkdir('tf_log/oc')
mkdir('data')
set_one_thread()
random_seed()
# select_device(-1)
select_device(0)
env_list = [
'RoboschoolHopper-v1', 'RoboschoolWalker2d-v1',
'RoboschoolHalfCheetah-v1', 'RoboschoolAnt-v1', 'RoboschoolHumanoid-v1'
]
game = 'CartPole-v0'
# dqn_feature(game=game)
# quantile_regression_dqn_feature(game=game)
# categorical_dqn_feature(game=game)
# a2c_feature(game=game)
# n_step_dqn_feature(game=game)
# option_critic_feature(game=game)
# ppo_feature(game=game)
# game = 'HalfCheetah-v2'
game = 'RoboschoolHopper-v1'
# game = 'BipedalWalkerHardcore-v2'
game = 'LunarLanderContinuous-v2'
# oc_continuous(game=game)
# doc_continuous(game=game)
# a2c_continuous(game=game)
# ppo_lstm_continuous(game=game)
# ppo_continuous(game=game)
# ddpg_continuous(game=game)
# td3_continuous(game=game)
ppo_lstm_continuous(game=game)
ppoc_lstm_continuous(game=game)
ppoc_continuous(game=game)
game = 'BreakoutNoFrameskip-v4'
# dqn_pixel(game=game)
# quantile_regression_dqn_pixel(game=game)
# categorical_dqn_pixel(game=game)
# a2c_pixel(game=game)
# n_step_dqn_pixel(game=game)
# option_critic_pixel(game=game)
# ppo_pixel(game=game)