Repository navigation
Expand file tree
/
Copy pathadvanced_tuning_methods_and_black_box_optimization.qmd
More file actions
1132 lines (899 loc) · 80.3 KB
/
Copy pathadvanced_tuning_methods_and_black_box_optimization.qmd
File metadata and controls
1132 lines (899 loc) · 80.3 KB
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
---
aliases:
- "/advanced_tuning_methods_and_black_box_optimization.html"
---
# Advanced Tuning Methods and Black Box Optimization {#sec-optimization-advanced}
{{< include ../../common/_setup.qmd >}}
`r chapter = "Advanced Tuning Methods and Black Box Optimization"`
`r authors(chapter)`
Having looked at the basic usage of `r mlr3tuning`, we will now turn to more advanced methods.
We will begin in @sec-tuning-errors by continuing to look at single-objective tuning but will consider what happens when experiments go wrong and how to prevent fatal errors\index{debugging}.
We will then extend the methodology from @sec-optimization to enable `r index('multi-objective tuning')`, where learners are optimized to multiple measures simultaneously, in @sec-multi-metrics-tuning we will demonstrate how this is handled relatively simply in `mlr3` by making use of the same classes and methods we have already used.
The final two sections focus on specific optimization methods.
@sec-hyperband looks in detail at multi-fidelity tuning and the Hyperband tuner, and then demonstrates it in practice with `r mlr3hyperband`.
Finally, @sec-bayesian-optimization takes a deep dive into black box Bayesian optimization.
This is a more theory-heavy section to motivate the design of the classes and methods in `r mlr3mbo`.
## Error Handling and Memory Management {#sec-tuning-errors}
In this section, we will look at how to use `mlr3` to ensure that tuning workflows are efficient and robust.
In particular, we will consider how to enable features that prevent fatal errors leading to irrecoverable data loss in the middle of an experiment, and then how to manage tuning experiments that may use up a lot of computer memory.
### Encapsulation and Fallback Learner {#sec-encapsulation-fallback}
Error handling is discussed in detail in @sec-error-handling, however, it is very important in the context of tuning so here we will just practically demonstrate how to make use of encapsulation and fallback learners and explain why they are essential during HPO.
Even in simple machine learning problems, there is a lot of potential for things to go wrong.
For example, when learners do not converge, run out of memory, or terminate with an error due to issues in the underlying data.
As a common issue, learners can fail if there are factor levels present in the test data that were not in the training data, models fail in this case as there have been no weights/coefficients trained for these new factor levels:
```{r advanced_tuning_methods_and_black_box_optimization-001, error = TRUE, warning = FALSE, message = FALSE}
tsk_pen = tsk("penguins")
# remove rows with missing values
tsk_pen$filter(tsk_pen$row_ids[complete.cases(tsk_pen$data())])
# create custom resampling with new factors in test data
rsmp_custom = rsmp("custom")
rsmp_custom$instantiate(tsk_pen,
list(tsk_pen$row_ids[tsk_pen$data()$island != "Torgersen"]),
list(tsk_pen$row_ids[tsk_pen$data()$island == "Torgersen"])
)
msr_ce = msr("classif.ce")
tnr_random = tnr("random_search")
learner = lrn("classif.lda", method = "t", nu = to_tune(3, 10))
tune(tnr_random, tsk_pen, learner, rsmp_custom, msr_ce, 10)
```
In the above example, we can see the tuning process breaks and we lose all information about the hyperparameter optimization process.
This is even worse in nested resampling or benchmarking when errors could cause us to lose all progress across multiple configurations or even learners and tasks.
`r index('Encapsulation')` (@sec-encapsulation) allows errors to be isolated and handled, without disrupting the tuning process.
We can tell a learner to encapsulate an error using the `$encapsulate()` method as follows:
```{r advanced_tuning_methods_and_black_box_optimization-002}
learner$encapsulate(method = "evaluate", fallback = lrn("classif.featureless"))
```
Note by passing `"evaluate"`, we are telling the learner to set up encapsulation in both the training and prediction stages (see @sec-error-handling for other encapsulation options).
Another common issue that cannot be easily solved during HPO is learners not converging and the process running indefinitely.
We can prevent this from happening by setting the `timeout` field in a learner, which signals the learner to stop if it has been running for that much time (in seconds), again this can be set for training and prediction individually:
```{r advanced_tuning_methods_and_black_box_optimization-003}
learner$timeout = c(train = 30, predict = 30)
```
Now if either an error occurs, or the model timeout threshold is reached, then instead of breaking, the learner will simply not make predictions when errors are found and the result is `NA` for resampling iterations with errors.
When this happens, our hyperparameter optimization experiment will fail as we cannot aggregate results across resampling iterations.
Therefore it is essential to select a `r index('fallback learner')` (@sec-fallback), which is a learner that will be fitted if the learner of interest fails.
A common approach is to use a featureless baseline\index{baselines} (`lrn("regr.featureless")` or `lrn("classif.featureless")`).
We use `lrn("classif.featureless")`, which always predicts the majority class.
We can now run our experiment and see errors that occurred during tuning in the archive.
```{r advanced_tuning_methods_and_black_box_optimization-004}
instance = tune(tnr_random, tsk_pen, learner, rsmp_custom, msr_ce,
10)
as.data.table(instance$archive)[1:3, .(df, classif.ce, errors)]
# Reading the error in the first resample result
instance$archive$resample_result(1)$errors
```
The learner was tuned without breaking because the errors were encapsulated and logged before the fallback learners were used for fitting and predicting:
```{r advanced_tuning_methods_and_black_box_optimization-005}
instance$result
```
### Memory Management {#sec-memory-management}
Running a large tuning experiment can use a lot of memory, especially when using nested resampling.
Most of the memory is consumed by the models since each resampling iteration creates one new model.
Storing the models is therefore disabled by default and in most cases is not required.
The option `store_models` in the functions `r ref("ti()")` and `r ref("auto_tuner()")` allows us to enable the storage of the models.
The archive stores a `r ref("ResampleResult")` for each evaluated hyperparameter configuration.
The contained `r ref("Prediction")` objects can also take up a lot of memory, especially with large datasets and many resampling iterations.
We can disable the storage of the resample results by setting `store_benchmark_result = FALSE` in the functions `ti()` and `auto_tuner()`.
Note that without the resample results, it is no longer possible to score the configurations with another measure.
When we run nested resampling with many outer resampling iterations, additional memory can be saved if we set `store_tuning_instance = FALSE` in the `auto_tuner()` function.
However, the functions `r ref("extract_inner_tuning_results()")` and `r ref("extract_inner_tuning_archives()")` will then no longer work.
The option `store_models = TRUE` sets `store_benchmark_result` and `store_tuning_instance` to `TRUE` because the models are stored in the benchmark results which in turn is part of the instance.
This also means that `store_benchmark_result = TRUE` sets `store_tuning_instance` to `TRUE`.
Finally, we can set `store_models = FALSE` in the `r ref("resample()")` or `r ref("benchmark()")` functions to disable the storage of the auto tuners when running nested resampling.
This way we can still access the aggregated performance (`rr$aggregate()`) but lose information about the inner resampling.
## Multi-Objective Tuning {#sec-multi-metrics-tuning}
So far we have considered optimizing a model with respect to one metric, but multi-criteria, or `r index("multi-objective", "multi-objective tuning", aside = TRUE)` optimization, is also possible.
A simple example of multi-objective optimization might be optimizing a classifier to simultaneously maximize true positive predictions and minimize false negative predictions.
In another example, consider the single-objective problem of tuning a `r index('neural network')` to minimize classification error.
The best-performing model is likely to be quite complex, possibly with many layers that will have drawbacks like being harder to deploy on devices with limited resources.
In this case, we might want to simultaneously minimize the classification error and model complexity.
By definition, optimization of multiple metrics means these will be in competition (otherwise we would only optimize one of them) and therefore in general no *single* configuration exists that optimizes all metrics.
Therefore, we instead focus on the concept of `r index("Pareto optimality", lower = FALSE)`.
One hyperparameter configuration is said to *Pareto-dominate* another if the resulting model is equal or better in all metrics and strictly better in at least one metric.
For example say we are minimizing classification error, CE, and complexity, CP, for configurations A and B with CE of $CE_A$ and $CE_B$ respectively and CP of $CP_A$ and $CP_B$ respectively.
Then, A pareto-dominates B if: 1) $CE_A \leq CE_B$ and $CP_A < CP_B$ or; 2) $CE_A < CE_B$ and $CP_A \leq CP_B$.
All configurations that are not Pareto-dominated by any other configuration are called *Pareto-efficient* and the set of all these configurations is the *Pareto set*.
The metric values corresponding to the Pareto set are referred to as the `r index("Pareto front", lower = FALSE, aside = TRUE)`.
The goal of multi-objective hyperparameter optimization is to find a set of non-dominated solutions so that their corresponding metric values approximate the Pareto front.
We will now demonstrate multi-objective hyperparameter optimization by tuning a decision tree on `tsk("sonar")` with respect to the classification error, as a measure of model performance, and the number of selected features, as a measure of model complexity (in a decision tree the number of selected features is straightforward to obtain by simply counting the number of unique splitting variables).
Methodological details on multi-objective hyperparameter optimization can be found in @karl2022 and @Morales2022.
We will tune `cp`, `minsplit`, and `maxdepth`:
```{r advanced_tuning_methods_and_black_box_optimization-006}
learner = lrn("classif.rpart", cp = to_tune(1e-04, 1e-1),
minsplit = to_tune(2, 64), maxdepth = to_tune(1, 30))
measures = msrs(c("classif.ce", "selected_features"))
```
As we are tuning with respect to multiple measures, the function `ti()` automatically creates a `r ref("TuningInstanceBatchMultiCrit")` instead of a `r ref("TuningInstanceBatchSingleCrit")`.
Below we set `store_models = TRUE` as this is required by the selected features measure.
```{r advanced_tuning_methods_and_black_box_optimization-007}
instance = ti(
task = tsk("sonar"),
learner = learner,
resampling = rsmp("cv", folds = 3),
measures = measures,
terminator = trm("evals", n_evals = 30),
store_models = TRUE
)
instance
```
We can then select and tune a tuning algorithm as usual:
```{r advanced_tuning_methods_and_black_box_optimization-008,output=FALSE}
#| cache: false
tuner = tnr("random_search")
tuner$optimize(instance)
```
Finally, we inspect the best-performing configurations, i.e., the Pareto set, and visualize the corresponding estimated Pareto front (@fig-pareto). Note that the `selected_features` measure is averaged across the folds, so the values in the archive may not always be integers.
```{r advanced_tuning_methods_and_black_box_optimization-009}
#| cache: false
instance$archive$best()[, .(cp, minsplit, maxdepth, classif.ce,
selected_features)]
```
```{r advanced_tuning_methods_and_black_box_optimization-010,message=FALSE, echo = FALSE, warning = FALSE}
#| cache: false
#| label: fig-pareto
#| fig-cap: Pareto front of selected features and classification error. White dots represent tested configurations, each black dot individually represents a Pareto-optimal configuration and all black dots together represent the approximated Pareto front.
#| fig-alt: Scatter plot with selected_features on x-axis and classif.ce on y-axis. Plot shows around 15 white dots and two black dots joined by a line at roughly (1, 0.26) and (2.5, 0.25).
library(ggplot2)
ggplot(as.data.table(instance$archive), aes(x = selected_features, y = classif.ce)) +
geom_point(
data = ,
shape = 21,
size = 3,
fill = "white",
color = "black",
alpha = 0.8,
stroke = 0.5) +
geom_step(
data = instance$archive$best(),
direction = "hv",
colour = "black",
linewidth = 1) +
geom_point(
data = instance$archive$best(),
shape = 21,
size = 3,
fill = "black",
colour = "white",
alpha = 0.8,
stroke = 0.5) +
theme_minimal()
```
Determining which configuration to deploy from the Pareto front is up to you.
By definition, there is no optimal configuration so this may depend on your use case, for example, if you would prefer lower complexity at the cost of higher error then you might prefer a configuration where `selected_features = 1`.
You can select one configuration and pass it to a learner for training using `$result_learner_param_vals`, so if we want to select the second configuration we would run:
```{r advanced_tuning_methods_and_black_box_optimization-011}
learner = lrn("classif.rpart")
learner$param_set$values = instance$result_learner_param_vals[[2]]
```
As multi-objective tuning requires manual intervention to select a configuration, it is currently not possible to use `r ref("auto_tuner()")`.
## Multi-Fidelity Tuning via Hyperband {#sec-hyperband}
Increasingly large datasets and search spaces and increasingly complex models make hyperparameter optimization a time-consuming and computationally expensive task.
To tackle this, some HPO methods make use of evaluating a configuration at multiple fidelity levels\index{fidelity}.
`r index('Multi-fidelity HPO', "multi-fidelity", parent = "fidelity", lower = FALSE, aside = TRUE)` is motivated by the idea that the performance of a lower-fidelity model is indicative of the full-fidelity model, which can be used to make HPO more efficient (as we will soon see with Hyperband).
To unpack what these terms mean and to motivate multi-fidelity tuning, say that we think a gradient boosting algorithm with up to 1000 rounds will be a very good fit to our training data.
However, we are concerned this model will take too long to tune and train.
Therefore, we want to gauge the performance of this model using a similar model that is quicker to train by setting a smaller number of rounds.
In this example, the hyperparameter controlling the number of rounds is a *fidelity parameter*, as it controls the tradeoff between model performance and speed.
The different configurations of this parameter are known as *fidelity levels*.
We refer to the model with 1000 rounds as the model at *full-fidelity* and we want to approximate this model's performance using models at different fidelity levels.
Lower fidelity levels result in low-fidelity models that are quicker to train but may poorly predict the full-fidelity model's performance.
On the other hand, higher fidelity levels result in high-fidelity models that are slower to train but may better indicate the full-fidelity model's performance.
Other common models that have natural fidelity parameters include `r index('neural networks', "neural network")` (number of epochs) and `r index('random forests', 'random forest')` (number of trees).
The proportion of data to subsample before running any algorithm can also be viewed as a model-agnostic fidelity parameter, we will return to this in @sec-hyperband-example-svm.
### Hyperband and Successive Halving
A popular multi-fidelity HPO algorithm is *Hyperband* [@li_2018].
After having evaluated randomly sampled configurations on low fidelities, `r index('Hyperband')` iteratively allocates more resources to promising configurations and terminates low-performing ones early.
Hyperband builds upon the Successive Halving algorithm by @jamieson_2016.
Successive Halving is initialized with a number of starting configurations $m_0$, the proportion of configurations discarded in each stage $\eta$, and the minimum, $r{_{0}}$, and maximum, $r{_{max}}$, budget (fidelity) of a single evaluation.
The algorithm starts by sampling $m_0$ random configurations and allocating the minimum budget $r{_{0}}$ to them.
The configurations are evaluated and $\frac{\eta - 1}{\eta}$ of the worst-performing configurations are discarded.
The remaining configurations are promoted to the next stage, or 'bracket', and evaluated on a larger budget.
This continues until one or more configurations are evaluated on the maximum budget $r{_{max}}$ and the best-performing configuration is selected.
The total number of stages is calculated so that each stage consumes approximately the same overall budget.
A big disadvantage of this method is that it is unclear if it is better to start with many configurations (large $m_0$) and a small budget or fewer configurations (small $m_0$) but a larger budget.
Hyperband solves this problem by running Successive Halving with different numbers of starting configurations, each at different budget levels $r_{0}$.
The algorithm is initialized with the same $\eta$ and $r_{max}$ parameters (but not $m_0$).
Each bracket starts with a different budget, $r_0$, where smaller values mean that more configurations can be evaluated and so the most exploratory bracket (i.e., the one with the most number of stages) is allocated the global minimum budget $r_{min}$.
In each bracket, the starting budget increases by a factor of $\eta$ until the last bracket essentially performs a random search with the full budget $r_{max}$.
The total number of brackets, $s_{max} + 1$, is calculated as $s_{max} = {\log_\eta \frac{r_{max}}{r_{min}}}$.
The number of starting configurations $m_0$ of each bracket are calculated so that each bracket uses approximately the same amount of budget.
The optimal hyperparameter configuration in each bracket is the configuration with the best performance in the final stage.
The optimal hyperparameter configuration at the end of tuning is the configuration with the best performance across all brackets.
An example Hyperband schedule is given in @tbl-hyperband where $s = 3$ is the most exploratory bracket and $s = 0$ essentially performs a random search using the full budget.
@tbl-hyperband-eg demonstrates how this schedule may look if we were to tune 20 different hyperparameter configurations; note that each entry in the table is a unique ID referring to a possible configuration of multiple hyperparameters to tune.
+-----+-------------------+-------------------+-------------------+-------------------+
| | $s = 3$ | $s = 2$ | $s = 1$ | $s = 0$ |
+-----+---------+---------+---------+---------+---------+---------+---------+---------+
| $i$ | $m_{i}$ | $r_{i}$ | $m_{i}$ | $r_{i}$ | $m_{i}$ | $r_{i}$ | $m_{i}$ | $r_{i}$ |
+=====+=========+=========+=========+=========+=========+=========+=========+=========+
| 0 | 8 | 1 | 6 | 2 | 4 | 4 | 4 | 8 |
+-----+---------+---------+---------+---------+---------+---------+---------+---------+
| 1 | 4 | 2 | 3 | 4 | 2 | 8 | | |
+-----+---------+---------+---------+---------+---------+---------+---------+---------+
| 2 | 2 | 4 | 1 | 8 | | | | |
+-----+---------+---------+---------+---------+---------+---------+---------+---------+
| 3 | 1 | 8 | | | | | | |
+-----+---------+---------+---------+---------+---------+---------+---------+---------+
: Hyperband schedule with the number of configurations, $m_{i}$, and resources, $r_{i}$, for each bracket, $s$, and stage, $i$, when $\eta = 2$, $r{_{min}} = 1$ and $r{_{max}} = 8$. {#tbl-hyperband}
| | $s = 3$ | $s = 2$ | $s = 1$ | $s = 0$ |
|---- | ---------------- | ---------------- | ---------------- | ---------------- |
| $i = 0$ | $\{1, 2, 3, 4, 5, 6, 7, 8\}$ | $\{9, 10, 11, 12, 13, 14\}$ | $\{15, 16, 17, 18\}$ | $\{19, 20, 21, 22\}$ |
| $i = 1$ | $\{1, 2, 7, 8\}$ | $\{9, 14, 15\}$ | $\{20, 21\}$ |
| $i = 2$ | $\{1, 8\}$ | $\{15\}$ | |
| $i = 3$ | $\{1\}$ | | | |
| $HPC^*_s$ | $\{1\}$ | $\{15\}$ | $\{21\}$ | $\{22\}$ |
| $HPC^*$ | $\{15\}$ |
: Hyperparameter configurations in each stage and bracket from the schedule in @tbl-hyperband. Entries are unique identifiers for tested hyperparameter configurations (HPCs). $HPC^*_s$ is the optimal hyperparameter configuration in bracket $s$ and $HPC^*$ is the optimal hyperparameter configuration across all brackets. {#tbl-hyperband-eg}
### mlr3hyperband
The Successive Halving and Hyperband algorithms are implemented in `r mlr3hyperband` as `tnr("successive_halving")` and `tnr("hyperband")` respectively; in this section, we will only showcase the Hyperband method.
By example, we will optimize `lrn("classif.xgboost")` on `tsk("sonar")` and use the number of boosting iterations (`nrounds`) as the fidelity parameter, this is a suitable choice as increasing iterations increases model training time but generally also improves performance.
Hyperband will allocate increasingly more boosting iterations to well-performing hyperparameter configurations.
We will load the learner and define the search space.
We specify a range from 16 ($r_{min}$) to 128 ($r_{max}$) boosting iterations and tag the parameter with `"budget"` to identify it as a fidelity parameter.
For the other hyperparameters, we take the search space for XGBoost from @hpo_practical, which usually works well for a wide range of datasets.
```{r advanced_tuning_methods_and_black_box_optimization-012, message = FALSE, warning = FALSE}
library(mlr3hyperband)
learner = lrn("classif.xgboost")
learner$param_set$set_values(
nrounds = to_tune(p_int(16, 128, tags = "budget")),
eta = to_tune(1e-4, 1, logscale = TRUE),
max_depth = to_tune(1, 20),
colsample_bytree = to_tune(1e-1, 1),
colsample_bylevel = to_tune(1e-1, 1),
lambda = to_tune(1e-3, 1e3, logscale = TRUE),
alpha = to_tune(1e-3, 1e3, logscale = TRUE),
subsample = to_tune(1e-1, 1)
)
```
We now construct the tuning instance and a hyperband tuner with `eta = 2`.
We use `trm("none")` and set the `repetitions` control parameter to `1` so that Hyperband can terminate itself after all brackets have been evaluated a single time.
Note that setting `repetition = Inf` can be useful if you want a terminator to stop the optimization, for example, based on runtime.
The `r ref("hyperband_schedule()")` function can be used to display the schedule across the given fidelity levels and budget increase factor.
```{r advanced_tuning_methods_and_black_box_optimization-013}
instance = ti(
task = tsk("sonar"),
learner = learner,
resampling = rsmp("holdout"),
measures = msr("classif.ce"),
terminator = trm("none")
)
tuner = tnr("hyperband", eta = 2, repetitions = 1)
hyperband_schedule(r_min = 16, r_max = 128, eta = 2)
```
Finally, we can tune as normal and print the result and archive.
Note that the archive resulting from a Hyperband run contains the additional columns `bracket` and `stage` which break down the results by the corresponding bracket and stage.
```{r advanced_tuning_methods_and_black_box_optimization-014, message = FALSE}
tuner$optimize(instance)
instance$result[, .(classif.ce, nrounds)]
as.data.table(instance$archive)[,
.(bracket, stage, classif.ce, eta, max_depth, colsample_bytree)]
```
## Bayesian Optimization {#sec-bayesian-optimization}
In this section, we will take a deep dive into `r index('Bayesian optimization', lower = FALSE)` (BO), also known as Model Based Optimization (MBO)\index{model based optimization|see{Bayesian optimization}}.
The design of BO is more complex than what we have seen so far in other tuning methods so to help motivate this we will spend a little more time in this section on theory and methodology.
In `r index('hyperparameter optimization')` (@sec-optimization), learners are passed a hyperparameter configuration and evaluated on a given task via a resampling technique to estimate its generalization performance with the goal to find the optimal hyperparameter configuration.
In general, no analytical description for the mapping from hyperparameter configuration to performance exists and gradient information is also not available.
HPO is, therefore, a prime example for `r index('black box optimization', aside = TRUE)`, which considers the optimization of a function whose mathematical structure and analytical description is unknown or unexploitable.
As a result, the only observable information is the output value (i.e., generalization performance) of the function given an input value (i.e., hyperparameter configuration).
In fact, as evaluating the performance of a learner can take a substantial amount of time, HPO is quite an expensive black box optimization problem.
Black box optimization problems occur in the real-world, for example they are encountered quite often in engineering such as in modeling experiments like crash tests or chemical reactions.
Many optimization algorithm classes exist that can be used for black box optimization, which differ in how they tackle this problem; for example we saw in @sec-optimization methods including grid/random search and briefly discussed evolutionary strategies.
Bayesian optimization refers to a class of sample-efficient iterative global black box optimization algorithms that rely on a 'surrogate model' trained on observed data to model the black box function.
This surrogate model is typically a non-linear regression model that tries to capture the unknown function using limited observed data.
During each iteration, BO algorithms employ an 'acquisition function' to determine the next candidate point for evaluation.
This function measures the expected 'utility' of each point within the search space based on the prediction of the surrogate model.
The algorithm then selects the candidate point with the best acquisition function value and evaluates the black box function at that point to then update the surrogate model.
This iterative process continues until a termination criterion is met, such as reaching a pre-specified maximum number of evaluations or achieving a desired level of performance.
BO is a powerful method that often results in good optimization performance, especially if the cost of the black box evaluation becomes expensive and the optimization budget is tight.
In the rest of this section, we will first provide an introduction to black box optimization with the `r bbotk` package and then introduce the building blocks of BO algorithms and examine their interplay and interaction during the optimization process before we assemble these building blocks in a ready to use black box optimizer with `r mlr3mbo`.
Readers who are primarily interested in how to utilize BO for HPO without delving deep into the underlying building blocks may want to skip to @sec-bayesian-tuning.
Detailed introductions to black box optimization and BO are given in @hpo_practical, @hpo_automl and @garnett_2022.
As a running example throughout this section, we will optimize the sinusoidal function $f: [0, 1] \rightarrow \mathbb{R}, x \mapsto 2x * \sin(14x)$ (@fig-bayesian-optimization-sinusoidal), which is characterized by two local minima and one global minimum.
### Black Box Optimization {#sec-black-box-optimization}
The `r bbotk` (black box optimization toolkit) package is the workhorse package for general black box optimization within the `mlr3` ecosystem.
At the heart of the package are the R6 classes:
* `r ref("OptimInstanceBatchSingleCrit")` and `r ref("OptimInstanceBatchMultiCrit")`, which are used to construct an `r index('optimization instance', aside = TRUE)` that describes the optimization problem and stores the results
* `r ref("OptimizerBatch")` which is used to construct and configure optimization algorithms.
These classes might look familiar after reading @sec-optimization, and in fact `r ref("TuningInstanceBatchSingleCrit")` and `r ref("TuningInstanceBatchMultiCrit")` inherit from `OptimInstanceBatchSingle/MultiCrit` and `r ref("TunerBatch")` is closely based on `OptimizerBatch`.
`OptimInstanceBatchSingleCrit` relies on an `r ref("Objective", aside = TRUE)` function that wraps the actual mapping from a domain (all possible function inputs) to a codomain (all possible function outputs).
Objective functions can be created using different classes, all of which inherit from `r ref("Objective")`.
These classes provide different ways to define and evaluate objective functions and picking the right one will reduce type conversion overhead:
* `r ref("ObjectiveRFun")` wraps a function that takes a list describing a *single configuration* as input where elements can be of any type. It is suitable when the underlying function evaluation mechanism is given by evaluating a single configuration at a time.
* `r ref("ObjectiveRFunMany")` wraps a function that takes a list of *multiple configurations* as input where elements can be of any type and even mixed types. It is useful when the function evaluation of multiple configurations can be parallelized.
* `r ref("ObjectiveRFunDt")` wraps a function that operates on a `data.table`. It allows for efficient vectorized or batched evaluations directly on the `data.table` object, avoiding unnecessary data type conversions.
To start translating our problem to code we will use the `r ref("ObjectiveRFun")` class to take a single configuration as input.
The `Objective` requires specification of the function to optimize its domain and codomain.
By tagging the codomain with `"minimize"` or `"maximize"` we specify the optimization direction.
Note how below our optimization function takes a `list` as an input with one element called `x`.
```{r advanced_tuning_methods_and_black_box_optimization-015}
library(bbotk)
sinus_1D = function(xs) 2 * xs$x * sin(14 * xs$x)
domain = ps(x = p_dbl(lower = 0, upper = 1))
codomain = ps(y = p_dbl(tags = "minimize"))
objective = ObjectiveRFun$new(sinus_1D,
domain = domain, codomain = codomain)
```
We can visualize our objective by generating a grid of points on which we evaluate the function (@fig-bayesian-optimization-sinusoidal), this will help us identify its local minima and global minimum.
```{r advanced_tuning_methods_and_black_box_optimization-016}
#| label: fig-bayesian-optimization-sinusoidal
#| fig-cap: Visualization of the sinusoidal function. Local minima in triangles and global minimum in the circle.
#| fig-alt: Line graph from (0,1) on the x-axis to (-2,2) on the y-axis; labelled 'x' and 'y' respectively. The line starts with a local minimum at (0,0), increases and then has a local minimum at around (0.35,-0.69), the function then increases and then decreases to the global minimum at around (0.79, -1.56).
library(ggplot2)
xydt = generate_design_grid(domain, resolution = 1001)$data
xydt[, y := objective$eval_dt(xydt)$y]
optima = data.table(x = c(0, 0.3509406, 0.7918238))
optima[, y := objective$eval_dt(optima)$y]
optima[, type := c("local", "local", "global")]
ggplot(aes(x = x, y = y), data = xydt) +
geom_line() +
geom_point(aes(pch = type), color = "black", size = 4, data = optima) +
theme_minimal() +
theme(legend.position = "none")
```
The global minimizer, `r xydt[y == min(y), x]`, corresponds to the point of the domain with the lowest function value:
```{r advanced_tuning_methods_and_black_box_optimization-017}
xydt[y == min(y), ]
```
With the objective function defined, we can proceed to optimize it using `OptimInstanceBatchSingleCrit`.
This class allows us to wrap the objective function and explicitly specify a search space.
The search space defines the set of input values we want to optimize over, and it is typically a subset or transformation of the domain, though by default the entire domain is taken as the search space.
In black box optimization, it is common for the domain, and hence also the search space, to have finite box constraints.
Similarly to HPO, transformations can sometimes be used to more efficiently search the space (@sec-logarithmic-transformations).
In the following, we use a simple random search to optimize the sinusoidal function over the whole domain and inspect the result from the `instance` in the usual way (@sec-tuner).
An optimization instance is constructed with the `r ref("oi()")` function.
Analogously to tuners, `Optimizer`s in `bbotk` are stored in the `r ref('mlr_optimizers')` dictionary and can be constructed with `r ref('opt()', aside = TRUE)`.
```{r advanced_tuning_methods_and_black_box_optimization-018, output=FALSE}
instance = oi(objective,
search_space = domain,
terminator = trm("evals", n_evals = 20))
optimizer = opt("random_search", batch_size = 20)
optimizer$optimize(instance)
```
Similarly to how we can use `r ref("tune()")` to construct a tuning instance, here we can use `r ref("bb_optimize()")`, which returns a list with elements `"par"` (best found parameters), `"val"` (optimal outcome), and `"instance"` (the optimization instance); the values given as `"par"` and `"val"` are the same as the values found in `instance$result`:
```{r advanced_tuning_methods_and_black_box_optimization-019}
optimal = bb_optimize(objective, method = "random_search",
max_evals = 20)
optimal$instance$result
```
Now we have introduced the basic black box optimization setup, we can introduce the building blocks of any Bayesian optimization algorithm.
### Building Blocks of Bayesian Optimization {#sec-bayesian-optimization-blocks}
Bayesian optimization (BO) is a global optimization algorithm that usually follows the following process (@fig-optimization-loop):
1. Generate and evaluate an `r index('initial design')`
2. Loop:
a. Fit a `r index('surrogate model')` on the archive of all observations made so far to model the unknown black box function.
b. Optimize an `r index('acquisition function')` to determine which points of the search space are promising candidate(s) that should be evaluated next.
c. Evaluate the next candidate(s) and update the archive of all observations made so far.
d. Check if a given termination criterion is met, if not go back to (a).
The acquisition function relies on the mean and standard deviation prediction of the surrogate model and requires no evaluation of the true black box function, making it comparably cheap to optimize.
A good acquisition function will balance *exploiting* knowledge about regions where we observed that performance is good and the surrogate model has low uncertainty, with *exploring* regions where it has not yet evaluated points and as a result the uncertainty of the surrogate model is high.
We refer to these elements as the 'building blocks' of BO as it is a highly modular algorithm; as long as the above structure is in place, then the surrogate models, acquisition functions, and acquisition function optimizers are all interchangeable to a certain extent.
The design of `mlr3mbo` reflects this modularity, with the base class for `r ref("OptimizerMbo")` holding all the key elements: the BO algorithm loop structure (`r ref("loop_function")`), *surrogate* model (`r ref("Surrogate")`), *acquisition function* (`r ref("AcqFunction")`), and *acquisition function optimizer* (`r ref("AcqOptimizer")`).
In this section, we will provide a more detailed explanation of these building blocks and explore their interplay and interaction during optimization.
```{r advanced_tuning_methods_and_black_box_optimization-020, echo = FALSE, out.width = "80%"}
#| label: fig-optimization-loop
#| fig-cap: Bayesian optimization loop.
#| fig-alt: Diagram is a graphical representation of the loop written out above from initial design to surrogate model and then looping until the termination criterion is met.
include_multi_graphics("mlr3book_figures-10")
```
#### The Initial Design {#sec-bayesian-optimization-initial}
Before we can fit a surrogate model to model the unknown black box function, we need data.
The initial set of points that is evaluated before a surrogate model can be fit is referred to as the `r index('initial design')`.
`mlr3mbo` allows you to either construct the initial design manually or let a `r ref("loop_function")` construct and evaluate this for you.
In this section, we will demonstrate the first method, which requires more user input but therefore allows more control over the initial design.
A simple method to construct an initial design is to use one of the four design generators in `r paradox`:
* `r ref("generate_design_random()")`: Generate points uniformly at random
* `r ref("generate_design_grid()")`: Generate points in a uniform-sized grid
* `r ref("generate_design_lhs()")`: Latin hypercube sampling [@Stein1987]
* `r ref("generate_design_sobol()")`: Sobol sequence [@Niederreiter1988]
@fig-bayesian-optimization-designs illustrates the difference in generated designs from these four methods assuming an initial design of size nine and a domain of two numeric variables from $0$ to $1$.
We already covered the difference between grid and random designs in @sec-tuner.
An LHS design divides each input variable into equally sized intervals (indicated by the horizontal and vertical dotted lines in @fig-bayesian-optimization-designs) and ensures that each interval is represented by exactly one sample point, resulting in uniform marginal distributions.
Furthermore, in LHS designs the minimal distance between two points is usually maximized, resulting in its space-filling coverage of the space.
The Sobol design works similarly to LHS but can provide better coverage than LHS when the number of dimensions is large.
For this reason, LHS or Sobol designs are usually recommended for BO, but usually the influence of the initial design will be smaller compared to other design choices of BO.
A random design might work well-enough, but grid designs are usually discouraged.
```{r advanced_tuning_methods_and_black_box_optimization-021, echo = FALSE}
#| label: fig-bayesian-optimization-designs
#| fig-cap: Comparing different samplers for constructing an initial design of nine points on a domain of two numeric variables ranging from $0$ to $1$. Dotted horizontal and vertical lines partition the domain into equally sized bins. Histograms on the top and right visualize the marginal distributions of the generated sample.
#| fig-alt: "Plot shows four grids with x_1 on x-axis ranging from 0 to 1 and x_2 on y-axis ranging from 0 to 1. Each grid has bars above them and to the right representing marginal distributions. Top left: 'Random Design' nine points are scattered randomly across the grid with poor coverage. Marginal distributions are also random. Top right: 'Grid Design', points are uniformly scattered across the grid on lines x_1=0,x_1=0.5,x_1=1 and same for x_2. Marginal distributions show three long bars at each of the corresponding lines. Bottom left: 'LHS Design', points appear randomly scattered however marginal distributions are completely equal with equal-sized bars along each axis. Bottom right: 'Sobol Design', very similar to 'LHS Design' however one of the bars in the marginal distribution is slightly longer than the others."
library(ggExtra)
library(gridExtra)
sample_domain = ps(x1 = p_dbl(lower = 0, upper = 1),
x2 = p_dbl(lower = 0, upper = 1))
random_design = generate_design_random(sample_domain, n = 9)$data
grid_design = generate_design_grid(sample_domain, resolution = 3)$data
lhs_design = generate_design_lhs(sample_domain, n = 9)$data
sobol_design = generate_design_sobol(sample_domain, n = 9)$data
qs = seq(from = 0, to = 1, length.out = 10)
g_random = ggplot(aes(x = x1, y = x2), data = random_design) +
geom_point(size = 3L) +
geom_vline(xintercept = qs, linetype = 2) +
geom_hline(yintercept = qs, linetype = 2) +
labs(title = "Random Design", x = expression(x[1]), y = expression(x[2])) +
theme_minimal() +
xlim(c(0, 1)) +
ylim(c(0, 1))
g_random = ggMarginal(g_random, type = "histogram", bins = 10)
g_grid = ggplot(aes(x = x1, y = x2), data = grid_design) +
geom_point(size = 3L) +
geom_vline(xintercept = qs, linetype = 2) +
geom_hline(yintercept = qs, linetype = 2) +
labs(title = "Grid Design", x = expression(x[1]), y = expression(x[2])) +
theme_minimal() +
xlim(c(0, 1)) +
ylim(c(0, 1))
g_grid = ggMarginal(g_grid, type = "histogram", bins = 10)
g_lhs = ggplot(aes(x = x1, y = x2), data = lhs_design) +
geom_point(size = 3L) +
geom_vline(xintercept = qs, linetype = 2) +
geom_hline(yintercept = qs, linetype = 2) +
labs(title = "LHS Design", x = expression(x[1]), y = expression(x[2])) +
theme_minimal() +
xlim(c(0, 1)) +
ylim(c(0, 1))
g_lhs = ggMarginal(g_lhs, type = "histogram", bins = 10)
g_sobol = ggplot(aes(x = x1, y = x2), data = sobol_design) +
geom_point(size = 3L) +
geom_vline(xintercept = qs, linetype = 2) +
geom_hline(yintercept = qs, linetype = 2) +
labs(title = "Sobol Design", x = expression(x[1]), y = expression(x[2])) +
theme_minimal() +
xlim(c(0, 1)) +
ylim(c(0, 1))
g_sobol = ggMarginal(g_sobol, type = "histogram", bins = 10)
grid.arrange(g_random, g_grid, g_lhs, g_sobol, nrow = 2, ncol = 2)
```
Whichever of these methods you choose, the result is a `r ref("Design")` object, which is mostly just a wrapper around a `data.table`:
```{r advanced_tuning_methods_and_black_box_optimization-022}
sample_domain = ps(x1 = p_dbl(0, 1), x2 = p_dbl(0, 1))
generate_design_random(sample_domain, n = 3)$data
```
Therefore you could also specify a completely custom initial design by defining your own `data.table`.
Either way, when manually constructing an initial design (as opposed to letting `loop_function` automate this), it needs to be evaluated on the `r ref("OptimInstance")` before optimizing it.
Returning to our running example of minimizing the sinusoidal function, we will evaluate a custom initial design with `$eval_batch()`:
```{r advanced_tuning_methods_and_black_box_optimization-023}
instance = OptimInstanceSingleCrit$new(objective,
terminator = trm("evals", n_evals = 20))
design = data.table(x = c(0.1, 0.34, 0.65, 1))
instance$eval_batch(design)
instance$archive$data
```
We can see how each point in our design was evaluated by the sinusoidal function, giving us data we can now use to start the iterative BO algorithm by fitting the surrogate model on that data.
#### Surrogate Model {#sec-bayesian-optimization-surrogate}
A `r index('surrogate model')` wraps a regression learner that models the unknown black box function based on observed data.
In `mlr3mbo`, the `r ref("SurrogateLearner")` is a higher-level R6 class inheriting from the base `r ref("Surrogate")` class, designed to construct and manage the surrogate model, including automatic construction of the `TaskRegr` that the learner should be trained on at each iteration of the BO loop.
Any regression learner in `mlr3` can be used.
However, most acquisition functions depend on both mean and standard deviation predictions from the surrogate model, the latter of which requires the `"se"` `predict_type` to be supported.
Therefore not all learners are suitable for all scenarios.
Typical choices of regression learners used as surrogate models include `r index('Gaussian processes', "Gaussian process", lower = FALSE)` (`lrn("regr.km")`) for low to medium dimensional numeric search spaces and `r index('random forests', 'random forest')` (e.g., `lrn("regr.ranger")`) for higher dimensional mixed (and/or hierarchical) search spaces.
A detailed introduction to Gaussian processes can be found in @williams_2006 and an in-depth focus on Gaussian processes in the context of surrogate models in BO is given in @garnett_2022.
In this example, we use a Gaussian process with Matérn 5/2 kernel, which uses `BFGS` as an optimizer to find the optimal kernel parameters and set `trace = FALSE` to prevent too much output during fitting.
```{r advanced_tuning_methods_and_black_box_optimization-024}
#| cache: false
lrn_gp = lrn("regr.km", covtype = "matern5_2", optim.method = "BFGS",
control = list(trace = FALSE))
```
A `SurrogateLearner` can be constructed by passing a `LearnerRegr` object to the sugar function `r index('srlrn()', aside = TRUE, code = TRUE)`, alongside the `archive` of the instance:
```{r advanced_tuning_methods_and_black_box_optimization-025}
#| cache: false
library(mlr3mbo)
surrogate = srlrn(lrn_gp, archive = instance$archive)
```
Internally, the regression learner is fit on a `TaskRegr` where features are the variables of the domain and the target is the codomain, the data is from the `r ref("bbotk::Archive")` of the `r ref("bbotk::OptimInstance")` that is to be optimized.
In our running example we have already initialized our archive with the initial design, so we can update our surrogate model, which essentially fits the Gaussian process, note how we use `$learner` to access the wrapped model:
```{r advanced_tuning_methods_and_black_box_optimization-026}
#| cache: false
surrogate$update()
surrogate$learner$model
```
Having introduced the concept of a surrogate model, we can now move on to the acquisition function, which makes use of the surrogate model predictions to decide which candidate to evaluate next.
#### Acquisition Function {#sec-bayesian-optimization-acquisition}
Roughly speaking, an `r index('acquisition function')` relies on the prediction of a surrogate model and quantifies the expected 'utility' of each point of the search space if it were to be evaluated in the next iteration.
A popular example is the expected improvement [@jones_1998], which tells us how much we can expect a candidate point to improve over the best function value observed so far (the 'incumbent'), given the performance prediction of the surrogate model:
$$
\alpha_{\mathrm{EI}}(\mathbf{x}) = \mathbb{E} \left[ \max \left( f_{\mathrm{min}} - Y(\mathbf{x}), 0 \right) \right]
$$
Here, $Y(\mathbf{x)}$ is the surrogate model prediction (a random variable) for a given point $\mathbf{x}$ (which when using a Gaussian process follows a normal distribution) and $f_{\mathrm{min}}$ is the best function value observed so far (assuming minimization).
Calculating the expected improvement requires mean and standard deviation predictions from the model.
In `mlr3mbo`, acquisition functions (of class `r ref( "AcqFunction")`) are stored in the `r ref("mlr_acqfunctions")` dictionary and can be constructed with `r ref('acqf()', aside = TRUE)`, passing the key of the method you want to use and our surrogate learner.
In our running example, we will use the expected improvement (`acqf("ei")`) to choose the next candidate for evaluation.
Before we can do that, we have to update (`$update()`) the `AcqFunction`'s view of the incumbent, to ensure it is still using the best value observed so far.
```{r advanced_tuning_methods_and_black_box_optimization-027}
#| cache: false
acq_function = acqf("ei", surrogate = surrogate)
acq_function$update()
acq_function$y_best
```
You can use `$eval_dt()` to evaluate the acquisition function for the domain given as a `data.table`.
In @fig-bayesian-optimization-ei we evaluated the expected improvement on a uniform grid of points between $0$ and $1$ using the predicted mean and standard deviation from the Gaussian process.
We can see that the expected improvement is high in regions where the mean prediction (gray dashed lines) of the Gaussian process is low, or where the uncertainty is high.
```{r advanced_tuning_methods_and_black_box_optimization-028}
#| cache: false
#| label: fig-bayesian-optimization-ei
#| fig-cap: Expected improvement (solid dark gray line) based on the mean and uncertainty prediction (dashed gray line) of the Gaussian process surrogate model trained on an initial design of four points (black). Ribbons represent the mean plus minus the standard deviation prediction.
#| fig-alt: Plot of sinusoidal function plotted previously but now with four black dots at around (0.1, 0), (0.3, -0.8), (0.6, 0.5) and (1, 2). These are connected by a black curve. A gray dotted line is also fit through these black dots in a roughly 'u' shape, a gray ribbon surrounds this suggesting uncertainty.
xydt = generate_design_grid(domain, resolution = 1001)$data
# evaluate our sinusoidal function
xydt[, y := objective$eval_dt(xydt)$y]
# evaluate expected improvement
xydt[, ei := acq_function$eval_dt(xydt[, "x"])]
# make predictions from our data
xydt[, c("mean", "se") := surrogate$predict(xydt[, "x"])]
xydt[1:3]
ggplot(xydt, mapping = aes(x = x, y = y)) +
geom_point(size = 2, data = instance$archive$data) +
geom_line() +
geom_line(aes(y = mean), colour = "gray", linetype = 2) +
geom_ribbon(aes(min = mean - se, max = mean + se),
fill = "gray", alpha = 0.3) +
geom_line(aes(y = ei * 40), linewidth = 1, colour = "darkgray") +
scale_y_continuous("y",
sec.axis = sec_axis(~ . * 0.025, name = "EI",
breaks = c(0, 0.025, 0.05))) +
theme_minimal()
```
We will now proceed to optimize the acquisition function itself to find the candidate with the largest expected improvement.
#### Acquisition Function Optimizer {#sec-bayesian-optimization-acquisitionopt}
An `r index('acquisition function optimizer')` of class `r ref("AcqOptimizer", aside = TRUE)` is used to optimize the acquisition function by efficiently searching the space of potential candidates within a limited computational budget.
Due to the non-convex nature of most commonly used acquisition functions [@garnett_2022] it is typical to employ global optimization techniques for acquisition function optimization.
Widely used approaches for optimizing acquisition functions include derivative-free global optimization methods like branch and bound algorithms, such as the `r index('DIRECT', lower = FALSE)` algorithm [@jones_1993_lipschitzian], as well as multi-start local optimization methods, such as running the `r index('L-BFGS-B', lower = FALSE)` algorithm [@byrd1995limited] or a local search multiple times from various starting points [@kim_2021].
Consequently the optimization problem of the acquisition function can be handled as a black box optimization problem itself, but a much cheaper one than the original.
`AcqOptimizer` objects are constructed with `r ref("acqo()", aside = TRUE)`, which takes as input a `r ref('bbotk::Optimizer')`, a `r ref("bbotk::Terminator")`, and the acquisition function.
Optimizers are stored in the `r ref("mlr_optimizers")` dictionary and can be constructed with the sugar function `r ref('opt()', aside = TRUE)`.
The terminators are the same as those introduced in @sec-terminator.
Below we use the DIRECT algorithm and we terminate the acquisition function optimization if there is no improvement of at least `1e-5` for `100` iterations.
The `$optimize()` method optimizes the acquisition function and returns the next candidate.
```{r advanced_tuning_methods_and_black_box_optimization-029}
acq_optimizer = acqo(
optimizer = opt("nloptr", algorithm = "NLOPT_GN_ORIG_DIRECT"),
terminator = trm("stagnation", iters = 100, threshold = 1e-5),
acq_function = acq_function
)
candidate = acq_optimizer$optimize()
candidate
```
We have now shown how to run a single iteration of the BO algorithm loop manually.
In practice, one would use `r ref("OptimizerMbo")` to put all these pieces together to automate the process.
Before demonstrating this class we will first take a step back and introduce the `loop_function` which tells the algorithm how it should be run.
#### Using and Building Loop Functions {#sec-bayesian-optimization-loop}
The `r ref("loop_function", index = TRUE)` determines the behavior of the BO algorithm on a global level, i.e., how to define the subroutine that is performed at each iteration to generate new candidates for evaluation.
`r index('Loop functions')` are relatively simple functions that take as input the classes that we have just discussed and define the BO loop.
Loop functions are stored in the `r ref("mlr_loop_functions")` dictionary.
As these are `S3` (not `R6`) classes, they can be simply loaded by just referencing the `key` (i.e., there is no constructor required).
```{r advanced_tuning_methods_and_black_box_optimization-030}
as.data.table(mlr_loop_functions)[, .(key, label, instance)]
```
You could pick and use one of the loop functions included in the dictionary above, or you can write your own for finer control over the BO process.
A common choice of loop function is the `r index('Efficient Global Optimization')` (EGO) algorithm [@jones_1998] (`r ref('bayesopt_ego()')`).
A simplified version of this code is shown at the end of this section, both to help demonstrate the EGO algorithm, and to give an example of how to write a custom BO variant yourself.
In short, the code sets up the relevant components discussed above and then loops the steps above: 1) update the surrogate model 2) update the acquisition function 3) optimize the acquisition function to yield a new candidate 4) evaluate the candidate and add it to the archive.
If there is an error during the loop then a fallback is used where the next candidate is proposed uniformly at random, ensuring that the process continues even in the presence of potential issues, we will return to this in @sec-practical-bayesian-optimization.
```{r advanced_tuning_methods_and_black_box_optimization-031, eval=FALSE}
my_simple_ego = function(
instance,
surrogate,
acq_function,
acq_optimizer,
init_design_size
) {
# setting up the building blocks
surrogate$archive = instance$archive # archive
acq_function$surrogate = surrogate # surrogate model
acq_optimizer$acq_function = acq_function # acquisition function
search_space = instance$search_space
# initial design
design = generate_design_sobol(
search_space, n = init_design_size)$data
instance$eval_batch(design)
# MBO loop
repeat {
candidate = tryCatch({
# update the surrogate model
acq_function$surrogate$update()
# update the acquisition function
acq_function$update()
# optimize the acquisition function to yield a new candidate
acq_optimizer$optimize()
}, mbo_error = function(mbo_error_condition) {
generate_design_random(search_space, n = 1L)$data
})
# evaluate the candidate and add it to the archive
tryCatch({
instance$eval_batch(candidate)
}, terminated_error = function(cond) {
# $eval_batch() throws a terminated_error if the instance is
# already terminated, e.g. because of timeout.
})
if (instance$is_terminated) break
}
return(instance)
}
```
We are now ready to put everything together to automate the BO process.
### Automating BO with OptimizerMbo {#sec-bayesian-black-box-optimization}
`r ref("OptimizerMbo")` can be used to assemble the building blocks described above into a single object that can then be used for optimization.
We use the `bayesopt_ego` loop function provided by `mlr_loop_functions`, which works similarly to the code shown above but takes more care to offer sensible default values for its arguments and handle edge cases correctly.
You do not need to pass any of these building blocks to each other manually as the `r ref('opt()', aside = TRUE)` constructor will do this for you:
```{r advanced_tuning_methods_and_black_box_optimization-032}
bayesopt_ego = mlr_loop_functions$get("bayesopt_ego")
surrogate = srlrn(lrn("regr.km", covtype = "matern5_2",
optim.method = "BFGS", control = list(trace = FALSE)))
acq_function = acqf("ei")
acq_optimizer = acqo(opt("nloptr", algorithm = "NLOPT_GN_ORIG_DIRECT"),
terminator = trm("stagnation", iters = 100, threshold = 1e-5))
optimizer = opt("mbo",
loop_function = bayesopt_ego,
surrogate = surrogate,
acq_function = acq_function,
acq_optimizer = acq_optimizer)
```
::: {.callout-tip}
## Loop Function Arguments
Additional arguments for customizing certain loop functions can be passed through with the `args` parameter of `opt()`.
:::
In this example, we will use the same initial design that we created before and will optimize our sinusoidal function using `$optimize()`:
```{r advanced_tuning_methods_and_black_box_optimization-033}
instance = OptimInstanceSingleCrit$new(objective,
terminator = trm("evals", n_evals = 20))
design = data.table(x = c(0.1, 0.34, 0.65, 1))
instance$eval_batch(design)
optimizer$optimize(instance)
```
Using only a few evaluations, BO comes close to the true global optimum (0.792).
@fig-bayesian-optimization-sampling shows the sampling trajectory of candidates as the algorithm progressed, we can see that focus is increasingly given to more regions around the global optimum.
However, even in later optimization stages, the algorithm still explores new areas, illustrating that the expected improvement acquisition function indeed balances exploration and exploitation as we required.
```{r advanced_tuning_methods_and_black_box_optimization-034, echo = FALSE}
#| label: fig-bayesian-optimization-sampling
#| fig-cap: Sampling trajectory of the BO algorithm. Points of the initial design in black triangles. Sampled points are in dots with color progressing from black to white as the algorithm progresses.
#| fig-alt: Line graph of the same sinusoidal function as before but now there are dots from white to black along the line. There are more dots around the global minimum in later stages but still a spread of dots throughout the line.
ggplot() +
geom_line(aes(x = x, y = y), data = xydt) +
geom_point(aes(x = x, y = y), colour = "black", fill = "black", size = 2, pch = 24,
data = instance$archive$data[batch_nr == 1]) +
geom_point(aes(x = x, y = y, fill = batch_nr), size = 2, pch = 21, color = "black",
data = instance$archive$data[batch_nr > 1]) +
scale_fill_gradient(low = "black", high = "white") +
labs(colour = "Evaluation Number") +
theme_minimal() +
theme(legend.position = "bottom")
```
If we replicate running our BO algorithm ten times (with random initial designs and varying random seeds) and compare this to a random search, we can see that BO indeed performs much better and on average reaches the global optimum after around 15 function evaluations (@fig-bayesian-sinusoidal_bo_rs).
As expected, the performance for the initial design size is close to the performance of the random search.
```{r advanced_tuning_methods_and_black_box_optimization-035, eval=FALSE, echo=FALSE}
library(mlr3misc)
library(pammtools)
set.seed(2906)
results = map_dtr(1:10, function(replication) {
instance$archive$clear()
design = generate_design_random(instance$search_space, n = 4L)$data
instance$eval_batch(design)
optimizer$optimize(instance)
bo_result = instance$archive$data
bo_result[, nr_eval := seq_len(.N)]
bo_result[, y_min := cummin(y)]
bo_result[, method := "BO"]
instance$archive$clear()
opt("random_search", batch_size = 20)$optimize(instance)
rs_result = instance$archive$data
rs_result[, nr_eval := seq_len(.N)]
rs_result[, y_min := cummin(y)]
rs_result[, method := "Random Search"]
result = rbind(bo_result, rs_result, fill = TRUE)
result[, replication := replication]
result
})
mean_results = results[, .(mean_y_min = mean(y_min), se_y_min = sd(y_min) / sqrt(.N)), by = .(method, nr_eval)]
g = ggplot() +
geom_step(aes(x = nr_eval, y = mean_y_min, linetype = method, colour = method), data = mean_results) +
geom_stepribbon(aes(x = nr_eval, min = mean_y_min - se_y_min, max = mean_y_min + se_y_min, fill = method), colour = NA, alpha = 0.2, data = mean_results) +
scale_colour_manual(values = c("black", "darkgrey")) +
scale_fill_manual(values = c("black", "darkgrey")) +
labs(y = "Best Observed Function Value", x = "Number of Function Evaluations", colour = "Method", fill = "Method", linetype = "Method") +
theme_minimal() +
theme(legend.position = "bottom")
ggsave("Figures/bo_1d_sinusoidal_bo_rs.pdf", plot = g, width = 6, height = 4)
ggsave("Figures/bo_1d_sinusoidal_bo_rs.png", plot = g, width = 6, height = 4)
```
```{r advanced_tuning_methods_and_black_box_optimization-036, echo=FALSE}
#| echo: false
#| label: fig-bayesian-sinusoidal_bo_rs
#| fig-cap: Anytime performance of BO and random search on the 1D sinusoidal function given a budget of 20 function evaluations. Solid line depicts the best observed target value averaged over 10 replications. Ribbons represent standard errors.
#| fig-alt: Line graph with two lines. x-axis is 'Number of Function Evaluations' between 0 and 20, y-axis is 'Best Observed Function Value' between -1.5 and 0. Solid line is Bayesian optimization, dashed line is random search. At 0-5 evaluations the methods perform similarly and then BO converges to the optimum at around 15 evaluations whereas random search gets stuck before the optimum.
knitr::include_graphics("Figures/bo_1d_sinusoidal_bo_rs.png")
```
### Bayesian Optimization for HPO {#sec-bayesian-tuning}
`mlr3mbo` can be used for HPO by making use of `r ref("TunerMbo", aside = TRUE)`, which is a wrapper around `r ref("OptimizerMbo")` and works in the exact same way.
As an example, below we will tune the `cost` and `gamma` parameters of `lrn("classif.svm")` with a radial kernel on `tsk("sonar")` with three-fold CV.
We set up `tnr("mbo")` using the same objects constructed above and then run our tuning experiment as usual:
```{r advanced_tuning_methods_and_black_box_optimization-037}
tuner = tnr("mbo",
loop_function = bayesopt_ego,
surrogate = surrogate,
acq_function = acq_function,
acq_optimizer = acq_optimizer)
lrn_svm = lrn("classif.svm", kernel = "radial",
type = "C-classification",
cost = to_tune(1e-5, 1e5, logscale = TRUE),
gamma = to_tune(1e-5, 1e5, logscale = TRUE)
)
instance = tune(tuner, tsk("sonar"), lrn_svm, rsmp("cv", folds = 3),
msr("classif.ce"), 25)
instance$result
```
Multi-objective tuning is also possible with BO with algorithms using many different design choices, for example, whether they use a scalarization approach of objectives and only rely on a single surrogate model, or fit a surrogate model for each objective.
More details on multi-objective BO can for example be found in @Horn2015 or @Morales2022.
Below we will illustrate multi-objective tuning using the ParEGO [@knowles_2006] loop function.
ParEGO (`r ref("bayesopt_parego()")`) tackles multi-objective BO via a randomized scalarization approach and models a single scalarized objective function via a single surrogate model and then proceeds to find the next candidate for evaluation making use of a standard single-objective acquisition function such as the expected improvement.
Other compatible loop functions can be found by looking at the `"instance"` column of `r ref('mlr_loop_functions')`.
We will tune three parameters of a decision tree with respect to the true positive (maximize) and false positive (minimize) rates, the `r index('Pareto front', lower = FALSE)` is visualized in @fig-pareto-bayesopt.
```{r advanced_tuning_methods_and_black_box_optimization-038}
tuner = tnr("mbo",
loop_function = bayesopt_parego,
surrogate = surrogate,
acq_function = acq_function,
acq_optimizer = acq_optimizer)
lrn_rpart = lrn("classif.rpart",
cp = to_tune(1e-04, 1e-1),
minsplit = to_tune(2, 64),
maxdepth = to_tune(1, 30)
)
instance = tune(tuner, tsk("sonar"), lrn_rpart, rsmp("cv", folds = 3),
msrs(c("classif.tpr", "classif.fpr")), 25)
```
```{r advanced_tuning_methods_and_black_box_optimization-039, echo = FALSE, warning = FALSE}
#| label: fig-pareto-bayesopt
#| fig-cap: Pareto front of TPR and FPR obtained via ParEGO. White dots represent tested configurations, each black dot individually represents a Pareto-optimal configuration and all black dots together represent the Pareto front.
#| fig-alt: Scatter plot with classif.tpr on y-axis (between 0.75 and 1.00) and classif.fpr on x-axis (between 0.2 and 1.0). The Pareto front is shown as the set of points at roughly (0.23, 0.85), (0.24, 0.88), (0.25, 0.91), (0.30, 0.93), (0.35, 0.95), (0.40, 0.96), (0.8, 1.00).
ggplot(aes(y = classif.tpr, x = classif.fpr),
data = as.data.table(instance$archive)) +
geom_point(
shape = 21,
size = 3,
fill = "white",
color = "black",
alpha = 0.8,
stroke = 0.5) +
geom_step(
data = instance$archive$best(),
direction = "hv",
colour = "black",
linewidth = 1) +
geom_point(
data = instance$archive$best(),
shape = 21,
size = 3,
fill = "black",
colour = "white",
alpha = 0.8,
stroke = 0.5) +
theme_minimal()
```
### Noisy Bayesian Optimization {#sec-noisy-bayesian-optimization}
So far, we implicitly assumed that the black box function we are trying to optimize is deterministic, i.e., repeatedly evaluating the same point will always return the same objective function value.
However, real-world black box functions are often noisy, which means that repeatedly evaluating the same point will return different objective function values due to background noise on top of the black box function.
For example, if you were modeling a machine in a factory to estimate the rate of production, even if all parameters of the machine were controlled, we would still expect different performance at different times due to uncontrollable background factors such as environmental conditions.
In `bbotk`, you can mark an `r ref("Objective")` object as noisy by passing the `"noisy"` tag to the `properties` parameter, which allows us to use methods that can treat such objectives differently.
```{r advanced_tuning_methods_and_black_box_optimization-040}
sinus_1D_noisy = function(xs) {
y = 2 * xs$x * sin(14 * xs$x) + rnorm(1, mean = 0, sd = 0.1)
y
}
domain = ps(x = p_dbl(lower = 0, upper = 1))
codomain = ps(y = p_dbl(tags = "minimize"))
objective_noisy = ObjectiveRFun$new(sinus_1D_noisy,
domain = domain, codomain = codomain, properties = "noisy")
```
Noisy objectives can be treated in different ways:
1. A surrogate model can be used to incorporate the noise
2. An acquisition function can be used that respects noisiness
3. The final best point(s) after optimization (i.e., the `$result` field of the instance) can be chosen in a way to reflect noisiness
In the first case, instead of using an interpolating `r index('Gaussian process', lower = FALSE)`, we could instead use Gaussian process regression that estimates the measurement error by setting `nugget.estim = TRUE`:
```{r advanced_tuning_methods_and_black_box_optimization-041, output=FALSE}
srlrn(lrn("regr.km", nugget.estim = TRUE))