How to tune integer hyperparameters with tuners that can only propose real numbers.

`Tuner`

for real-valued search spaces are not able to tune on integer hyperparameters. However, it is possible to round the real values proposed by a `Tuner`

to integers before passing them to the learner in the evaluation. We show how to apply a parameter transformation to a `ParamSet`

and use this set in the tuning process.

We load the mlr3verse package which pulls in the most important packages for this example.

We initialize the random number generator with a fixed seed for reproducibility, and decrease the verbosity of the logger to keep the output clearly represented.

```
set.seed(7832)
lgr::get_logger("mlr3")$set_threshold("warn")
lgr::get_logger("bbotk")$set_threshold("warn")
```

In this example, we use the k-Nearest-Neighbor classification learner. We want to tune the integer-valued hyperparameter `k`

which defines the numbers of neighbors.

```
id class lower upper levels default
1: k ParamInt 1 Inf 7
```

We choose generalized simulated annealing as tuning strategy. The `param_classes`

field of `TunerGenSA`

states that the tuner only supports real-valued (`ParamDbl`

) hyperparameter tuning.

```
<TunerGenSA>
* Parameters: list()
* Parameter classes: ParamDbl
* Properties: single-crit
* Packages: mlr3tuning, bbotk, GenSA
```

To get integer-valued hyperparameter values for `k`

, we construct a search space with a transformation function. The `as.integer()`

function converts any real valued number to an integer by removing the decimal places.

We start the tuning and compare the results of the search space to the results in the space of the learners hyperparameter set.

The optimal `k`

is still a real number in the search space.

```
instance$result_x_search_space
```

```
k
1: 3.82686
```

However, in the learners hyperparameters space, `k`

is an integer value.

```
instance$result_x_domain
```

```
$k
[1] 3
```

The archive shows us that for all real-valued `k`

proposed by GenSA, an integer-valued `k`

in the learner hyperparameter space (`x_domain_k`

) was created.

```
as.data.table(instance$archive)
```

k | classif.ce | x_domain_k |
---|---|---|

3.826860 | 0.06 | 3 |

5.996323 | 0.06 | 5 |

5.941332 | 0.06 | 5 |

3.826860 | 0.06 | 3 |

3.826861 | 0.06 | 3 |

3.826859 | 0.06 | 3 |

6.566057 | 0.06 | 6 |

3.516582 | 0.06 | 3 |

7.871788 | 0.06 | 7 |

4.049171 | 0.06 | 4 |

6.704470 | 0.06 | 6 |

3.941998 | 0.06 | 3 |

3.945753 | 0.06 | 3 |

5.737044 | 0.06 | 5 |

6.661272 | 0.06 | 6 |

7.984981 | 0.06 | 7 |

3.503817 | 0.06 | 3 |

4.050002 | 0.06 | 4 |

7.610021 | 0.06 | 7 |

6.116269 | 0.06 | 6 |

Internally, `TunerGenSA`

was given the parameter types of the search space and therefore suggested real numbers for `k`

. Before the performance of the different `k`

values was evaluated, the transformation function of the `search_space`

parameter set was called and `k`

was transformed to an integer value.

Note that the tuner is not aware of the transformation. This has two problematic consequences: First, the tuner might propose different real valued configurations that after rounding end up to be already evaluated configurations and we end up with re-evaluating the same hyperparameter configuration. This is only problematic, if we only optimze integer parameters. Second, the rounding introduces discontinuities which can be problematic for some tuners.

We successfully tuned a integer-valued hyperparameter with `TunerGenSA`

which is only suitable for an real-valued search space. This technique is not limited to tuning problems. `Optimizer`

in bbotk can be also used in the same way to produce points with integer parameters.

For attribution, please cite this work as

Becker (2021, Jan. 19). mlr-org: Integer Hyperparameters in Tuners for Real-valued Search Spaces. Retrieved from https://mlr-org.github.io/mlr-org-website/gallery/2021-01-19-integer-hyperparameters-in-tuners-for-real-valued-search-spaces/

BibTeX citation

@misc{becker2021integer, author = {Becker, Marc}, title = {mlr-org: Integer Hyperparameters in Tuners for Real-valued Search Spaces}, url = {https://mlr-org.github.io/mlr-org-website/gallery/2021-01-19-integer-hyperparameters-in-tuners-for-real-valued-search-spaces/}, year = {2021} }