mlr3tuning

Hyperparameter optimization package of the mlr3 ecosystem

54
5
R

output: github_document

lgr::get_logger("mlr3")$set_threshold("warn")
lgr::get_logger("bbotk")$set_threshold("warn")
set.seed(1)
options(
    datatable.print.nrows = 10,
    datatable.print.class = FALSE,
    datatable.print.keys = FALSE,
    width = 100)
# mute load messages
library("mlr3tuning")

mlr3tuning

Package website: release | dev

r-cmd-check
CRAN Status
StackOverflow
Mattermost

mlr3tuning is the hyperparameter optimization package of the mlr3 ecosystem.
It features highly configurable search spaces via the paradox package and finds optimal hyperparameter configurations for any mlr3 learner.
mlr3tuning works with several optimization algorithms e.g. Random Search, Iterated Racing, Bayesian Optimization (in mlr3mbo) and Hyperband (in mlr3hyperband).
Moreover, it can automatically optimize learners and estimate the performance of optimized models with nested resampling.
The package is built on the optimization framework bbotk.

Extension packages

mlr3tuning is extended by the following packages.

  • mlr3tuningspaces is a collection of search spaces from scientific articles for commonly used learners.
  • mlr3hyperband adds the Hyperband and Successive Halving algorithm.
  • mlr3mbo adds Bayesian Optimization methods.

Resources

There are several sections about hyperparameter optimization in the mlr3book.

The gallery features a collection of case studies and demos about optimization.

The cheatsheet summarizes the most important functions of mlr3tuning.

Installation

Install the last release from CRAN:

install.packages("mlr3tuning")

Install the development version from GitHub:

remotes::install_github("mlr-org/mlr3tuning")

Examples

We optimize the cost and gamma hyperparameters of a support vector machine on the Sonar data set.

library("mlr3learners")
library("mlr3tuning")

learner = lrn("classif.svm",
  cost  = to_tune(1e-5, 1e5, logscale = TRUE),
  gamma = to_tune(1e-5, 1e5, logscale = TRUE),
  kernel = "radial",
  type = "C-classification"
)

We construct a tuning instance with the ti() function.
The tuning instance describes the tuning problem.

instance = ti(
  task = tsk("sonar"),
  learner = learner,
  resampling = rsmp("cv", folds = 3),
  measures = msr("classif.ce"),
  terminator = trm("none")
)
instance

We select a simple grid search as the optimization algorithm.

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

To start the tuning, we simply pass the tuning instance to the tuner.

tuner$optimize(instance)

The tuner returns the best hyperparameter configuration and the corresponding measured performance.

The archive contains all evaluated hyperparameter configurations.

as.data.table(instance$archive)[, .(cost, gamma, classif.ce, batch_nr, resample_result)]

The mlr3viz package visualizes tuning results.

library(mlr3viz)

autoplot(instance, type = "surface")
p = autoplot(instance, type = "surface")
ggplot2::ggsave(filename = "plot.png", plot = p, units = "px", width = 600,  height = 500, scale = 5)

We fit a final model with optimized hyperparameters to make predictions on new data.

learner$param_set$values = instance$result_learner_param_vals
learner$train(tsk("sonar"))