library(mlr3misc)
library(utils)
library(mlr3tuningspaces)
library(data.table)
source("R/bibentries.R")
writeLines(toBibtex(bibentries), "references.bib")

lgr::get_logger("mlr3")$set_threshold("warn")
lgr::get_logger("bbotk")$set_threshold("warn")
set.seed(0)
options(
  datatable.print.nrows = 10,
  datatable.print.class = FALSE,
  datatable.print.keys = FALSE,
  datatable.print.trunc.cols = TRUE,
  width = 100)

# mute load messages
library(bbotk)
library(mlr3verse)
library(mlr3hyperband)
library(mlr3learners)

mlr3hyperband

Package website: release | dev

r-cmd-check CRAN Status StackOverflow Mattermost

mlr3hyperband adds the optimization algorithms Successive Halving [@jamieson_2016] and Hyperband [@li_2018] to the mlr3 ecosystem. The implementation in mlr3hyperband features improved scheduling and parallelizes the evaluation of configurations. The package includes tuners for hyperparameter optimization in mlr3tuning and optimizers for black-box optimization in bbotk.

Resources

There are several sections about hyperparameter optimization in the mlr3book.

The gallery features a series of case studies on Hyperband.

Installation

Install the last release from CRAN:

install.packages("mlr3hyperband")

Install the development version from GitHub:

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

Examples

We optimize the hyperparameters of an XGBoost model on the Sonar data set. The number of boosting rounds nrounds is the fidelity parameter. We tag this parameter with "budget" in the search space.

library(mlr3hyperband)
library(mlr3learners)

learner = lrn("classif.xgboost",
  nrounds           = to_tune(p_int(27, 243, 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 use the tune() function to run the optimization.

instance = tune(
  tnr("hyperband", eta = 3),
  task = tsk("pima"),
  learner = learner,
  resampling = rsmp("cv", folds = 3),
  measures = msr("classif.ce")
)

The instance contains the best-performing hyperparameter configuration.

instance$result

The archive contains all evaluated hyperparameter configurations. Hyperband adds the "stage" and "braket".

as.data.table(instance$archive)[, .(stage, bracket, classif.ce, nrounds)]

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"))

References



mlr-org/mlr3hyperband documentation built on Feb. 12, 2025, 5:33 p.m.