-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathbaselines.py
1339 lines (1149 loc) · 47.7 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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from typing import Dict, Optional, Any, List, Union
import logging
import copy
from functools import partial
from syne_tune.optimizer.schedulers import (
FIFOScheduler,
HyperbandScheduler,
)
from syne_tune.optimizer.schedulers.legacy_pbt import LegacyPopulationBasedTraining
from syne_tune.optimizer.schedulers.multiobjective import (
MOASHA,
NSGA2Searcher,
LinearScalarizedScheduler,
)
from syne_tune.optimizer.schedulers.multiobjective.legacy_multi_objective_regularized_evolution import (
LegacyMultiObjectiveRegularizedEvolution,
)
from syne_tune.optimizer.schedulers.searchers.bayesopt.models.estimator import Estimator
from syne_tune.optimizer.schedulers.searchers.legacy_regularized_evolution import (
LegacyRegularizedEvolution,
)
from syne_tune.optimizer.schedulers.synchronous import (
SynchronousGeometricHyperbandScheduler,
GeometricDifferentialEvolutionHyperbandScheduler,
)
from syne_tune.optimizer.schedulers.transfer_learning import (
TransferLearningTaskEvaluations,
)
from syne_tune.optimizer.schedulers.random_seeds import RandomSeedGenerator
from syne_tune.util import dict_get
logger = logging.getLogger(__name__)
def _random_seed_from_generator(random_seed: int) -> int:
"""
This helper makes sure that a searcher within
:class:`~syne_tune.optimizer.schedulers.FIFOScheduler` is seeded in the same
way whether it is created by the searcher factory, or by hand.
:param random_seed: Random seed for scheduler
:return: Random seed to be used for searcher created by hand
"""
return RandomSeedGenerator(random_seed)()
def _assert_searcher_must_be(kwargs: Dict[str, Any], name: str):
searcher = kwargs.get("searcher")
assert searcher is None or searcher == name, f"Must have searcher='{name}'"
class RandomSearch(FIFOScheduler):
"""Random search.
See :class:`~syne_tune.optimizer.schedulers.searchers.RandomSearcher`
for ``kwargs["search_options"]`` parameters.
:param config_space: Configuration space for evaluation function
:param metric: Name of metric to optimize
:param kwargs: Additional arguments to
:class:`~syne_tune.optimizer.schedulers.FIFOScheduler`
"""
def __init__(self, config_space: Dict[str, Any], metric: str, **kwargs):
searcher_name = "random"
_assert_searcher_must_be(kwargs, searcher_name)
super(RandomSearch, self).__init__(
config_space=config_space,
metric=metric,
searcher=searcher_name,
**kwargs,
)
class GridSearch(FIFOScheduler):
"""Grid search.
See :class:`~syne_tune.optimizer.schedulers.searchers.GridSearcher`
for ``kwargs["search_options"]`` parameters.
:param config_space: Configuration space for evaluation function
:param metric: Name of metric to optimize
:param kwargs: Additional arguments to
:class:`~syne_tune.optimizer.schedulers.FIFOScheduler`
"""
def __init__(self, config_space: Dict[str, Any], metric: str, **kwargs):
searcher_name = "grid"
_assert_searcher_must_be(kwargs, searcher_name)
super(GridSearch, self).__init__(
config_space=config_space,
metric=metric,
searcher=searcher_name,
**kwargs,
)
class BayesianOptimization(FIFOScheduler):
"""Gaussian process based Bayesian optimization.
See :class:`~syne_tune.optimizer.schedulers.searchers.GPFIFOSearcher`
for ``kwargs["search_options"]`` parameters.
:param config_space: Configuration space for evaluation function
:param metric: Name of metric to optimize
:param kwargs: Additional arguments to
:class:`~syne_tune.optimizer.schedulers.FIFOScheduler`
"""
def __init__(self, config_space: Dict[str, Any], metric: str, **kwargs):
searcher_name = "bayesopt"
_assert_searcher_must_be(kwargs, searcher_name)
super(BayesianOptimization, self).__init__(
config_space=config_space,
metric=metric,
searcher=searcher_name,
**kwargs,
)
def _assert_need_one(kwargs: Dict[str, Any], 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):
"""Asynchronous Sucessive Halving (ASHA).
One of ``max_t``, ``max_resource_attr`` needs to be in ``kwargs``. For
``type="promotion"``, the latter is more useful.
See also :class:`~syne_tune.optimizer.schedulers.HyperbandScheduler` for
``kwargs`` parameters.
:param config_space: Configuration space for evaluation function
:param metric: Name of metric to optimize
:param resource_attr: Name of resource attribute
:param kwargs: Additional arguments to :class:`~syne_tune.optimizer.schedulers.HyperbandScheduler`
"""
def __init__(
self, config_space: Dict[str, Any], metric: str, resource_attr: str, **kwargs
):
_assert_need_one(kwargs)
searcher_name = "random"
_assert_searcher_must_be(kwargs, searcher_name)
super(ASHA, self).__init__(
config_space=config_space,
metric=metric,
searcher=searcher_name,
resource_attr=resource_attr,
**kwargs,
)
class MOBSTER(HyperbandScheduler):
"""Model-based Asynchronous Multi-fidelity Optimizer (MOBSTER).
One of ``max_t``, ``max_resource_attr`` needs to be in ``kwargs``. For
``type="promotion"``, the latter is more useful, see also
:class:`~syne_tune.optimizer.schedulers.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).
See also:
* :class:`~syne_tune.optimizer.schedulers.HyperbandScheduler` for ``kwargs``
parameters
* :class:`~syne_tune.optimizer.schedulers.searchers.GPMultiFidelitySearcher`
for ``kwargs["search_options"]`` parameters
:param config_space: Configuration space for evaluation function
:param metric: Name of metric to optimize
:param resource_attr: Name of resource attribute
:param kwargs: Additional arguments to :class:`~syne_tune.optimizer.schedulers.HyperbandScheduler`
"""
def __init__(
self, config_space: Dict[str, Any], metric: str, resource_attr: str, **kwargs
):
_assert_need_one(kwargs)
searcher_name = "bayesopt"
_assert_searcher_must_be(kwargs, searcher_name)
super(MOBSTER, self).__init__(
config_space=config_space,
metric=metric,
searcher=searcher_name,
resource_attr=resource_attr,
**kwargs,
)
class HyperTune(HyperbandScheduler):
"""
One of ``max_t``, ``max_resource_attr`` needs to be in ``kwargs``. For
``type="promotion"``, the latter is more useful, see also
:class:`~syne_tune.optimizer.schedulers.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
``search_options["model"]`` in ``kwargs`` being ``"gp_independent"`` or
``"gp_multitask"``. It has a model-based way to sample the bracket for every
new trial, as well as an ensemble predictive distribution feeding into the
acquisition function. Our implementation is based on:
| Yang Li et al
| Hyper-Tune: Towards Efficient Hyper-parameter Tuning at Scale
| VLDB 2022
| https://arxiv.org/abs/2201.06834
See also:
* :class:`~syne_tune.optimizer.schedulers.HyperbandScheduler` for ``kwargs``
parameters
* :class:`~syne_tune.optimizer.schedulers.searchers.hypertune.HyperTuneSearcher`
for ``kwargs["search_options"]`` parameters
* :class:`~syne_tune.optimizer.schedulers.searchers.bayesopt.gpautograd.hypertune.gp_model.HyperTuneIndependentGPModel`
for implementation
:param config_space: Configuration space for evaluation function
:param metric: Name of metric to optimize
:param resource_attr: Name of resource attribute
:param kwargs: Additional arguments to :class:`~syne_tune.optimizer.schedulers.HyperbandScheduler`
"""
def __init__(self, config_space: Dict, metric: str, resource_attr: str, **kwargs):
_assert_need_one(kwargs)
searcher_name = "hypertune"
_assert_searcher_must_be(kwargs, searcher_name)
kwargs = copy.deepcopy(kwargs)
search_options = dict_get(kwargs, "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
kwargs["search_options"] = search_options
super(HyperTune, self).__init__(
config_space=config_space,
metric=metric,
searcher=searcher_name,
resource_attr=resource_attr,
**kwargs,
)
class DyHPO(HyperbandScheduler):
"""Dynamic Gray-Box Hyperparameter Optimization (DyHPO)
One of ``max_t``, ``max_resource_attr`` needs to be in ``kwargs``. The latter
is more useful (DyHPO is a pause-resume scheduler), see also
:class:`~syne_tune.optimizer.schedulers.HyperbandScheduler`.
DyHPO can be run with the same surrogate models as :class:`MOBSTER`, but
``search_options["model"] != "gp_independent"``. This is because DyHPO
requires extrapolation to resource levels without any data, which cannot
sensibly be done with independent GPs per resource level. Compared to
:class:`MOBSTER` or :class:`HyperTune`, DyHPO is typically run with linearly
spaced rung levels (the default being 1, 2, 3, ...). Decisions whether to
promote a paused trial are folded together with suggesting a new
configuration, both are model-based. Our implementation is based on
| Wistuba, M. and Kadra, A. and Grabocka, J.
| Dynamic and Efficient Gray-Box Hyperparameter Optimization for Deep Learning
| https://arxiv.org/abs/2202.09774
However, there are important differences:
* We do not implement their surrogate model based on a neural network kernel,
but instead just use the surrogate models we provide for :class:`MOBSTER` as
well
* We implement a hybrid of DyHPO with the asynchronous successive halving
rule for promoting trials, controlled by ``probability_sh``. With this
probability, we promote a trial via the SH rule. This mitigates the issue
that DyHPO tends to start many trials initially, because due to lack of any
data at higher rungs, the score values for promoting a trial are much worse
than those for starting a new one.
See :class:`~syne_tune.optimizer.schedulers.HyperbandScheduler` for ``kwargs``
parameters, and
:class:`~syne_tune.optimizer.schedulers.searchers.GPMultiFidelitySearcher`
for ``kwargs["search_options"]`` parameters. The following parameters are
most important for DyHPO:
* ``rung_increment`` (and ``grace_period``): These parameters determine the
rung level spacing. DyHPO is run with linearly spaced rung levels
:math:`r_{min} + k \nu`, where :math:`r_{min}` is ``grace_period`` and
:math:`\nu` is ``rung_increment``. The default is 2.
* ``probability_sh``: See comment. The smaller this probability, the closer
the method is to the published original, which tends to start many more
trials than promote paused ones. On the other hand, if this probability is
close to 1, you may as well run MOBSTER. The default is
:const:`~syne_tune.optimizer.schedulers.searchers.dyhpo.hyperband_dyhpo.DEFAULT_SH_PROBABILITY`.
* ``search_options["opt_skip_period"]``: DyHPO can be quite a bit slower
than MOBSTER, because the GP surrogate model is used more frequently. It
can be sped up a bit by changing ``opt_skip_period`` (general default is
1). The default here is 3.
:param config_space: Configuration space for evaluation function
:param metric: Name of metric to optimize
:param resource_attr: Name of resource attribute
:param probability_sh: See above
:param kwargs: Additional arguments to :class:`~syne_tune.optimizer.schedulers.HyperbandScheduler`
"""
def __init__(
self,
config_space: Dict[str, Any],
metric: str,
resource_attr: str,
probability_sh: Optional[float] = None,
**kwargs,
):
_assert_need_one(kwargs)
searcher_name = "dyhpo"
_assert_searcher_must_be(kwargs, searcher_name)
scheduler_type = kwargs.get("type")
assert (
scheduler_type is None or scheduler_type == "dyhpo"
), "Must have type='dyhpo'"
kwargs["type"] = "dyhpo"
if probability_sh is not None:
rung_system_kwargs = dict_get(kwargs, "rung_system_kwargs", dict())
rung_system_kwargs["probability_sh"] = probability_sh
kwargs["rung_system_kwargs"] = rung_system_kwargs
search_options = dict_get(kwargs, "search_options", dict())
k = "opt_skip_period"
if k not in search_options:
search_options[k] = 3
kwargs["search_options"] = search_options
if (
kwargs.get("reduction_factor") is None
and kwargs.get("rung_increment") is None
):
kwargs["rung_increment"] = 2
super(DyHPO, self).__init__(
config_space=config_space,
metric=metric,
searcher=searcher_name,
resource_attr=resource_attr,
**kwargs,
)
class PASHA(HyperbandScheduler):
"""Progressive ASHA.
One of ``max_t``, ``max_resource_attr`` needs to be in ``kwargs``. The latter is
more useful, see also :class:`~syne_tune.optimizer.schedulers.HyperbandScheduler`.
:param config_space: Configuration space for evaluation function
:param metric: Name of metric to optimize
:param resource_attr: Name of resource attribute
:param kwargs: Additional arguments to :class:`~syne_tune.optimizer.schedulers.HyperbandScheduler`
"""
def __init__(
self, config_space: Dict[str, Any], metric: str, resource_attr: str, **kwargs
):
_assert_need_one(kwargs)
super(PASHA, self).__init__(
config_space=config_space,
metric=metric,
searcher="random", # default, can be overwritten
resource_attr=resource_attr,
type="pasha",
**kwargs,
)
class BOHB(HyperbandScheduler):
"""Asynchronous BOHB
Combines :class:`ASHA` with TPE-like Bayesian optimization, using kernel
density estimators.
One of ``max_t``, ``max_resource_attr`` needs to be in ``kwargs``. For
``type="promotion"``, the latter is more useful, see also
:class:`~syne_tune.optimizer.schedulers.HyperbandScheduler`.
See
:class:`~syne_tune.optimizer.schedulers.searchers.kde.MultiFidelityKernelDensityEstimator`
for ``kwargs["search_options"]`` parameters, and
:class:`~syne_tune.optimizer.schedulers.HyperbandScheduler` for ``kwargs``
parameters.
:param config_space: Configuration space for evaluation function
:param metric: Name of metric to optimize
:param resource_attr: Name of resource attribute
:param kwargs: Additional arguments to :class:`~syne_tune.optimizer.schedulers.HyperbandScheduler`
"""
def __init__(
self, config_space: Dict[str, Any], metric: str, resource_attr: str, **kwargs
):
_assert_need_one(kwargs)
searcher_name = "kde"
_assert_searcher_must_be(kwargs, searcher_name)
super(BOHB, self).__init__(
config_space=config_space,
metric=metric,
searcher=searcher_name,
resource_attr=resource_attr,
**kwargs,
)
class SyncHyperband(SynchronousGeometricHyperbandScheduler):
"""Synchronous Hyperband.
One of ``max_resource_level``, ``max_resource_attr`` needs to be in ``kwargs``.
The latter is more useful, see also :class:`~syne_tune.optimizer.schedulers.HyperbandScheduler`.
If ``kwargs["brackets"]`` is not given, the maximum number of brackets is
used. Choose ``kwargs["brackets"] = 1`` for synchronous successive halving.
:param config_space: Configuration space for evaluation function
:param metric: Name of metric to optimize
:param resource_attr: Name of resource attribute
:param kwargs: Additional arguments to
:class:`~syne_tune.optimizer.schedulers.synchronous.SynchronousGeometricHyperbandScheduler`
"""
def __init__(
self,
config_space: Dict[str, Any],
metric: str,
resource_attr: str,
**kwargs,
):
_assert_need_one(kwargs, need_one={"max_resource_level", "max_resource_attr"})
super(SyncHyperband, self).__init__(
config_space=config_space,
metric=metric,
searcher="random", # default, can be overwritten
resource_attr=resource_attr,
**kwargs,
)
class SyncBOHB(SynchronousGeometricHyperbandScheduler):
"""Synchronous BOHB.
Combines :class:`SyncHyperband` with TPE-like Bayesian optimization, using
kernel density estimators.
One of ``max_resource_level``, ``max_resource_attr`` needs to be in ``kwargs``.
The latter is more useful, see also
:class:`~syne_tune.optimizer.schedulers.HyperbandScheduler`.
If ``kwargs["brackets"]`` is not given, the maximum number of brackets is
used. Choose ``kwargs["brackets"] = 1`` for synchronous successive halving.
:param config_space: Configuration space for evaluation function
:param metric: Name of metric to optimize
:param resource_attr: Name of resource attribute
:param kwargs: Additional arguments to
:class:`~syne_tune.optimizer.schedulers.synchronous.SynchronousGeometricHyperbandScheduler`
"""
def __init__(
self,
config_space: Dict[str, Any],
metric: str,
resource_attr: str,
**kwargs,
):
_assert_need_one(kwargs, need_one={"max_resource_level", "max_resource_attr"})
searcher_name = "kde"
_assert_searcher_must_be(kwargs, searcher_name)
super(SyncBOHB, self).__init__(
config_space=config_space,
metric=metric,
searcher=searcher_name,
resource_attr=resource_attr,
**kwargs,
)
class DEHB(GeometricDifferentialEvolutionHyperbandScheduler):
"""Differential Evolution Hyperband (DEHB).
Combines :class:`SyncHyperband` with ideas from evolutionary algorithms.
One of ``max_resource_level``, ``max_resource_attr`` needs to be in ``kwargs``.
The latter is more useful, see also :class:`~syne_tune.optimizer.schedulers.HyperbandScheduler`.
:param config_space: Configuration space for evaluation function
:param metric: Name of metric to optimize
:param resource_attr: Name of resource attribute
:param kwargs: Additional arguments to
:class:`~syne_tune.optimizer.schedulers.synchronous.SynchronousGeometricHyperbandScheduler`
"""
def __init__(
self,
config_space: Dict[str, Any],
metric: str,
resource_attr: str,
**kwargs,
):
_assert_need_one(kwargs, need_one={"max_resource_level", "max_resource_attr"})
super(DEHB, self).__init__(
config_space=config_space,
metric=metric,
searcher="random_encoded", # default, can be overwritten
resource_attr=resource_attr,
**kwargs,
)
class SyncMOBSTER(SynchronousGeometricHyperbandScheduler):
"""Synchronous MOBSTER.
Combines :class:`SyncHyperband` with Gaussian process based Bayesian
optimization, just like :class:`MOBSTER` builds on top of :class:`ASHA` in
the asynchronous case.
One of ``max_resource_level``, ``max_resource_attr`` needs to be in ``kwargs``.
The latter is more useful, see also
:class:`~syne_tune.optimizer.schedulers.HyperbandScheduler`.
If ``kwargs["brackets"]`` is not given, the maximum number of brackets is
used. Choose ``kwargs["brackets"] = 1`` for synchronous successive halving.
The default surrogate model (``search_options["model"]`` in ``kwargs``) is
``"gp_independent"``, different to :class:`MOBSTER`.
:param config_space: Configuration space for evaluation function
:param metric: Name of metric to optimize
:param resource_attr: Name of resource attribute
:param kwargs: Additional arguments to
:class:`~syne_tune.optimizer.schedulers.synchronous.SynchronousGeometricHyperbandScheduler`
"""
def __init__(
self,
config_space: Dict[str, Any],
metric: str,
resource_attr: str,
**kwargs,
):
_assert_need_one(kwargs, need_one={"max_resource_level", "max_resource_attr"})
searcher_name = "bayesopt"
_assert_searcher_must_be(kwargs, searcher_name)
search_options = dict_get(kwargs, "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=searcher_name,
resource_attr=resource_attr,
**kwargs,
)
def _create_searcher_kwargs(
config_space: Dict[str, Any],
metric: Union[str, List[str]],
random_seed: Optional[int],
kwargs: Dict[str, Any],
) -> Dict[str, Any]:
searcher_kwargs = dict(
config_space=config_space,
metric=metric,
points_to_evaluate=kwargs.get("points_to_evaluate"),
)
search_options = dict_get(kwargs, "search_options", dict())
searcher_kwargs.update(search_options)
if random_seed is not None:
searcher_kwargs["random_seed"] = _random_seed_from_generator(random_seed)
return searcher_kwargs
class BORE(FIFOScheduler):
"""Bayesian Optimization by Density-Ratio Estimation (BORE).
See :class:`~syne_tune.optimizer.schedulers.searchers.bore.Bore`
for ``kwargs["search_options"]`` parameters.
:param config_space: Configuration space for evaluation function
:param metric: Name of metric to optimize
:param random_seed: Random seed, optional
:param kwargs: Additional arguments to
:class:`~syne_tune.optimizer.schedulers.FIFOScheduler`
"""
def __init__(
self,
config_space: Dict[str, Any],
metric: str,
random_seed: Optional[int] = None,
**kwargs,
):
try:
from syne_tune.optimizer.schedulers.searchers.bore.legacy_bore import (
LegacyBore,
)
except ImportError:
raise
searcher_kwargs = _create_searcher_kwargs(
config_space, metric, random_seed, kwargs
)
super(BORE, self).__init__(
config_space=config_space,
metric=metric,
searcher=LegacyBore(**searcher_kwargs),
random_seed=random_seed,
**kwargs,
)
class ASHABORE(HyperbandScheduler):
"""Model-based ASHA with BORE searcher
See :class:`~syne_tune.optimizer.schedulers.searchers.bore.MultiFidelityBore`
for ``kwargs["search_options"]`` parameters.
:param config_space: Configuration space for evaluation function
:param metric: Name of metric to optimize
:param resource_attr: Name of resource attribute
:param random_seed: Random seed, optional
:param kwargs: Additional arguments to
:class:`~syne_tune.optimizer.schedulers.FIFOScheduler`
"""
def __init__(
self,
config_space: Dict[str, Any],
metric: str,
resource_attr: str,
random_seed: Optional[int] = None,
**kwargs,
):
try:
from syne_tune.optimizer.schedulers.searchers.bore.legacy_multi_fidelity_bore import (
LegacyMultiFidelityBore,
)
except ImportError:
raise
searcher_kwargs = _create_searcher_kwargs(
config_space, metric, random_seed, kwargs
)
searcher_kwargs["resource_attr"] = resource_attr
searcher_kwargs["mode"] = kwargs.get("mode")
super(ASHABORE, self).__init__(
config_space=config_space,
metric=metric,
searcher=LegacyMultiFidelityBore(**searcher_kwargs),
resource_attr=resource_attr,
random_seed=random_seed,
**kwargs,
)
class BoTorch(FIFOScheduler):
"""Bayesian Optimization using BoTorch
See :class:`~syne_tune.optimizer.schedulers.searchers.botorch.BoTorchSearcher`
for ``kwargs["search_options"]`` parameters.
:param config_space: Configuration space for evaluation function
:param metric: Name of metric to optimize
:param random_seed: Random seed, optional
:param kwargs: Additional arguments to
:class:`~syne_tune.optimizer.schedulers.FIFOScheduler`
"""
def __init__(
self,
config_space: Dict[str, Any],
metric: str,
random_seed: Optional[int] = None,
**kwargs,
):
try:
from syne_tune.optimizer.schedulers.searchers.botorch.legacy_botorch_searcher import (
LegacyBoTorchSearcher,
)
except ImportError:
raise
searcher_kwargs = _create_searcher_kwargs(
config_space, metric, random_seed, kwargs
)
super(BoTorch, self).__init__(
config_space=config_space,
metric=metric,
searcher=LegacyBoTorchSearcher(**searcher_kwargs),
random_seed=random_seed,
**kwargs,
)
class REA(FIFOScheduler):
"""Regularized Evolution (REA).
See :class:`~syne_tune.optimizer.schedulers.searchers.regularized_evolution.RegularizedEvolution`
for ``kwargs["search_options"]`` parameters.
:param config_space: Configuration space for evaluation function
:param metric: Name of metric to optimize
:param population_size: See
:class:`~syne_tune.optimizer.schedulers.searchers.RegularizedEvolution`.
Defaults to 100
:param sample_size: See
:class:`~syne_tune.optimizer.schedulers.searchers.RegularizedEvolution`.
Defaults to 10
:param random_seed: Random seed, optional
:param kwargs: Additional arguments to
:class:`~syne_tune.optimizer.schedulers.FIFOScheduler`
"""
def __init__(
self,
config_space: Dict[str, Any],
metric: str,
population_size: int = 100,
sample_size: int = 10,
random_seed: Optional[int] = None,
**kwargs,
):
searcher_kwargs = _create_searcher_kwargs(
config_space, metric, random_seed, kwargs
)
searcher_kwargs["population_size"] = population_size
searcher_kwargs["sample_size"] = sample_size
super(REA, self).__init__(
config_space=config_space,
metric=metric,
searcher=LegacyRegularizedEvolution(**searcher_kwargs),
random_seed=random_seed,
**kwargs,
)
def create_gaussian_process_estimator(
config_space: Dict[str, Any],
metric: str,
random_seed: Optional[int] = None,
search_options: Optional[Dict[str, Any]] = None,
) -> Estimator:
scheduler = BayesianOptimization(
config_space=config_space,
metric=metric,
random_seed=random_seed,
search_options=search_options,
)
searcher = scheduler.searcher # GPFIFOSearcher
state_transformer = searcher.state_transformer # ModelStateTransformer
estimator = state_transformer.estimator # GaussProcEmpiricalBayesEstimator
# update the estimator properties
estimator.active_metric = metric
return estimator
class MORandomScalarizationBayesOpt(FIFOScheduler):
"""
Uses :class:`~syne_tune.optimizer.schedulers.multiobjective.MultiObjectiveMultiSurrogateSearcher`
with one standard GP surrogate model per metric (same as in
:class:`BayesianOptimization`, together with the
:class:`~syne_tune.optimizer.schedulers.multiobjective.MultiObjectiveLCBRandomLinearScalarization`
acquisition function.
If `estimators` is given, surrogate models are taken from there, and the
default is used otherwise. This is useful if you have a good low-variance
model for one of the objectives.
:param config_space: Configuration space for evaluation function
:param metric: Name of metrics to optimize
:param mode: Modes of optimization. Defaults to "min" for all
:param random_seed: Random seed, optional
:param estimators: Use these surrogate models instead of the default GP
one. Optional
:param kwargs: Additional arguments to
:class:`~syne_tune.optimizer.schedulers.FIFOScheduler`. Here,
``kwargs["search_options"]`` is used to create the searcher and its
GP surrogate models.
"""
def __init__(
self,
config_space: Dict[str, Any],
metric: List[str],
mode: Union[List[str], str] = "min",
random_seed: Optional[int] = None,
estimators: Optional[Dict[str, Estimator]] = None,
**kwargs,
):
try:
from syne_tune.optimizer.schedulers.multiobjective import (
MultiObjectiveMultiSurrogateSearcher,
MultiObjectiveLCBRandomLinearScalarization,
)
except ImportError:
raise
searcher_kwargs = _create_searcher_kwargs(
config_space, metric, random_seed, kwargs
)
if estimators is None:
estimators = dict()
else:
estimators = estimators.copy()
if isinstance(mode, str):
mode = [mode] * len(metric)
if "search_options" in kwargs:
search_options = kwargs["search_options"].copy()
else:
search_options = dict()
search_options["no_fantasizing"] = True
for _metric in metric:
if _metric not in estimators:
estimators[_metric] = create_gaussian_process_estimator(
config_space=config_space,
metric=_metric,
search_options=search_options,
)
# Note: ``mode`` is dealt with in the ``update`` method of the MO
# searcher, by converting the metrics. Internally, all metrics are
# minimized
searcher = MultiObjectiveMultiSurrogateSearcher(
estimators=estimators,
mode=mode,
scoring_class=partial(
MultiObjectiveLCBRandomLinearScalarization, random_seed=random_seed
),
**searcher_kwargs,
)
super().__init__(
config_space=config_space,
metric=metric,
mode=mode,
searcher=searcher,
random_seed=random_seed,
**kwargs,
)
class NSGA2(FIFOScheduler):
"""
See :class:`~syne_tune.optimizer.schedulers.searchers.RandomSearcher`
for ``kwargs["search_options"]`` parameters.
:param config_space: Configuration space for evaluation function
:param metric: Name of metric to optimize
:param population_size: The size of the population for NSGA-2
:param random_seed: Random seed, optional
:param kwargs: Additional arguments to
:class:`~syne_tune.optimizer.schedulers.FIFOScheduler`
"""
def __init__(
self,
config_space: Dict[str, Any],
metric: List[str],
mode: Union[List[str], str] = "min",
population_size: int = 20,
random_seed: Optional[int] = None,
**kwargs,
):
searcher_kwargs = _create_searcher_kwargs(
config_space, metric, random_seed, kwargs
)
searcher_kwargs["mode"] = mode
searcher_kwargs["population_size"] = population_size
super(NSGA2, self).__init__(
config_space=config_space,
metric=metric,
mode=mode,
searcher=NSGA2Searcher(**searcher_kwargs),
random_seed=random_seed,
**kwargs,
)
class MOREA(FIFOScheduler):
"""
See :class:`~syne_tune.optimizer.schedulers.searchers.RandomSearcher`
for ``kwargs["search_options"]`` parameters.
:param config_space: Configuration space for evaluation function
:param metric: Name of metric to optimize
:param population_size: See
:class:`~syne_tune.optimizer.schedulers.searchers.RegularizedEvolution`.
Defaults to 100
:param sample_size: See
:class:`~syne_tune.optimizer.schedulers.searchers.RegularizedEvolution`.
Defaults to 10
:param random_seed: Random seed, optional
:param kwargs: Additional arguments to
:class:`~syne_tune.optimizer.schedulers.FIFOScheduler`
"""
def __init__(
self,
config_space: Dict[str, Any],
metric: List[str],
mode: Union[List[str], str] = "min",
population_size: int = 100,
sample_size: int = 10,
random_seed: Optional[int] = None,
**kwargs,
):
searcher_kwargs = _create_searcher_kwargs(
config_space, metric, random_seed, kwargs
)
searcher_kwargs["mode"] = mode
searcher_kwargs["population_size"] = population_size
searcher_kwargs["sample_size"] = sample_size
super(MOREA, self).__init__(
config_space=config_space,
metric=metric,
mode=mode,
searcher=LegacyMultiObjectiveRegularizedEvolution(**searcher_kwargs),
random_seed=random_seed,
**kwargs,
)
class MOLinearScalarizationBayesOpt(LinearScalarizedScheduler):
"""
Uses :class:`~syne_tune.optimizer.schedulers.multiobjective.LinearScalarizedScheduler`
together with a default GP surrogate model.
See :class:`~syne_tune.optimizer.schedulers.searchers.GPFIFOSearcher`
for ``kwargs["search_options"]`` parameters.
:param config_space: Configuration space for evaluation function
:param metric: Name of metric to optimize
:param scalarization_weights: Positive weight used for the scalarization.
Defaults to all 1
:param kwargs: Additional arguments to
:class:`~syne_tune.optimizer.schedulers.FIFOScheduler`
"""
def __init__(
self,
config_space: Dict[str, Any],
metric: List[str],
scalarization_weights: Optional[List[float]] = None,
**kwargs,
):
searcher_name = "bayesopt"
_assert_searcher_must_be(kwargs, searcher_name)
super().__init__(
config_space=config_space,
metric=metric,
scalarization_weights=scalarization_weights,
searcher=searcher_name,
**kwargs,
)
class ConstrainedBayesianOptimization(FIFOScheduler):
"""Constrained Bayesian Optimization.
See :class:`~syne_tune.optimizer.schedulers.searchers.constrained.ConstrainedGPFIFOSearcher`
for ``kwargs["search_options"]`` parameters.
:param config_space: Configuration space for evaluation function
:param metric: Name of metric to optimize
:param constraint_attr: Name of constraint metric
:param kwargs: Additional arguments to
:class:`~syne_tune.optimizer.schedulers.FIFOScheduler`
"""
def __init__(
self, config_space: Dict[str, Any], metric: str, constraint_attr: str, **kwargs
):
searcher_name = "bayesopt_constrained"
_assert_searcher_must_be(kwargs, searcher_name)
search_options = dict_get(kwargs, "search_options", dict())
kwargs["search_options"] = dict(search_options, constraint_attr=constraint_attr)
super(ConstrainedBayesianOptimization, self).__init__(
config_space=config_space,
metric=metric,
searcher=searcher_name,
**kwargs,
)
class ZeroShotTransfer(FIFOScheduler):
"""