-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_oc.py
242 lines (202 loc) · 7.3 KB
/
run_oc.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
#######################################################################
# Copyright (C) 2017 Shangtong Zhang([email protected]) #
# Permission given to modify the code as long as you keep this #
# declaration at the top #
#######################################################################
from deep_rl import *
def show_uncontained_keys(config):
contained_keys = set(config.__dict__.keys())
uncontained_keys = dir(config)
uncontained_keys = {k for k in uncontained_keys if not k.startswith('_')}
missing_keys = uncontained_keys - contained_keys
print(missing_keys)
def sort_config_keys(config):
kv = config.__dict__
sk = sorted(kv)
sorted_kv = ['%s: %s' % (k, kv[k]) for k in sk]
print(sorted_kv)
# Option-Critic
def option_critic_feature(**kwargs):
# kwargs = {'game': 'CartPole-v0'}
generate_tag(kwargs)
kwargs.setdefault('log_level', 0)
config = Config()
config.merge(kwargs)
config.num_workers = 6
config.task_fn = lambda: Task(config.game, num_envs=config.num_workers)
config.eval_env = Task(config.game)
config.optimizer_fn = lambda params: torch.optim.RMSprop(params, 0.001)
# FCBody: 2 layers FC net with ReLU gate. (4,64 -> 64,64)
config.network_fn = lambda: OptionCriticNet(
FCBody(config.state_dim), config.action_dim, num_options=7)
config.random_option_prob = LinearSchedule(1.0, 0.1, 1e4)
config.discount = 0.99
config.target_network_update_freq = 200
config.rollout_length = 10
config.termination_regularizer = 0.01
config.entropy_weight = 0.01
config.gradient_clip = 5
run_steps(OptionCriticAgent(config))
def option_critic_pixel(**kwargs):
generate_tag(kwargs)
kwargs.setdefault('log_level', 0)
config = Config()
config.merge(kwargs)
config.task_fn = lambda: Task(config.game, num_envs=config.num_workers)
config.eval_env = Task(config.game)
config.num_workers = 16
config.optimizer_fn = lambda params: torch.optim.RMSprop(
params, lr=7e-4, alpha=0.99, eps=1e-5)
config.network_fn = lambda: OptionCriticNet(
NatureConvBody(), config.action_dim, num_options=4)
config.random_option_prob = LinearSchedule(0.1)
config.state_normalizer = ImageNormalizer()
config.reward_normalizer = SignNormalizer()
config.discount = 0.99
config.target_network_update_freq = 10000
config.rollout_length = 5
config.gradient_clip = 5
config.max_steps = int(10e7)
config.entropy_weight = 0.01
config.termination_regularizer = 0.01
run_steps(OptionCriticAgent(config))
# PPO
def ppo_feature(**kwargs):
generate_tag(kwargs)
kwargs.setdefault('log_level', 0)
config = Config()
config.merge(kwargs)
config.num_workers = 5
config.task_fn = lambda: Task(config.game, num_envs=config.num_workers)
config.eval_env = Task(config.game)
config.optimizer_fn = lambda params: torch.optim.RMSprop(params, 0.001)
config.network_fn = lambda: CategoricalActorCriticNet(
config.state_dim, config.action_dim, FCBody(config.state_dim))
config.discount = 0.99
config.use_gae = True
config.gae_tau = 0.95
config.entropy_weight = 0.01
config.gradient_clip = 5
config.rollout_length = 128
config.optimization_epochs = 10
config.mini_batch_size = 32 * 5
config.ppo_ratio_clip = 0.2
config.log_interval = 128 * 5 * 10
run_steps(PPOAgent(config))
def ppo_pixel(**kwargs):
generate_tag(kwargs)
kwargs.setdefault('log_level', 0)
config = Config()
config.merge(kwargs)
config.task_fn = lambda: Task(config.game, num_envs=config.num_workers)
config.eval_env = Task(config.game)
config.num_workers = 8
config.optimizer_fn = lambda params: torch.optim.RMSprop(
params, lr=0.00025, alpha=0.99, eps=1e-5)
config.network_fn = lambda: CategoricalActorCriticNet(
config.state_dim, config.action_dim, NatureConvBody())
config.state_normalizer = ImageNormalizer()
config.reward_normalizer = SignNormalizer()
config.discount = 0.99
config.use_gae = True
config.gae_tau = 0.95
config.entropy_weight = 0.01
config.gradient_clip = 0.5
config.rollout_length = 128
config.optimization_epochs = 3
config.mini_batch_size = 32 * 8
config.ppo_ratio_clip = 0.1
config.log_interval = 128 * 8
config.max_steps = int(2e7)
run_steps(PPOAgent(config))
def ppo_continuous(**kwargs):
generate_tag(kwargs)
kwargs.setdefault('log_level', 0)
config = Config()
config.merge(kwargs)
config.num_workers = 8
config.task_fn = lambda: Task(config.game, num_envs=config.num_workers)
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.gradient_clip = 0.5
config.discount = 0.99
config.rollout_length = 2048
config.optimization_epochs = 10
config.mini_batch_size = 64
config.max_steps = 1e6
config.state_normalizer = MeanStdNormalizer()
config.log_interval = 2048
# PPO Params
config.use_gae = True
config.gae_tau = 0.95
config.ppo_ratio_clip = 0.2
run_steps(PPOAgent(config))
# Option-Critic
def option_critic_continuous(**kwargs):
# kwargs = {'game': 'CartPole-v0'}
generate_tag(kwargs)
kwargs.setdefault('log_level', 0)
config = Config()
config.merge(kwargs)
config.num_workers = 16
config.task_fn = lambda: Task(config.game, num_envs=config.num_workers)
config.eval_env = Task(config.game)
# FCBody: 2 layers FC net with ReLU gate. (4,64 -> 64,64)
config.network_fn = lambda: OptionCriticGaussianNet(
FCBody(config.state_dim), config.action_dim, num_options=2)
config.optimizer_fn = lambda params: torch.optim.Adam(params, 3e-4, eps=1e-5)
config.gradient_clip = 0.5
# config.optimizer_fn = lambda params: torch.optim.RMSprop(params, 0.001)
# config.gradient_clip = 5
config.discount = 0.99
config.rollout_length = 50
# config.optimization_epochs = 10
# config.mini_batch_size = 64
config.max_steps = 1e8
config.state_normalizer = MeanStdNormalizer()
config.log_interval = 2
# OC Params
# config.reward_normalizer = SignNormalizer()
config.target_network_update_freq = 200
config.random_option_prob = LinearSchedule(1.0, 0.1, 1e4)
config.termination_regularizer = 0.01
config.entropy_weight = 0.01
run_steps(OptionCriticContinuousAgent(config))
if __name__ == '__main__':
mkdir('log')
mkdir('tf_log')
set_one_thread()
random_seed()
# select_device(-1) # use cpu
select_device(0)
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 = 'Hopper-v2'
game = 'LunarLanderContinuous-v2'
game = 'BipedalWalkerHardcore-v2'
# a2c_continuous(game=game)
option_critic_continuous(game=game)
# ppo_continuous(game=game)
# ddpg_continuous(game=game)
# td3_continuous(game=game)
game = 'BreakoutNoFrameskip-v4'
game = 'MsPacmanNoFrameskip-v0'
# 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)