-
Notifications
You must be signed in to change notification settings - Fork 206
/
hyperparams_opt.py
406 lines (342 loc) · 15.2 KB
/
hyperparams_opt.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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
from copy import deepcopy
import numpy as np
import optuna
from optuna.pruners import SuccessiveHalvingPruner, MedianPruner
from optuna.samplers import RandomSampler, TPESampler
from optuna.integration.skopt import SkoptSampler
from stable_baselines import SAC, DDPG, TD3
from stable_baselines.ddpg import AdaptiveParamNoiseSpec, NormalActionNoise, OrnsteinUhlenbeckActionNoise
from stable_baselines.common.vec_env import VecNormalize, VecEnv
from stable_baselines.her import HERGoalEnvWrapper
from stable_baselines.common.base_class import _UnvecWrapper
from .callbacks import TrialEvalCallback
def hyperparam_optimization(algo, model_fn, env_fn, n_trials=10, n_timesteps=5000, hyperparams=None,
n_jobs=1, sampler_method='random', pruner_method='halving',
seed=0, verbose=1):
"""
:param algo: (str)
:param model_fn: (func) function that is used to instantiate the model
:param env_fn: (func) function that is used to instantiate the env
:param n_trials: (int) maximum number of trials for finding the best hyperparams
:param n_timesteps: (int) maximum number of timesteps per trial
:param hyperparams: (dict)
:param n_jobs: (int) number of parallel jobs
:param sampler_method: (str)
:param pruner_method: (str)
:param seed: (int)
:param verbose: (int)
:return: (pd.Dataframe) detailed result of the optimization
"""
# TODO: eval each hyperparams several times to account for noisy evaluation
# TODO: take into account the normalization (also for the test env -> sync obs_rms)
if hyperparams is None:
hyperparams = {}
n_startup_trials = 10
# test during 5 episodes
n_eval_episodes = 5
# evaluate every 20th of the maximum budget per iteration
n_evaluations = 20
eval_freq = int(n_timesteps / n_evaluations)
# n_warmup_steps: Disable pruner until the trial reaches the given number of step.
if sampler_method == 'random':
sampler = RandomSampler(seed=seed)
elif sampler_method == 'tpe':
sampler = TPESampler(n_startup_trials=n_startup_trials, seed=seed)
elif sampler_method == 'skopt':
# cf https://scikit-optimize.github.io/#skopt.Optimizer
# GP: gaussian process
# Gradient boosted regression: GBRT
sampler = SkoptSampler(skopt_kwargs={'base_estimator': "GP", 'acq_func': 'gp_hedge'})
else:
raise ValueError('Unknown sampler: {}'.format(sampler_method))
if pruner_method == 'halving':
pruner = SuccessiveHalvingPruner(min_resource=1, reduction_factor=4, min_early_stopping_rate=0)
elif pruner_method == 'median':
pruner = MedianPruner(n_startup_trials=n_startup_trials, n_warmup_steps=n_evaluations // 3)
elif pruner_method == 'none':
# Do not prune
pruner = MedianPruner(n_startup_trials=n_trials, n_warmup_steps=n_evaluations)
else:
raise ValueError('Unknown pruner: {}'.format(pruner_method))
if verbose > 0:
print("Sampler: {} - Pruner: {}".format(sampler_method, pruner_method))
study = optuna.create_study(sampler=sampler, pruner=pruner)
algo_sampler = HYPERPARAMS_SAMPLER[algo]
def objective(trial):
kwargs = hyperparams.copy()
trial.model_class = None
if algo == 'her':
trial.model_class = hyperparams['model_class']
# Hack to use DDPG/TD3 noise sampler
if algo in ['ddpg', 'td3'] or trial.model_class in ['ddpg', 'td3']:
trial.n_actions = env_fn(n_envs=1).action_space.shape[0]
kwargs.update(algo_sampler(trial))
model = model_fn(**kwargs)
eval_env = env_fn(n_envs=1, eval_env=True)
# Account for parallel envs
eval_freq_ = eval_freq
if isinstance(model.get_env(), VecEnv):
eval_freq_ = max(eval_freq // model.get_env().num_envs, 1)
# TODO: use non-deterministic eval for Atari?
eval_callback = TrialEvalCallback(eval_env, trial, n_eval_episodes=n_eval_episodes,
eval_freq=eval_freq_, deterministic=True)
if algo == 'her':
# Wrap the env if need to flatten the dict obs
if isinstance(eval_env, VecEnv):
eval_env = _UnvecWrapper(eval_env)
eval_env = HERGoalEnvWrapper(eval_env)
try:
model.learn(n_timesteps, callback=eval_callback)
# Free memory
model.env.close()
eval_env.close()
except AssertionError:
# Sometimes, random hyperparams can generate NaN
# Free memory
model.env.close()
eval_env.close()
raise optuna.exceptions.TrialPruned()
is_pruned = eval_callback.is_pruned
cost = -1 * eval_callback.last_mean_reward
del model.env, eval_env
del model
if is_pruned:
raise optuna.exceptions.TrialPruned()
return cost
try:
study.optimize(objective, n_trials=n_trials, n_jobs=n_jobs)
except KeyboardInterrupt:
pass
print('Number of finished trials: ', len(study.trials))
print('Best trial:')
trial = study.best_trial
print('Value: ', trial.value)
print('Params: ')
for key, value in trial.params.items():
print(' {}: {}'.format(key, value))
return study.trials_dataframe()
def sample_ppo2_params(trial):
"""
Sampler for PPO2 hyperparams.
:param trial: (optuna.trial)
:return: (dict)
"""
batch_size = trial.suggest_categorical('batch_size', [32, 64, 128, 256])
n_steps = trial.suggest_categorical('n_steps', [16, 32, 64, 128, 256, 512, 1024, 2048])
gamma = trial.suggest_categorical('gamma', [0.9, 0.95, 0.98, 0.99, 0.995, 0.999, 0.9999])
learning_rate = trial.suggest_loguniform('lr', 1e-5, 1)
ent_coef = trial.suggest_loguniform('ent_coef', 0.00000001, 0.1)
cliprange = trial.suggest_categorical('cliprange', [0.1, 0.2, 0.3, 0.4])
noptepochs = trial.suggest_categorical('noptepochs', [1, 5, 10, 20, 30, 50])
lam = trial.suggest_categorical('lamdba', [0.8, 0.9, 0.92, 0.95, 0.98, 0.99, 1.0])
if n_steps < batch_size:
nminibatches = 1
else:
nminibatches = int(n_steps / batch_size)
return {
'n_steps': n_steps,
'nminibatches': nminibatches,
'gamma': gamma,
'learning_rate': learning_rate,
'ent_coef': ent_coef,
'cliprange': cliprange,
'noptepochs': noptepochs,
'lam': lam
}
def sample_a2c_params(trial):
"""
Sampler for A2C hyperparams.
:param trial: (optuna.trial)
:return: (dict)
"""
gamma = trial.suggest_categorical('gamma', [0.9, 0.95, 0.98, 0.99, 0.995, 0.999, 0.9999])
n_steps = trial.suggest_categorical('n_steps', [8, 16, 32, 64, 128, 256, 512, 1024, 2048])
lr_schedule = trial.suggest_categorical('lr_schedule', ['linear', 'constant'])
learning_rate = trial.suggest_loguniform('lr', 1e-5, 1)
ent_coef = trial.suggest_loguniform('ent_coef', 0.00000001, 0.1)
vf_coef = trial.suggest_uniform('vf_coef', 0, 1)
# normalize = trial.suggest_categorical('normalize', [True, False])
# TODO: take into account the normalization (also for the test env)
return {
'n_steps': n_steps,
'gamma': gamma,
'learning_rate': learning_rate,
'lr_schedule': lr_schedule,
'ent_coef': ent_coef,
'vf_coef': vf_coef
}
def sample_acktr_params(trial):
"""
Sampler for ACKTR hyperparams.
:param trial: (optuna.trial)
:return: (dict)
"""
gamma = trial.suggest_categorical('gamma', [0.9, 0.95, 0.98, 0.99, 0.995, 0.999, 0.9999])
n_steps = trial.suggest_categorical('n_steps', [16, 32, 64, 128, 256, 512, 1024, 2048])
lr_schedule = trial.suggest_categorical('lr_schedule', ['linear', 'constant'])
learning_rate = trial.suggest_loguniform('lr', 1e-5, 1)
ent_coef = trial.suggest_loguniform('ent_coef', 0.00000001, 0.1)
vf_coef = trial.suggest_uniform('vf_coef', 0, 1)
return {
'n_steps': n_steps,
'gamma': gamma,
'learning_rate': learning_rate,
'lr_schedule': lr_schedule,
'ent_coef': ent_coef,
'vf_coef': vf_coef
}
def sample_sac_params(trial):
"""
Sampler for SAC hyperparams.
:param trial: (optuna.trial)
:return: (dict)
"""
gamma = trial.suggest_categorical('gamma', [0.9, 0.95, 0.98, 0.99, 0.995, 0.999, 0.9999])
learning_rate = trial.suggest_loguniform('lr', 1e-5, 1)
batch_size = trial.suggest_categorical('batch_size', [16, 32, 64, 128, 256, 512])
buffer_size = trial.suggest_categorical('buffer_size', [int(1e4), int(1e5), int(1e6)])
learning_starts = trial.suggest_categorical('learning_starts', [0, 1000, 10000, 20000])
train_freq = trial.suggest_categorical('train_freq', [1, 10, 100, 300])
# gradient_steps takes too much time
# gradient_steps = trial.suggest_categorical('gradient_steps', [1, 100, 300])
gradient_steps = train_freq
ent_coef = trial.suggest_categorical('ent_coef', ['auto', 0.5, 0.1, 0.05, 0.01, 0.0001])
net_arch = trial.suggest_categorical('net_arch', ["small", "medium", "big"])
net_arch = {
'small': [64, 64],
'medium': [256, 256],
'big': [400, 300],
}[net_arch]
target_entropy = 'auto'
if ent_coef == 'auto':
target_entropy = trial.suggest_categorical('target_entropy', ['auto', -1, -10, -20, -50, -100])
return {
'gamma': gamma,
'learning_rate': learning_rate,
'batch_size': batch_size,
'buffer_size': buffer_size,
'learning_starts': learning_starts,
'train_freq': train_freq,
'gradient_steps': gradient_steps,
'ent_coef': ent_coef,
'target_entropy': target_entropy,
'policy_kwargs': dict(layers=net_arch)
}
def sample_td3_params(trial):
"""
Sampler for TD3 hyperparams.
:param trial: (optuna.trial)
:return: (dict)
"""
gamma = trial.suggest_categorical('gamma', [0.9, 0.95, 0.98, 0.99, 0.995, 0.999, 0.9999])
learning_rate = trial.suggest_loguniform('lr', 1e-5, 1)
batch_size = trial.suggest_categorical('batch_size', [16, 32, 64, 100, 128, 256, 512])
buffer_size = trial.suggest_categorical('buffer_size', [int(1e4), int(1e5), int(1e6)])
train_freq = trial.suggest_categorical('train_freq', [1, 10, 100, 1000, 2000])
gradient_steps = train_freq
noise_type = trial.suggest_categorical('noise_type', ['ornstein-uhlenbeck', 'normal'])
noise_std = trial.suggest_uniform('noise_std', 0, 1)
hyperparams = {
'gamma': gamma,
'learning_rate': learning_rate,
'batch_size': batch_size,
'buffer_size': buffer_size,
'train_freq': train_freq,
'gradient_steps': gradient_steps,
}
if noise_type == 'normal':
hyperparams['action_noise'] = NormalActionNoise(mean=np.zeros(trial.n_actions),
sigma=noise_std * np.ones(trial.n_actions))
elif noise_type == 'ornstein-uhlenbeck':
hyperparams['action_noise'] = OrnsteinUhlenbeckActionNoise(mean=np.zeros(trial.n_actions),
sigma=noise_std * np.ones(trial.n_actions))
return hyperparams
def sample_trpo_params(trial):
"""
Sampler for TRPO hyperparams.
:param trial: (optuna.trial)
:return: (dict)
"""
gamma = trial.suggest_categorical('gamma', [0.9, 0.95, 0.98, 0.99, 0.995, 0.999, 0.9999])
timesteps_per_batch = trial.suggest_categorical('timesteps_per_batch', [16, 32, 64, 128, 256, 512, 1024, 2048, 4096])
max_kl = trial.suggest_loguniform('max_kl', 0.000001, 1)
ent_coef = trial.suggest_loguniform('ent_coef', 0.00000001, 0.1)
lam = trial.suggest_categorical('lamdba', [0.8, 0.9, 0.92, 0.95, 0.98, 0.99, 1.0])
# cg_damping = trial.suggest_loguniform('cg_damping', 1e-5, 1)
cg_damping = 0.1
cg_iters = trial.suggest_categorical('cg_iters', [10, 15, 20, 30])
vf_stepsize = trial.suggest_loguniform('vf_stepsize', 1e-5, 1)
vf_iters = trial.suggest_categorical('vf_iters', [1, 3, 5, 10, 20])
return {
'gamma': gamma,
'timesteps_per_batch': timesteps_per_batch,
'max_kl': max_kl,
'entcoeff': ent_coef,
'lam': lam,
'cg_damping': cg_damping,
'cg_iters': cg_iters,
'vf_stepsize': vf_stepsize,
'vf_iters': vf_iters
}
def sample_ddpg_params(trial):
"""
Sampler for DDPG hyperparams.
:param trial: (optuna.trial)
:return: (dict)
"""
gamma = trial.suggest_categorical('gamma', [0.9, 0.95, 0.98, 0.99, 0.995, 0.999, 0.9999])
# actor_lr = trial.suggest_loguniform('actor_lr', 1e-5, 1)
# critic_lr = trial.suggest_loguniform('critic_lr', 1e-5, 1)
learning_rate = trial.suggest_loguniform('lr', 1e-5, 1)
batch_size = trial.suggest_categorical('batch_size', [16, 32, 64, 128, 256])
buffer_size = trial.suggest_categorical('memory_limit', [int(1e4), int(1e5), int(1e6)])
noise_type = trial.suggest_categorical('noise_type', ['ornstein-uhlenbeck', 'normal', 'adaptive-param'])
noise_std = trial.suggest_uniform('noise_std', 0, 1)
normalize_observations = trial.suggest_categorical('normalize_observations', [True, False])
normalize_returns = trial.suggest_categorical('normalize_returns', [True, False])
hyperparams = {
'gamma': gamma,
'actor_lr': learning_rate,
'critic_lr': learning_rate,
'batch_size': batch_size,
'memory_limit': buffer_size,
'normalize_observations': normalize_observations,
'normalize_returns': normalize_returns
}
if noise_type == 'adaptive-param':
hyperparams['param_noise'] = AdaptiveParamNoiseSpec(initial_stddev=noise_std,
desired_action_stddev=noise_std)
# Apply layer normalization when using parameter perturbation
hyperparams['policy_kwargs'] = dict(layer_norm=True)
elif noise_type == 'normal':
hyperparams['action_noise'] = NormalActionNoise(mean=np.zeros(trial.n_actions),
sigma=noise_std * np.ones(trial.n_actions))
elif noise_type == 'ornstein-uhlenbeck':
hyperparams['action_noise'] = OrnsteinUhlenbeckActionNoise(mean=np.zeros(trial.n_actions),
sigma=noise_std * np.ones(trial.n_actions))
return hyperparams
def sample_her_params(trial):
"""
Sampler for HER hyperparams.
:param trial: (optuna.trial)
:return: (dict)
"""
if trial.model_class == SAC:
hyperparams = sample_sac_params(trial)
elif trial.model_class == DDPG:
hyperparams = sample_ddpg_params(trial)
elif trial.model_class == TD3:
hyperparams = sample_td3_params(trial)
hyperparams['random_exploration'] = trial.suggest_uniform('random_exploration', 0, 1)
hyperparams['n_sampled_goal'] = trial.suggest_categorical('n_sampled_goal', [1, 2, 4, 6, 8])
return hyperparams
HYPERPARAMS_SAMPLER = {
'ppo2': sample_ppo2_params,
'sac': sample_sac_params,
'a2c': sample_a2c_params,
'trpo': sample_trpo_params,
'ddpg': sample_ddpg_params,
'her': sample_her_params,
'acktr': sample_acktr_params,
'td3': sample_td3_params
}