3.1 Hyperparameter Tuning

Hyperparameters are second-order parameters of machine learning models that, while often not explicitly optimized during the model estimation process, can have an important impact on the outcome and predictive performance of a model. Typically, hyperparameters are fixed before training a model. However, because the output of a model can be sensitive to the specification of hyperparameters, it is often recommended to make an informed decision about which hyperparameter settings may yield better model performance. In many cases, hyperparameter settings may be chosen a priori, but it can be advantageous to try different settings before fitting your model on the training data. This process is often called model ‘tuning.’

Hyperparameter tuning is supported via the mlr3tuning extension package. Below you can find an illustration of the process:

At the heart of mlr3tuning are the R6 classes:

3.1.1 The TuningInstance* Classes

The following sub-section examines the optimization of a simple classification tree on the Pima Indian Diabetes data set.

task = tsk("pima")
print(task)
## <TaskClassif:pima> (768 x 9)
## * Target: diabetes
## * Properties: twoclass
## * Features (8):
##   - dbl (8): age, glucose, insulin, mass, pedigree, pregnant, pressure,
##     triceps

We use the classification tree from rpart and choose a subset of the hyperparameters we want to tune. This is often referred to as the “tuning space.”

learner = lrn("classif.rpart")
learner$param_set
## <ParamSet>
##                 id    class lower upper      levels        default value
##  1:       minsplit ParamInt     1   Inf                         20      
##  2:      minbucket ParamInt     1   Inf             <NoDefault[3]>      
##  3:             cp ParamDbl     0     1                       0.01      
##  4:     maxcompete ParamInt     0   Inf                          4      
##  5:   maxsurrogate ParamInt     0   Inf                          5      
##  6:       maxdepth ParamInt     1    30                         30      
##  7:   usesurrogate ParamInt     0     2                          2      
##  8: surrogatestyle ParamInt     0     1                          0      
##  9:           xval ParamInt     0   Inf                         10     0
## 10:     keep_model ParamLgl    NA    NA  TRUE,FALSE          FALSE

Here, we opt to tune two parameters:

  • The complexity cp
  • The termination criterion minsplit

The tuning space needs to be bounded, therefore one has to set lower and upper bounds:

library("paradox")
search_space = ps(
  cp = p_dbl(lower = 0.001, upper = 0.1),
  minsplit = p_int(lower = 1, upper = 10)
)
search_space
## <ParamSet>
##          id    class lower upper levels        default value
## 1:       cp ParamDbl 0.001   0.1        <NoDefault[3]>      
## 2: minsplit ParamInt 1.000  10.0        <NoDefault[3]>

Next, we need to specify how to evaluate the performance. For this, we need to choose a resampling strategy and a performance measure.

hout = rsmp("holdout")
measure = msr("classif.ce")

Finally, one has to select the budget available, to solve this tuning instance. This is done by selecting one of the available Terminators:

For this short introduction, we specify a budget of 20 evaluations and then put everything together into a TuningInstanceSingleCrit:

library("mlr3tuning")

evals20 = trm("evals", n_evals = 20)

instance = TuningInstanceSingleCrit$new(
  task = task,
  learner = learner,
  resampling = hout,
  measure = measure,
  search_space = search_space,
  terminator = evals20
)
instance
## <TuningInstanceSingleCrit>
## * State:  Not optimized
## * Objective: <ObjectiveTuning:classif.rpart_on_pima>
## * Search Space:
## <ParamSet>
##          id    class lower upper levels        default value
## 1:       cp ParamDbl 0.001   0.1        <NoDefault[3]>      
## 2: minsplit ParamInt 1.000  10.0        <NoDefault[3]>      
## * Terminator: <TerminatorEvals>
## * Terminated: FALSE
## * Archive:
## <ArchiveTuning>
## Null data.table (0 rows and 0 cols)

To start the tuning, we still need to select how the optimization should take place. In other words, we need to choose the optimization algorithm via the Tuner class.

3.1.2 The Tuner Class

The following algorithms are currently implemented in mlr3tuning:

In this example, we will use a simple grid search with a grid resolution of 5.

tuner = tnr("grid_search", resolution = 5)

Since we have only numeric parameters, TunerGridSearch will create an equidistant grid between the respective upper and lower bounds. As we have two hyperparameters with a resolution of 5, the two-dimensional grid consists of \(5^2 = 25\) configurations. Each configuration serves as a hyperparameter setting for the previously defined Learner which is then fitted on the task using the provided Resampling. All configurations will be examined by the tuner (in a random order), until either all configurations are evaluated or the Terminator signals that the budget is exhausted.

3.1.3 Triggering the Tuning

To start the tuning, we simply pass the TuningInstanceSingleCrit to the $optimize() method of the initialized Tuner. The tuner proceeds as follows:

  1. The Tuner proposes at least one hyperparameter configuration (the Tuner may propose multiple points to improve parallelization, which can be controlled via the setting batch_size).
  2. For each configuration, the given Learner is fitted on the Task using the provided Resampling. All evaluations are stored in the archive of the TuningInstanceSingleCrit.
  3. The Terminator is queried if the budget is exhausted. If the budget is not exhausted, restart with 1) until it is.
  4. Determine the configuration with the best observed performance.
  5. Store the best configurations as result in the instance object. The best hyperparameter settings ($result_learner_param_vals) and the corresponding measured performance ($result_y) can be accessed from the instance.
tuner$optimize(instance)
## INFO  [00:16:18.166] [bbotk] Starting to optimize 2 parameter(s) with '<OptimizerGridSearch>' and '<TerminatorEvals> [n_evals=20]' 
## INFO  [00:16:18.203] [bbotk] Evaluating 1 configuration(s) 
## INFO  [00:16:18.407] [bbotk] Result of batch 1: 
## INFO  [00:16:18.409] [bbotk]     cp minsplit classif.ce                                uhash 
## INFO  [00:16:18.409] [bbotk]  0.001        5     0.2422 f4f1699e-312e-4164-a10a-ca762b897cdd 
## INFO  [00:16:18.410] [bbotk] Evaluating 1 configuration(s) 
## INFO  [00:16:18.598] [bbotk] Result of batch 2: 
## INFO  [00:16:18.599] [bbotk]       cp minsplit classif.ce                                uhash 
## INFO  [00:16:18.599] [bbotk]  0.02575        3      0.207 92f9513b-eb30-4a8b-8511-50507aff54f1 
## INFO  [00:16:18.601] [bbotk] Evaluating 1 configuration(s) 
## INFO  [00:16:18.685] [bbotk] Result of batch 3: 
## INFO  [00:16:18.687] [bbotk]       cp minsplit classif.ce                                uhash 
## INFO  [00:16:18.687] [bbotk]  0.02575        5      0.207 6cb720b5-be56-4bfc-85ed-f960f60c9a39 
## INFO  [00:16:18.688] [bbotk] Evaluating 1 configuration(s) 
## INFO  [00:16:18.776] [bbotk] Result of batch 4: 
## INFO  [00:16:18.777] [bbotk]     cp minsplit classif.ce                                uhash 
## INFO  [00:16:18.777] [bbotk]  0.001        8     0.2148 fa9f3ea7-e02b-4753-bf49-29e9b2108776 
## INFO  [00:16:18.778] [bbotk] Evaluating 1 configuration(s) 
## INFO  [00:16:18.854] [bbotk] Result of batch 5: 
## INFO  [00:16:18.855] [bbotk]   cp minsplit classif.ce                                uhash 
## INFO  [00:16:18.855] [bbotk]  0.1        1      0.207 ec042e94-c396-4959-b7b4-9a966357c39f 
## INFO  [00:16:18.856] [bbotk] Evaluating 1 configuration(s) 
## INFO  [00:16:18.937] [bbotk] Result of batch 6: 
## INFO  [00:16:18.938] [bbotk]      cp minsplit classif.ce                                uhash 
## INFO  [00:16:18.938] [bbotk]  0.0505        8      0.207 cb03bc06-4f83-44fa-a1f8-4425a8b6f2cf 
## INFO  [00:16:18.939] [bbotk] Evaluating 1 configuration(s) 
## INFO  [00:16:19.017] [bbotk] Result of batch 7: 
## INFO  [00:16:19.019] [bbotk]      cp minsplit classif.ce                                uhash 
## INFO  [00:16:19.019] [bbotk]  0.0505        3      0.207 eda4a60a-ebe5-4f27-9da6-4582e68ba7a2 
## INFO  [00:16:19.020] [bbotk] Evaluating 1 configuration(s) 
## INFO  [00:16:19.094] [bbotk] Result of batch 8: 
## INFO  [00:16:19.096] [bbotk]       cp minsplit classif.ce                                uhash 
## INFO  [00:16:19.096] [bbotk]  0.07525       10      0.207 c1cff488-d9b1-4c13-a2ec-ec98c45c18fc 
## INFO  [00:16:19.097] [bbotk] Evaluating 1 configuration(s) 
## INFO  [00:16:19.176] [bbotk] Result of batch 9: 
## INFO  [00:16:19.178] [bbotk]   cp minsplit classif.ce                                uhash 
## INFO  [00:16:19.178] [bbotk]  0.1        8      0.207 5f346728-0227-46d4-90b6-9c9b2affb3ae 
## INFO  [00:16:19.179] [bbotk] Evaluating 1 configuration(s) 
## INFO  [00:16:19.257] [bbotk] Result of batch 10: 
## INFO  [00:16:19.259] [bbotk]      cp minsplit classif.ce                                uhash 
## INFO  [00:16:19.259] [bbotk]  0.0505        5      0.207 42844cbc-0e37-4f6b-b267-8c86476aa5f3 
## INFO  [00:16:19.260] [bbotk] Evaluating 1 configuration(s) 
## INFO  [00:16:19.337] [bbotk] Result of batch 11: 
## INFO  [00:16:19.338] [bbotk]      cp minsplit classif.ce                                uhash 
## INFO  [00:16:19.338] [bbotk]  0.0505       10      0.207 dc11ce51-4fa7-40b9-81fa-637149ce371e 
## INFO  [00:16:19.339] [bbotk] Evaluating 1 configuration(s) 
## INFO  [00:16:19.415] [bbotk] Result of batch 12: 
## INFO  [00:16:19.417] [bbotk]     cp minsplit classif.ce                                uhash 
## INFO  [00:16:19.417] [bbotk]  0.001        3     0.2539 831f1b75-7c05-4b08-b7e0-d10f9eb1dfd6 
## INFO  [00:16:19.418] [bbotk] Evaluating 1 configuration(s) 
## INFO  [00:16:19.502] [bbotk] Result of batch 13: 
## INFO  [00:16:19.503] [bbotk]   cp minsplit classif.ce                                uhash 
## INFO  [00:16:19.503] [bbotk]  0.1        3      0.207 84635347-8d3d-4a14-b2de-8eb8b611fc48 
## INFO  [00:16:19.504] [bbotk] Evaluating 1 configuration(s) 
## INFO  [00:16:19.578] [bbotk] Result of batch 14: 
## INFO  [00:16:19.580] [bbotk]       cp minsplit classif.ce                                uhash 
## INFO  [00:16:19.580] [bbotk]  0.07525        5      0.207 5852c38e-851e-488c-9d7e-66c517549d70 
## INFO  [00:16:19.581] [bbotk] Evaluating 1 configuration(s) 
## INFO  [00:16:19.664] [bbotk] Result of batch 15: 
## INFO  [00:16:19.666] [bbotk]       cp minsplit classif.ce                                uhash 
## INFO  [00:16:19.666] [bbotk]  0.02575       10      0.207 1e4e7a36-442b-48cc-8db3-af3057a0dda2 
## INFO  [00:16:19.667] [bbotk] Evaluating 1 configuration(s) 
## INFO  [00:16:19.740] [bbotk] Result of batch 16: 
## INFO  [00:16:19.742] [bbotk]   cp minsplit classif.ce                                uhash 
## INFO  [00:16:19.742] [bbotk]  0.1       10      0.207 0074fbf3-1bb3-4c6b-bf6f-d98d461bf9f2 
## INFO  [00:16:19.743] [bbotk] Evaluating 1 configuration(s) 
## INFO  [00:16:19.822] [bbotk] Result of batch 17: 
## INFO  [00:16:19.823] [bbotk]       cp minsplit classif.ce                                uhash 
## INFO  [00:16:19.823] [bbotk]  0.07525        1      0.207 d958a256-18c6-4d27-ab2a-629aca688324 
## INFO  [00:16:19.824] [bbotk] Evaluating 1 configuration(s) 
## INFO  [00:16:19.904] [bbotk] Result of batch 18: 
## INFO  [00:16:19.905] [bbotk]      cp minsplit classif.ce                                uhash 
## INFO  [00:16:19.905] [bbotk]  0.0505        1      0.207 d8c4e5fc-6f1e-4ed0-aa0d-fd606774a0b4 
## INFO  [00:16:19.906] [bbotk] Evaluating 1 configuration(s) 
## INFO  [00:16:19.980] [bbotk] Result of batch 19: 
## INFO  [00:16:19.981] [bbotk]       cp minsplit classif.ce                                uhash 
## INFO  [00:16:19.981] [bbotk]  0.07525        3      0.207 0e7d77e8-4762-446d-a70a-46d8545e6834 
## INFO  [00:16:19.982] [bbotk] Evaluating 1 configuration(s) 
## INFO  [00:16:20.066] [bbotk] Result of batch 20: 
## INFO  [00:16:20.069] [bbotk]       cp minsplit classif.ce                                uhash 
## INFO  [00:16:20.069] [bbotk]  0.02575        1      0.207 37806107-bd7d-4f68-b5c2-63178878b992 
## INFO  [00:16:20.075] [bbotk] Finished optimizing after 20 evaluation(s) 
## INFO  [00:16:20.075] [bbotk] Result: 
## INFO  [00:16:20.077] [bbotk]       cp minsplit learner_param_vals  x_domain classif.ce 
## INFO  [00:16:20.077] [bbotk]  0.02575        3          <list[3]> <list[2]>      0.207
##         cp minsplit learner_param_vals  x_domain classif.ce
## 1: 0.02575        3          <list[3]> <list[2]>      0.207
instance$result_learner_param_vals
## $xval
## [1] 0
## 
## $cp
## [1] 0.02575
## 
## $minsplit
## [1] 3
instance$result_y
## classif.ce 
##      0.207

One can investigate all resamplings which were undertaken, as they are stored in the archive of the TuningInstanceSingleCrit and can be accessed by using as.data.table():

as.data.table(instance$archive)
##          cp minsplit classif.ce                                uhash
##  1: 0.00100        5     0.2422 f4f1699e-312e-4164-a10a-ca762b897cdd
##  2: 0.02575        3     0.2070 92f9513b-eb30-4a8b-8511-50507aff54f1
##  3: 0.02575        5     0.2070 6cb720b5-be56-4bfc-85ed-f960f60c9a39
##  4: 0.00100        8     0.2148 fa9f3ea7-e02b-4753-bf49-29e9b2108776
##  5: 0.10000        1     0.2070 ec042e94-c396-4959-b7b4-9a966357c39f
##  6: 0.05050        8     0.2070 cb03bc06-4f83-44fa-a1f8-4425a8b6f2cf
##  7: 0.05050        3     0.2070 eda4a60a-ebe5-4f27-9da6-4582e68ba7a2
##  8: 0.07525       10     0.2070 c1cff488-d9b1-4c13-a2ec-ec98c45c18fc
##  9: 0.10000        8     0.2070 5f346728-0227-46d4-90b6-9c9b2affb3ae
## 10: 0.05050        5     0.2070 42844cbc-0e37-4f6b-b267-8c86476aa5f3
## 11: 0.05050       10     0.2070 dc11ce51-4fa7-40b9-81fa-637149ce371e
## 12: 0.00100        3     0.2539 831f1b75-7c05-4b08-b7e0-d10f9eb1dfd6
## 13: 0.10000        3     0.2070 84635347-8d3d-4a14-b2de-8eb8b611fc48
## 14: 0.07525        5     0.2070 5852c38e-851e-488c-9d7e-66c517549d70
## 15: 0.02575       10     0.2070 1e4e7a36-442b-48cc-8db3-af3057a0dda2
## 16: 0.10000       10     0.2070 0074fbf3-1bb3-4c6b-bf6f-d98d461bf9f2
## 17: 0.07525        1     0.2070 d958a256-18c6-4d27-ab2a-629aca688324
## 18: 0.05050        1     0.2070 d8c4e5fc-6f1e-4ed0-aa0d-fd606774a0b4
## 19: 0.07525        3     0.2070 0e7d77e8-4762-446d-a70a-46d8545e6834
## 20: 0.02575        1     0.2070 37806107-bd7d-4f68-b5c2-63178878b992
##               timestamp batch_nr x_domain_cp x_domain_minsplit
##  1: 2021-03-04 00:16:18        1     0.00100                 5
##  2: 2021-03-04 00:16:18        2     0.02575                 3
##  3: 2021-03-04 00:16:18        3     0.02575                 5
##  4: 2021-03-04 00:16:18        4     0.00100                 8
##  5: 2021-03-04 00:16:18        5     0.10000                 1
##  6: 2021-03-04 00:16:18        6     0.05050                 8
##  7: 2021-03-04 00:16:19        7     0.05050                 3
##  8: 2021-03-04 00:16:19        8     0.07525                10
##  9: 2021-03-04 00:16:19        9     0.10000                 8
## 10: 2021-03-04 00:16:19       10     0.05050                 5
## 11: 2021-03-04 00:16:19       11     0.05050                10
## 12: 2021-03-04 00:16:19       12     0.00100                 3
## 13: 2021-03-04 00:16:19       13     0.10000                 3
## 14: 2021-03-04 00:16:19       14     0.07525                 5
## 15: 2021-03-04 00:16:19       15     0.02575                10
## 16: 2021-03-04 00:16:19       16     0.10000                10
## 17: 2021-03-04 00:16:19       17     0.07525                 1
## 18: 2021-03-04 00:16:19       18     0.05050                 1
## 19: 2021-03-04 00:16:19       19     0.07525                 3
## 20: 2021-03-04 00:16:20       20     0.02575                 1

In sum, the grid search evaluated 20/25 different configurations of the grid in a random order before the Terminator stopped the tuning.

The associated resampling iterations can be accessed in the BenchmarkResult:

instance$archive$benchmark_result
## <BenchmarkResult> of 20 rows with 20 resampling runs
##  nr task_id    learner_id resampling_id iters warnings errors
##   1    pima classif.rpart       holdout     1        0      0
##   2    pima classif.rpart       holdout     1        0      0
##   3    pima classif.rpart       holdout     1        0      0
##   4    pima classif.rpart       holdout     1        0      0
##   5    pima classif.rpart       holdout     1        0      0
##   6    pima classif.rpart       holdout     1        0      0
##   7    pima classif.rpart       holdout     1        0      0
##   8    pima classif.rpart       holdout     1        0      0
##   9    pima classif.rpart       holdout     1        0      0
##  10    pima classif.rpart       holdout     1        0      0
##  11    pima classif.rpart       holdout     1        0      0
##  12    pima classif.rpart       holdout     1        0      0
##  13    pima classif.rpart       holdout     1        0      0
##  14    pima classif.rpart       holdout     1        0      0
##  15    pima classif.rpart       holdout     1        0      0
##  16    pima classif.rpart       holdout     1        0      0
##  17    pima classif.rpart       holdout     1        0      0
##  18    pima classif.rpart       holdout     1        0      0
##  19    pima classif.rpart       holdout     1        0      0
##  20    pima classif.rpart       holdout     1        0      0

The uhash column links the resampling iterations to the evaluated configurations stored in instance$archive$data. This allows e.g. to score the included ResampleResults on a different measure.

instance$archive$benchmark_result$score(msr("classif.acc"))
##                                    uhash nr              task task_id
##  1: f4f1699e-312e-4164-a10a-ca762b897cdd  1 <TaskClassif[45]>    pima
##  2: 92f9513b-eb30-4a8b-8511-50507aff54f1  2 <TaskClassif[45]>    pima
##  3: 6cb720b5-be56-4bfc-85ed-f960f60c9a39  3 <TaskClassif[45]>    pima
##  4: fa9f3ea7-e02b-4753-bf49-29e9b2108776  4 <TaskClassif[45]>    pima
##  5: ec042e94-c396-4959-b7b4-9a966357c39f  5 <TaskClassif[45]>    pima
##  6: cb03bc06-4f83-44fa-a1f8-4425a8b6f2cf  6 <TaskClassif[45]>    pima
##  7: eda4a60a-ebe5-4f27-9da6-4582e68ba7a2  7 <TaskClassif[45]>    pima
##  8: c1cff488-d9b1-4c13-a2ec-ec98c45c18fc  8 <TaskClassif[45]>    pima
##  9: 5f346728-0227-46d4-90b6-9c9b2affb3ae  9 <TaskClassif[45]>    pima
## 10: 42844cbc-0e37-4f6b-b267-8c86476aa5f3 10 <TaskClassif[45]>    pima
## 11: dc11ce51-4fa7-40b9-81fa-637149ce371e 11 <TaskClassif[45]>    pima
## 12: 831f1b75-7c05-4b08-b7e0-d10f9eb1dfd6 12 <TaskClassif[45]>    pima
## 13: 84635347-8d3d-4a14-b2de-8eb8b611fc48 13 <TaskClassif[45]>    pima
## 14: 5852c38e-851e-488c-9d7e-66c517549d70 14 <TaskClassif[45]>    pima
## 15: 1e4e7a36-442b-48cc-8db3-af3057a0dda2 15 <TaskClassif[45]>    pima
## 16: 0074fbf3-1bb3-4c6b-bf6f-d98d461bf9f2 16 <TaskClassif[45]>    pima
## 17: d958a256-18c6-4d27-ab2a-629aca688324 17 <TaskClassif[45]>    pima
## 18: d8c4e5fc-6f1e-4ed0-aa0d-fd606774a0b4 18 <TaskClassif[45]>    pima
## 19: 0e7d77e8-4762-446d-a70a-46d8545e6834 19 <TaskClassif[45]>    pima
## 20: 37806107-bd7d-4f68-b5c2-63178878b992 20 <TaskClassif[45]>    pima
##                       learner    learner_id              resampling
##  1: <LearnerClassifRpart[34]> classif.rpart <ResamplingHoldout[19]>
##  2: <LearnerClassifRpart[34]> classif.rpart <ResamplingHoldout[19]>
##  3: <LearnerClassifRpart[34]> classif.rpart <ResamplingHoldout[19]>
##  4: <LearnerClassifRpart[34]> classif.rpart <ResamplingHoldout[19]>
##  5: <LearnerClassifRpart[34]> classif.rpart <ResamplingHoldout[19]>
##  6: <LearnerClassifRpart[34]> classif.rpart <ResamplingHoldout[19]>
##  7: <LearnerClassifRpart[34]> classif.rpart <ResamplingHoldout[19]>
##  8: <LearnerClassifRpart[34]> classif.rpart <ResamplingHoldout[19]>
##  9: <LearnerClassifRpart[34]> classif.rpart <ResamplingHoldout[19]>
## 10: <LearnerClassifRpart[34]> classif.rpart <ResamplingHoldout[19]>
## 11: <LearnerClassifRpart[34]> classif.rpart <ResamplingHoldout[19]>
## 12: <LearnerClassifRpart[34]> classif.rpart <ResamplingHoldout[19]>
## 13: <LearnerClassifRpart[34]> classif.rpart <ResamplingHoldout[19]>
## 14: <LearnerClassifRpart[34]> classif.rpart <ResamplingHoldout[19]>
## 15: <LearnerClassifRpart[34]> classif.rpart <ResamplingHoldout[19]>
## 16: <LearnerClassifRpart[34]> classif.rpart <ResamplingHoldout[19]>
## 17: <LearnerClassifRpart[34]> classif.rpart <ResamplingHoldout[19]>
## 18: <LearnerClassifRpart[34]> classif.rpart <ResamplingHoldout[19]>
## 19: <LearnerClassifRpart[34]> classif.rpart <ResamplingHoldout[19]>
## 20: <LearnerClassifRpart[34]> classif.rpart <ResamplingHoldout[19]>
##     resampling_id iteration              prediction classif.acc
##  1:       holdout         1 <PredictionClassif[19]>      0.7578
##  2:       holdout         1 <PredictionClassif[19]>      0.7930
##  3:       holdout         1 <PredictionClassif[19]>      0.7930
##  4:       holdout         1 <PredictionClassif[19]>      0.7852
##  5:       holdout         1 <PredictionClassif[19]>      0.7930
##  6:       holdout         1 <PredictionClassif[19]>      0.7930
##  7:       holdout         1 <PredictionClassif[19]>      0.7930
##  8:       holdout         1 <PredictionClassif[19]>      0.7930
##  9:       holdout         1 <PredictionClassif[19]>      0.7930
## 10:       holdout         1 <PredictionClassif[19]>      0.7930
## 11:       holdout         1 <PredictionClassif[19]>      0.7930
## 12:       holdout         1 <PredictionClassif[19]>      0.7461
## 13:       holdout         1 <PredictionClassif[19]>      0.7930
## 14:       holdout         1 <PredictionClassif[19]>      0.7930
## 15:       holdout         1 <PredictionClassif[19]>      0.7930
## 16:       holdout         1 <PredictionClassif[19]>      0.7930
## 17:       holdout         1 <PredictionClassif[19]>      0.7930
## 18:       holdout         1 <PredictionClassif[19]>      0.7930
## 19:       holdout         1 <PredictionClassif[19]>      0.7930
## 20:       holdout         1 <PredictionClassif[19]>      0.7930

Now the optimized hyperparameters can take the previously created Learner, set the returned hyperparameters and train it on the full dataset.

learner$param_set$values = instance$result_learner_param_vals
learner$train(task)

The trained model can now be used to make a prediction on external data. Note that predicting on observations present in the task, should be avoided. The model has seen these observations already during tuning and therefore results would be statistically biased. Hence, the resulting performance measure would be over-optimistic. Instead, to get statistically unbiased performance estimates for the current task, nested resampling is required.

3.1.4 Automating the Tuning

The AutoTuner wraps a learner and augments it with an automatic tuning for a given set of hyperparameters. Because the AutoTuner itself inherits from the Learner base class, it can be used like any other learner. Analogously to the previous subsection, a new classification tree learner is created. This classification tree learner automatically tunes the parameters cp and minsplit using an inner resampling (holdout). We create a terminator which allows 10 evaluations, and use a simple random search as tuning algorithm:

library("paradox")
library("mlr3tuning")

learner = lrn("classif.rpart")
search_space = ps(
  cp = p_dbl(lower = 0.001, upper = 0.1),
  minsplit = p_int(lower = 1, upper = 10)
)
terminator = trm("evals", n_evals = 10)
tuner = tnr("random_search")

at = AutoTuner$new(
  learner = learner,
  resampling = rsmp("holdout"),
  measure = msr("classif.ce"),
  search_space = search_space,
  terminator = terminator,
  tuner = tuner
)
at
## <AutoTuner:classif.rpart.tuned>
## * Model: -
## * Parameters: list()
## * Packages: rpart
## * Predict Type: response
## * Feature types: logical, integer, numeric, factor, ordered
## * Properties: importance, missings, multiclass, selected_features,
##   twoclass, weights

We can now use the learner like any other learner, calling the $train() and $predict() method. This time however, we pass it to benchmark() to compare the tuner to a classification tree without tuning. This way, the AutoTuner will do its resampling for tuning on the training set of the respective split of the outer resampling. The learner then undertakes predictions using the test set of the outer resampling. This yields unbiased performance measures, as the observations in the test set have not been used during tuning or fitting of the respective learner. This is called nested resampling.

To compare the tuned learner with the learner that uses default values, we can use benchmark():

grid = benchmark_grid(
  task = tsk("pima"),
  learner = list(at, lrn("classif.rpart")),
  resampling = rsmp("cv", folds = 3)
)

# avoid console output from mlr3tuning
logger = lgr::get_logger("bbotk")
logger$set_threshold("warn")

bmr = benchmark(grid)
bmr$aggregate(msrs(c("classif.ce", "time_train")))
##    nr      resample_result task_id          learner_id resampling_id iters
## 1:  1 <ResampleResult[21]>    pima classif.rpart.tuned            cv     3
## 2:  2 <ResampleResult[21]>    pima       classif.rpart            cv     3
##    classif.ce time_train
## 1:     0.2917          0
## 2:     0.2708          0

Note that we do not expect any differences compared to the non-tuned approach for multiple reasons:

  • the task is too easy
  • the task is rather small, and thus prone to overfitting
  • the tuning budget (10 evaluations) is small
  • rpart does not benefit that much from tuning