forked from syne-tune/syne-tune
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbaselines.py
460 lines (408 loc) · 15.6 KB
/
baselines.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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
# Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License").
# You may not use this file except in compliance with the License.
# A copy of the License is located at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# or in the "license" file accompanying this file. This file is distributed
# on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
# express or implied. See the License for the specific language governing
# permissions and limitations under the License.
from typing import Dict, Optional
import numpy as np
import logging
import copy
from syne_tune.optimizer.schedulers import (
FIFOScheduler,
HyperbandScheduler,
PopulationBasedTraining,
)
from syne_tune.optimizer.schedulers.multiobjective import MOASHA
from syne_tune.optimizer.schedulers.searchers.regularized_evolution import (
RegularizedEvolution,
)
from syne_tune.optimizer.schedulers.synchronous import (
SynchronousGeometricHyperbandScheduler,
GeometricDifferentialEvolutionHyperbandScheduler,
)
from syne_tune.optimizer.schedulers.transfer_learning import (
TransferLearningTaskEvaluations,
)
from syne_tune.try_import import (
try_import_blackbox_repository_message,
try_import_bore_message,
)
class RandomSearch(FIFOScheduler):
def __init__(self, config_space: dict, metric: str, **kwargs):
super(RandomSearch, self).__init__(
config_space=config_space,
metric=metric,
searcher="random",
**kwargs,
)
class GridSearch(FIFOScheduler):
def __init__(self, config_space: dict, metric: str, **kwargs):
super(GridSearch, self).__init__(
config_space=config_space,
metric=metric,
searcher="grid",
**kwargs,
)
class BayesianOptimization(FIFOScheduler):
def __init__(self, config_space: dict, metric: str, **kwargs):
super(BayesianOptimization, self).__init__(
config_space=config_space,
metric=metric,
searcher="bayesopt",
**kwargs,
)
def _assert_need_one(kwargs: dict, need_one: Optional[set] = None):
if need_one is None:
need_one = {"max_t", "max_resource_attr"}
assert need_one.intersection(kwargs.keys()), f"Need one of these: {need_one}"
class ASHA(HyperbandScheduler):
def __init__(self, config_space: dict, metric: str, resource_attr: str, **kwargs):
"""
One of `max_t`, `max_resource_attr` needs to be in `kwargs`. For
`type='promotion'`, the latter is more useful, see also
:class:`HyperbandScheduler`.
"""
_assert_need_one(kwargs)
super(ASHA, self).__init__(
config_space=config_space,
metric=metric,
searcher="random",
resource_attr=resource_attr,
**kwargs,
)
class MOBSTER(HyperbandScheduler):
def __init__(self, config_space: dict, metric: str, resource_attr: str, **kwargs):
"""
One of `max_t`, `max_resource_attr` needs to be in `kwargs`. For
`type='promotion'`, the latter is more useful, see also
:class:`HyperbandScheduler`.
MOBSTER can be run with different surrogate models. The model is selected
by `search_options["model"]` in `kwargs`. The default is `"gp_multitask"`
(jointly dependent multi-task GP model), another useful choice is
`"gp_independent"` (independent GP models at each rung level, with shared
ARD kernel).
"""
_assert_need_one(kwargs)
super(MOBSTER, self).__init__(
config_space=config_space,
metric=metric,
searcher="bayesopt",
resource_attr=resource_attr,
**kwargs,
)
class HyperTune(HyperbandScheduler):
def __init__(self, config_space: Dict, metric: str, resource_attr: str, **kwargs):
"""
One of `max_t`, `max_resource_attr` needs to be in `kwargs`. For
`type='promotion'`, the latter is more useful, see also
:class:`HyperbandScheduler`.
Hyper-Tune is a model-based variant of ASHA with more than one bracket.
It can be seen as extension of MOBSTER and can be used with the
"gp_independent" or "gp_multitask" model. It has a model-based way
to sample the bracket for every new trial, as well as an ensemble
predictive distribution. Our
implementation is based on:
Yang Li et al
Hyper-Tune: Towards Efficient Hyper-parameter Tuning at Scale
VLDB 2022
See also :class:`HyperTuneIndependentGPModel`.
"""
_assert_need_one(kwargs)
kwargs = copy.deepcopy(kwargs)
search_options = kwargs.get("search_options", dict())
k, v, supp = "model", "gp_independent", {"gp_independent", "gp_multitask"}
model = search_options.get(k, v)
assert model in supp, (
f"HyperTune does not support search_options['{k}'] = '{model}'"
f", must be in {supp}"
)
search_options[k] = model
k = "hypertune_distribution_num_samples"
num_samples = search_options.get(k, 50)
search_options[k] = num_samples
kwargs["search_options"] = search_options
num_brackets = kwargs.get("brackets", 4)
kwargs["brackets"] = num_brackets
super(HyperTune, self).__init__(
config_space=config_space,
metric=metric,
searcher="hypertune",
resource_attr=resource_attr,
**kwargs,
)
class PASHA(HyperbandScheduler):
def __init__(self, config_space: dict, metric: str, resource_attr: str, **kwargs):
"""
One of `max_t`, `max_resource_attr` needs to be in `kwargs`. The
latter is more useful, see also :class:`HyperbandScheduler`.
"""
_assert_need_one(kwargs)
super(PASHA, self).__init__(
config_space=config_space,
metric=metric,
searcher="random",
resource_attr=resource_attr,
type="pasha",
**kwargs,
)
class SyncHyperband(SynchronousGeometricHyperbandScheduler):
def __init__(
self,
config_space: dict,
metric: str,
resource_attr: str,
**kwargs,
):
"""
One of `max_resource_level`, `max_resource_attr` needs to be in
`kwargs`. The latter is more useful, see also
:class:`HyperbandScheduler`.
"""
_assert_need_one(kwargs, need_one={"max_resource_level", "max_resource_attr"})
super(SyncHyperband, self).__init__(
config_space=config_space,
metric=metric,
searcher="random",
resource_attr=resource_attr,
**kwargs,
)
class SyncBOHB(SynchronousGeometricHyperbandScheduler):
def __init__(
self,
config_space: dict,
metric: str,
resource_attr: str,
**kwargs,
):
"""
One of `max_resource_level`, `max_resource_attr` needs to be in
`kwargs`. The latter is more useful, see also
:class:`HyperbandScheduler`.
"""
_assert_need_one(kwargs, need_one={"max_resource_level", "max_resource_attr"})
super(SyncBOHB, self).__init__(
config_space=config_space,
metric=metric,
searcher="kde",
resource_attr=resource_attr,
**kwargs,
)
class DEHB(GeometricDifferentialEvolutionHyperbandScheduler):
def __init__(
self,
config_space: dict,
metric: str,
resource_attr: str,
**kwargs,
):
"""
One of `max_resource_level`, `max_resource_attr` needs to be in
`kwargs`. The latter is more useful, see also
:class:`HyperbandScheduler`.
"""
_assert_need_one(kwargs, need_one={"max_resource_level", "max_resource_attr"})
super(DEHB, self).__init__(
config_space=config_space,
metric=metric,
searcher="random",
resource_attr=resource_attr,
**kwargs,
)
class SyncMOBSTER(SynchronousGeometricHyperbandScheduler):
def __init__(
self,
config_space: dict,
metric: str,
resource_attr: str,
**kwargs,
):
"""
One of `max_resource_level`, `max_resource_attr` needs to be in
`kwargs`. The latter is more useful, see also
:class:`HyperbandScheduler`.
The default surrogate model is "gp_independent", different to async
MOBSTER.
"""
_assert_need_one(kwargs, need_one={"max_resource_level", "max_resource_attr"})
search_options = kwargs.get("search_options", dict())
if "model" not in search_options:
search_options["model"] = "gp_independent"
kwargs["search_options"] = search_options
super(SyncMOBSTER, self).__init__(
config_space=config_space,
metric=metric,
searcher="bayesopt",
resource_attr=resource_attr,
**kwargs,
)
class BORE(FIFOScheduler):
def __init__(self, config_space: dict, metric: str, mode: str, **kwargs):
try:
from syne_tune.optimizer.schedulers.searchers.bore import Bore
except ImportError:
logging.info(try_import_bore_message())
raise
super(BORE, self).__init__(
config_space=config_space,
metric=metric,
searcher=Bore(
config_space=config_space, metric=metric, mode=mode, **kwargs
),
mode=mode,
**kwargs,
)
class REA(FIFOScheduler):
def __init__(
self,
config_space: dict,
metric: str,
population_size: int = 100,
sample_size: int = 10,
**kwargs,
):
super(REA, self).__init__(
config_space=config_space,
metric=metric,
searcher=RegularizedEvolution(
config_space=config_space,
metric=metric,
population_size=population_size,
sample_size=sample_size,
**kwargs,
),
**kwargs,
)
class ConstrainedBayesianOptimization(FIFOScheduler):
def __init__(self, config_space: dict, metric: str, constraint_attr: str, **kwargs):
search_options = kwargs.get("search_options", dict())
kwargs["search_options"] = dict(search_options, constraint_attr=constraint_attr)
super(ConstrainedBayesianOptimization, self).__init__(
config_space=config_space,
metric=metric,
searcher="bayesopt_constrained",
**kwargs,
)
class ZeroShotTransfer(FIFOScheduler):
def __init__(
self,
config_space: dict,
transfer_learning_evaluations: Dict[str, TransferLearningTaskEvaluations],
metric: str,
mode: str = "min",
sort_transfer_learning_evaluations: bool = True,
use_surrogates: bool = False,
random_seed: Optional[int] = None,
**kwargs,
):
"""
A zero-shot transfer hyperparameter optimization method which jointly selects configurations that minimize the
average rank obtained on historic metadata (transfer_learning_evaluations).
Reference: Sequential Model-Free Hyperparameter Tuning.
Martin Wistuba, Nicolas Schilling, Lars Schmidt-Thieme.
IEEE International Conference on Data Mining (ICDM) 2015.
:param config_space: Configuration space for trial evaluation function.
:param transfer_learning_evaluations: Dictionary from task name to offline evaluations.
:param metric: Objective name to optimize, must be present in transfer learning evaluations.
:param mode: Whether to minimize (min) or maximize (max)
:param sort_transfer_learning_evaluations: Use False if the hyperparameters for each task in
transfer_learning_evaluations Are already in the same order. If set to True, hyperparameters are sorted.
:param use_surrogates: If the same configuration is not evaluated on all tasks, set this to true. This will
generate a set of configurations and will impute their performance using surrogate models.
:param random_seed: Used for randomly sampling candidates. Only used if use_surrogate is True.
"""
try:
from syne_tune.optimizer.schedulers.transfer_learning import zero_shot
except ImportError:
logging.info(try_import_blackbox_repository_message())
raise
super(ZeroShotTransfer, self).__init__(
config_space=config_space,
metric=metric,
searcher=zero_shot.ZeroShotTransfer(
config_space=config_space,
metric=metric,
mode=mode,
sort_transfer_learning_evaluations=sort_transfer_learning_evaluations,
random_seed=random_seed,
transfer_learning_evaluations=transfer_learning_evaluations,
use_surrogates=use_surrogates,
),
mode=mode,
**kwargs,
)
class ASHACTS(HyperbandScheduler):
def __init__(
self,
config_space: dict,
metric: str,
resource_attr: str,
transfer_learning_evaluations: Dict[str, TransferLearningTaskEvaluations],
mode: str = "min",
random_seed: Optional[int] = None,
**kwargs,
):
"""
Runs ASHA where the searcher is done with the transfer-learning method:
A Quantile-based Approach for Hyperparameter Transfer Learning.
David Salinas, Huibin Shen, Valerio Perrone. ICML 2020.
This is the Copula Thompson Sampling approach described in the paper where a surrogate is fitted on the
transfer learning data to predict mean/variance of configuration performance given a hyperparameter.
The surrogate is then sampled from and the best configurations are returned as next candidate to evaluate.
:param config_space:
:param metric:
:param resource_attr:
:param transfer_learning_evaluations:
:param mode:
:param random_seed:
:param kwargs:
"""
try:
from syne_tune.optimizer.schedulers.transfer_learning.quantile_based.quantile_based_searcher import (
QuantileBasedSurrogateSearcher,
)
except ImportError:
logging.info(try_import_blackbox_repository_message())
raise
super(ASHACTS, self).__init__(
config_space=config_space,
searcher=QuantileBasedSurrogateSearcher(
mode=mode,
config_space=config_space,
metric=metric,
transfer_learning_evaluations=transfer_learning_evaluations,
random_seed=random_seed
if random_seed
else np.random.randint(0, 2**32),
),
mode=mode,
metric=metric,
resource_attr=resource_attr,
**kwargs,
)
# dictionary that allows to also list baselines who don't need a wrapper class such as PBT.
baselines_dict = {
"Random Search": RandomSearch,
"Grid Search": GridSearch,
"Bayesian Optimization": BayesianOptimization,
"ASHA": ASHA,
"MOBSTER": MOBSTER,
"PASHA": PASHA,
"MOASHA": MOASHA,
"PBT": PopulationBasedTraining,
"BORE": BORE,
"REA": REA,
"SyncHyperband": SyncHyperband,
"SyncBOHB": SyncBOHB,
"DEHB": DEHB,
"SyncMOBSTER": SyncMOBSTER,
"ConstrainedBayesianOptimization": ConstrainedBayesianOptimization,
"ZeroShotTransfer": ZeroShotTransfer,
"ASHACTS": ASHACTS,
}