Title: | Hyperparameter Optimization for 'mlr3' |
---|---|
Description: | 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. |
Authors: | Marc Becker [cre, aut] , Michel Lang [aut] , Jakob Richter [aut] , Bernd Bischl [aut] , Daniel Schalk [aut] |
Maintainer: | Marc Becker <[email protected]> |
License: | LGPL-3 |
Version: | 1.2.0 |
Built: | 2024-11-08 16:18:24 UTC |
Source: | https://github.com/mlr-org/mlr3tuning |
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.
Maintainer: Marc Becker [email protected] (ORCID)
Authors:
Michel Lang [email protected] (ORCID)
Jakob Richter [email protected] (ORCID)
Bernd Bischl [email protected] (ORCID)
Daniel Schalk [email protected] (ORCID)
Useful links:
Report bugs at https://github.com/mlr-org/mlr3tuning/issues
The 'ArchiveAsyncTuning“ stores all evaluated hyperparameter configurations and performance scores in a rush::Rush database.
The ArchiveAsyncTuning is a connector to a rush::Rush database.
The table ($data
) has the following columns:
One column for each hyperparameter of the search space ($search_space
).
One (list-)column for the internal_tuned_values
One column for each performance measure ($codomain
).
x_domain
(list()
)
Lists of (transformed) hyperparameter values that are passed to the learner.
runtime_learners
(numeric(1)
)
Sum of training and predict times logged in learners per mlr3::ResampleResult / evaluation.
This does not include potential overhead time.
timestamp
(POSIXct
)
Time stamp when the evaluation was logged into the archive.
batch_nr
(integer(1)
)
Hyperparameters are evaluated in batches.
Each batch has a unique batch number.
For analyzing the tuning results, it is recommended to pass the ArchiveAsyncTuning to as.data.table()
.
The returned data table contains the mlr3::ResampleResult for each hyperparameter evaluation.
as.data.table.ArchiveTuning(x, unnest = "x_domain", exclude_columns = "uhash", measures = NULL)
Returns a tabular view of all evaluated hyperparameter configurations.
ArchiveAsyncTuning -> data.table::data.table()
unnest
(character()
)
Transforms list columns to separate columns. Set to NULL
if no column should be unnested.
exclude_columns
(character()
)
Exclude columns from table. Set to NULL
if no column should be excluded.
measures
(List of mlr3::Measure)
Score hyperparameter configurations on additional measures.
bbotk::Archive
-> bbotk::ArchiveAsync
-> ArchiveAsyncTuning
internal_search_space
(paradox::ParamSet)
The search space containing those parameters that are internally optimized by the mlr3::Learner
.
benchmark_result
(mlr3::BenchmarkResult)
Benchmark result.
bbotk::Archive$format()
bbotk::Archive$help()
bbotk::ArchiveAsync$best()
bbotk::ArchiveAsync$clear()
bbotk::ArchiveAsync$data_with_state()
bbotk::ArchiveAsync$nds_selection()
bbotk::ArchiveAsync$pop_point()
bbotk::ArchiveAsync$push_failed_point()
bbotk::ArchiveAsync$push_points()
bbotk::ArchiveAsync$push_result()
bbotk::ArchiveAsync$push_running_point()
new()
Creates a new instance of this R6 class.
ArchiveAsyncTuning$new( search_space, codomain, rush, internal_search_space = NULL )
search_space
(paradox::ParamSet)
Hyperparameter search space. If NULL
(default), the search space is
constructed from the paradox::TuneToken of the learner's parameter set
(learner$param_set).
codomain
(bbotk::Codomain)
Specifies codomain of objective function i.e. a set of performance measures.
Internally created from provided mlr3::Measures.
rush
(Rush
)
If a rush instance is supplied, the tuning runs without batches.
internal_search_space
(paradox::ParamSet or NULL
)
The internal search space.
check_values
(logical(1)
)
If TRUE
(default), hyperparameter configurations are check for validity.
learner()
Retrieve mlr3::Learner of the i-th evaluation, by position or by unique hash uhash
.
i
and uhash
are mutually exclusive.
Learner does not contain a model. Use $learners()
to get learners with models.
ArchiveAsyncTuning$learner(i = NULL, uhash = NULL)
i
(integer(1)
)
The iteration value to filter for.
uhash
(logical(1)
)
The uhash
value to filter for.
learners()
Retrieve list of trained mlr3::Learner objects of the i-th evaluation, by position or by unique hash uhash
.
i
and uhash
are mutually exclusive.
ArchiveAsyncTuning$learners(i = NULL, uhash = NULL)
i
(integer(1)
)
The iteration value to filter for.
uhash
(logical(1)
)
The uhash
value to filter for.
learner_param_vals()
Retrieve param values of the i-th evaluation, by position or by unique hash uhash
.
i
and uhash
are mutually exclusive.
ArchiveAsyncTuning$learner_param_vals(i = NULL, uhash = NULL)
i
(integer(1)
)
The iteration value to filter for.
uhash
(logical(1)
)
The uhash
value to filter for.
predictions()
Retrieve list of mlr3::Prediction objects of the i-th evaluation, by position or by unique hash uhash
.
i
and uhash
are mutually exclusive.
ArchiveAsyncTuning$predictions(i = NULL, uhash = NULL)
i
(integer(1)
)
The iteration value to filter for.
uhash
(logical(1)
)
The uhash
value to filter for.
resample_result()
Retrieve mlr3::ResampleResult of the i-th evaluation, by position or by unique hash uhash
.
i
and uhash
are mutually exclusive.
ArchiveAsyncTuning$resample_result(i = NULL, uhash = NULL)
i
(integer(1)
)
The iteration value to filter for.
uhash
(logical(1)
)
The uhash
value to filter for.
print()
Printer.
ArchiveAsyncTuning$print()
...
(ignored).
clone()
The objects of this class are cloneable with this method.
ArchiveAsyncTuning$clone(deep = FALSE)
deep
Whether to make a deep clone.
The ArchiveBatchTuning
stores all evaluated hyperparameter configurations and performance scores in a data.table::data.table()
.
The ArchiveBatchTuning is a container around a data.table::data.table()
.
Each row corresponds to a single evaluation of a hyperparameter configuration.
See the section on Data Structure for more information.
The archive stores additionally a mlr3::BenchmarkResult ($benchmark_result
) that records the resampling experiments.
Each experiment corresponds to to a single evaluation of a hyperparameter configuration.
The table ($data
) and the benchmark result ($benchmark_result
) are linked by the uhash
column.
If the archive is passed to as.data.table()
, both are joined automatically.
The table ($data
) has the following columns:
One column for each hyperparameter of the search space ($search_space
).
One (list-)column for the internal_tuned_values
One column for each performance measure ($codomain
).
x_domain
(list()
)
Lists of (transformed) hyperparameter values that are passed to the learner.
runtime_learners
(numeric(1)
)
Sum of training and predict times logged in learners per mlr3::ResampleResult / evaluation.
This does not include potential overhead time.
timestamp
(POSIXct
)
Time stamp when the evaluation was logged into the archive.
batch_nr
(integer(1)
)
Hyperparameters are evaluated in batches.
Each batch has a unique batch number.
uhash
(character(1)
)
Connects each hyperparameter configuration to the resampling experiment stored in the mlr3::BenchmarkResult.
For analyzing the tuning results, it is recommended to pass the ArchiveBatchTuning to as.data.table()
.
The returned data table is joined with the benchmark result which adds the mlr3::ResampleResult for each hyperparameter evaluation.
The archive provides various getters (e.g. $learners()
) to ease the access.
All getters extract by position (i
) or unique hash (uhash
).
For a complete list of all getters see the methods section.
The benchmark result ($benchmark_result
) allows to score the hyperparameter configurations again on a different measure.
Alternatively, measures can be supplied to as.data.table()
.
The mlr3viz package provides visualizations for tuning results.
as.data.table.ArchiveTuning(x, unnest = "x_domain", exclude_columns = "uhash", measures = NULL)
Returns a tabular view of all evaluated hyperparameter configurations.
ArchiveBatchTuning -> data.table::data.table()
unnest
(character()
)
Transforms list columns to separate columns. Set to NULL
if no column should be unnested.
exclude_columns
(character()
)
Exclude columns from table. Set to NULL
if no column should be excluded.
measures
(List of mlr3::Measure)
Score hyperparameter configurations on additional measures.
bbotk::Archive
-> bbotk::ArchiveBatch
-> ArchiveBatchTuning
benchmark_result
(mlr3::BenchmarkResult)
Benchmark result.
internal_search_space
(paradox::ParamSet)
The search space containing those parameters that are internally optimized by the mlr3::Learner
.
new()
Creates a new instance of this R6 class.
ArchiveBatchTuning$new( search_space, codomain, check_values = FALSE, internal_search_space = NULL )
search_space
(paradox::ParamSet)
Hyperparameter search space. If NULL
(default), the search space is
constructed from the paradox::TuneToken of the learner's parameter set
(learner$param_set).
codomain
(bbotk::Codomain)
Specifies codomain of objective function i.e. a set of performance measures.
Internally created from provided mlr3::Measures.
check_values
(logical(1)
)
If TRUE
(default), hyperparameter configurations are check for validity.
internal_search_space
(paradox::ParamSet or NULL
)
The internal search space.
learner()
Retrieve mlr3::Learner of the i-th evaluation, by position or by unique hash uhash
.
i
and uhash
are mutually exclusive.
Learner does not contain a model. Use $learners()
to get learners with models.
ArchiveBatchTuning$learner(i = NULL, uhash = NULL)
i
(integer(1)
)
The iteration value to filter for.
uhash
(logical(1)
)
The uhash
value to filter for.
learners()
Retrieve list of trained mlr3::Learner objects of the i-th evaluation, by position or by unique hash uhash
.
i
and uhash
are mutually exclusive.
ArchiveBatchTuning$learners(i = NULL, uhash = NULL)
i
(integer(1)
)
The iteration value to filter for.
uhash
(logical(1)
)
The uhash
value to filter for.
learner_param_vals()
Retrieve param values of the i-th evaluation, by position or by unique hash uhash
.
i
and uhash
are mutually exclusive.
ArchiveBatchTuning$learner_param_vals(i = NULL, uhash = NULL)
i
(integer(1)
)
The iteration value to filter for.
uhash
(logical(1)
)
The uhash
value to filter for.
predictions()
Retrieve list of mlr3::Prediction objects of the i-th evaluation, by position or by unique hash uhash
.
i
and uhash
are mutually exclusive.
ArchiveBatchTuning$predictions(i = NULL, uhash = NULL)
i
(integer(1)
)
The iteration value to filter for.
uhash
(logical(1)
)
The uhash
value to filter for.
resample_result()
Retrieve mlr3::ResampleResult of the i-th evaluation, by position or by unique hash uhash
.
i
and uhash
are mutually exclusive.
ArchiveBatchTuning$resample_result(i = NULL, uhash = NULL)
i
(integer(1)
)
The iteration value to filter for.
uhash
(logical(1)
)
The uhash
value to filter for.
print()
Printer.
ArchiveBatchTuning$print()
...
(ignored).
clone()
The objects of this class are cloneable with this method.
ArchiveBatchTuning$clone(deep = FALSE)
deep
Whether to make a deep clone.
Convert object to a search space.
as_search_space(x, ...) ## S3 method for class 'Learner' as_search_space(x, ...) ## S3 method for class 'ParamSet' as_search_space(x, ...)
as_search_space(x, ...) ## S3 method for class 'Learner' as_search_space(x, ...) ## S3 method for class 'ParamSet' as_search_space(x, ...)
x |
( |
... |
(any) |
Convert object to a Tuner or a list of Tuner.
as_tuner(x, ...) ## S3 method for class 'Tuner' as_tuner(x, clone = FALSE, ...) as_tuners(x, ...) ## Default S3 method: as_tuners(x, ...) ## S3 method for class 'list' as_tuners(x, ...)
as_tuner(x, ...) ## S3 method for class 'Tuner' as_tuner(x, clone = FALSE, ...) as_tuners(x, ...) ## Default S3 method: as_tuners(x, ...) ## S3 method for class 'list' as_tuners(x, ...)
x |
(any) |
... |
(any) |
clone |
( |
Assertions for CallbackAsyncTuning class.
assert_async_tuning_callback(callback, null_ok = FALSE) assert_async_tuning_callbacks(callbacks)
assert_async_tuning_callback(callback, null_ok = FALSE) assert_async_tuning_callbacks(callbacks)
callback |
|
null_ok |
( |
callbacks |
(list of CallbackAsyncTuning). |
[CallbackAsyncTuning | List of CallbackAsyncTunings.
Assertions for CallbackBatchTuning class.
assert_batch_tuning_callback(callback, null_ok = FALSE) assert_batch_tuning_callbacks(callbacks)
assert_batch_tuning_callback(callback, null_ok = FALSE) assert_batch_tuning_callbacks(callbacks)
callback |
|
null_ok |
( |
callbacks |
(list of CallbackBatchTuning). |
[CallbackBatchTuning | List of CallbackBatchTunings.
The AutoTuner wraps a mlr3::Learner and augments it with an automatic tuning process for a given set of hyperparameters.
The auto_tuner()
function creates an AutoTuner object.
auto_tuner( tuner, learner, resampling, measure = NULL, term_evals = NULL, term_time = NULL, terminator = NULL, search_space = NULL, internal_search_space = NULL, store_tuning_instance = TRUE, store_benchmark_result = TRUE, store_models = FALSE, check_values = FALSE, callbacks = NULL, rush = NULL, id = NULL )
auto_tuner( tuner, learner, resampling, measure = NULL, term_evals = NULL, term_time = NULL, terminator = NULL, search_space = NULL, internal_search_space = NULL, store_tuning_instance = TRUE, store_benchmark_result = TRUE, store_models = FALSE, check_values = FALSE, callbacks = NULL, rush = NULL, id = NULL )
tuner |
(Tuner) |
learner |
(mlr3::Learner) |
resampling |
(mlr3::Resampling) |
measure |
(mlr3::Measure) |
term_evals |
( |
term_time |
( |
terminator |
(bbotk::Terminator) |
search_space |
(paradox::ParamSet) |
internal_search_space |
(paradox::ParamSet or |
store_tuning_instance |
( |
store_benchmark_result |
( |
store_models |
( |
check_values |
( |
callbacks |
(list of mlr3misc::Callback) |
rush |
( |
id |
( |
The AutoTuner is a mlr3::Learner which wraps another mlr3::Learner and performs the following steps during $train()
:
The hyperparameters of the wrapped (inner) learner are trained on the training data via resampling. The tuning can be specified by providing a Tuner, a bbotk::Terminator, a search space as paradox::ParamSet, a mlr3::Resampling and a mlr3::Measure.
The best found hyperparameter configuration is set as hyperparameters for the wrapped (inner) learner stored in at$learner
.
Access the tuned hyperparameters via at$tuning_result
.
A final model is fit on the complete training data using the now parametrized wrapped learner.
The respective model is available via field at$learner$model
.
During $predict()
the AutoTuner
just calls the predict method of the wrapped (inner) learner.
A set timeout is disabled while fitting the final model.
If no measure is passed, the default measure is used. The default measure depends on the task type.
Task | Default Measure | Package |
"classif" |
"classif.ce" |
mlr3 |
"regr" |
"regr.mse" |
mlr3 |
"surv" |
"surv.cindex" |
mlr3proba |
"dens" |
"dens.logloss" |
mlr3proba |
"classif_st" |
"classif.ce" |
mlr3spatial |
"regr_st" |
"regr.mse" |
mlr3spatial |
"clust" |
"clust.dunn" |
mlr3cluster |
There are several sections about hyperparameter optimization in the mlr3book.
Getting started with hyperparameter optimization.
An overview of all tuners can be found on our website.
Tune a support vector machine on the Sonar data set.
Learn about tuning spaces.
Estimate the model performance with nested resampling.
Learn about multi-objective optimization.
Simultaneously optimize hyperparameters and use early stopping with XGBoost.
Automate the tuning.
The gallery features a collection of case studies and demos about optimization.
Learn more advanced methods with the Practical Tuning Series.
Learn about hotstarting models.
Run the default hyperparameter configuration of learners as a baseline.
Use the Hyperband optimizer with different budget parameters.
The cheatsheet summarizes the most important functions of mlr3tuning.
Nested resampling is performed by passing an AutoTuner to mlr3::resample()
or mlr3::benchmark()
.
To access the inner resampling results, set store_tuning_instance = TRUE
and execute mlr3::resample()
or mlr3::benchmark()
with store_models = TRUE
(see examples).
The mlr3::Resampling passed to the AutoTuner is meant to be the inner resampling, operating on the training set of an arbitrary outer resampling.
For this reason, the inner resampling should be not instantiated.
If an instantiated resampling is passed, the AutoTuner fails when a row id of the inner resampling is not present in the training set of the outer resampling.
at = auto_tuner( tuner = tnr("random_search"), learner = lrn("classif.rpart", cp = to_tune(1e-04, 1e-1, logscale = TRUE)), resampling = rsmp ("holdout"), measure = msr("classif.ce"), term_evals = 4) at$train(tsk("pima"))
at = auto_tuner( tuner = tnr("random_search"), learner = lrn("classif.rpart", cp = to_tune(1e-04, 1e-1, logscale = TRUE)), resampling = rsmp ("holdout"), measure = msr("classif.ce"), term_evals = 4) at$train(tsk("pima"))
The AutoTuner wraps a mlr3::Learner and augments it with an automatic tuning process for a given set of hyperparameters.
The auto_tuner()
function creates an AutoTuner object.
The AutoTuner is a mlr3::Learner which wraps another mlr3::Learner and performs the following steps during $train()
:
The hyperparameters of the wrapped (inner) learner are trained on the training data via resampling. The tuning can be specified by providing a Tuner, a bbotk::Terminator, a search space as paradox::ParamSet, a mlr3::Resampling and a mlr3::Measure.
The best found hyperparameter configuration is set as hyperparameters for the wrapped (inner) learner stored in at$learner
.
Access the tuned hyperparameters via at$tuning_result
.
A final model is fit on the complete training data using the now parametrized wrapped learner.
The respective model is available via field at$learner$model
.
During $predict()
the AutoTuner
just calls the predict method of the wrapped (inner) learner.
A set timeout is disabled while fitting the final model.
The AutoTuner
itself does not have the "validation"
property.
To enable validation during the tuning, set the $validate
field of the tuned learner.
This is also possible via set_validate()
.
Nested resampling is performed by passing an AutoTuner to mlr3::resample()
or mlr3::benchmark()
.
To access the inner resampling results, set store_tuning_instance = TRUE
and execute mlr3::resample()
or mlr3::benchmark()
with store_models = TRUE
(see examples).
The mlr3::Resampling passed to the AutoTuner is meant to be the inner resampling, operating on the training set of an arbitrary outer resampling.
For this reason, the inner resampling should be not instantiated.
If an instantiated resampling is passed, the AutoTuner fails when a row id of the inner resampling is not present in the training set of the outer resampling.
If no measure is passed, the default measure is used. The default measure depends on the task type.
Task | Default Measure | Package |
"classif" |
"classif.ce" |
mlr3 |
"regr" |
"regr.mse" |
mlr3 |
"surv" |
"surv.cindex" |
mlr3proba |
"dens" |
"dens.logloss" |
mlr3proba |
"classif_st" |
"classif.ce" |
mlr3spatial |
"regr_st" |
"regr.mse" |
mlr3spatial |
"clust" |
"clust.dunn" |
mlr3cluster |
There are several sections about hyperparameter optimization in the mlr3book.
Getting started with hyperparameter optimization.
An overview of all tuners can be found on our website.
Tune a support vector machine on the Sonar data set.
Learn about tuning spaces.
Estimate the model performance with nested resampling.
Learn about multi-objective optimization.
Simultaneously optimize hyperparameters and use early stopping with XGBoost.
Automate the tuning.
The gallery features a collection of case studies and demos about optimization.
Learn more advanced methods with the Practical Tuning Series.
Learn about hotstarting models.
Run the default hyperparameter configuration of learners as a baseline.
Use the Hyperband optimizer with different budget parameters.
The cheatsheet summarizes the most important functions of mlr3tuning.
mlr3::Learner
-> AutoTuner
instance_args
(list()
)
All arguments from construction to create the TuningInstanceBatchSingleCrit.
tuner
(Tuner)
Optimization algorithm.
internal_valid_scores
Retrieves the inner validation scores as a named list()
.
Returns NULL
if learner is not trained yet.
archive
ArchiveBatchTuning
Archive of the TuningInstanceBatchSingleCrit.
learner
(mlr3::Learner)
Trained learner
tuning_instance
(TuningInstanceAsyncSingleCrit | TuningInstanceBatchSingleCrit)
Internally created tuning instance with all intermediate results.
tuning_result
(data.table::data.table)
Short-cut to result
from tuning instance.
predict_type
(character(1)
)
Stores the currently active predict type, e.g. "response"
.
Must be an element of $predict_types
.
hash
(character(1)
)
Hash (unique identifier) for this object.
phash
(character(1)
)
Hash (unique identifier) for this partial object, excluding some components which are varied systematically during tuning (parameter values) or feature selection (feature names).
new()
Creates a new instance of this R6 class.
AutoTuner$new( tuner, learner, resampling, measure = NULL, terminator, search_space = NULL, internal_search_space = NULL, store_tuning_instance = TRUE, store_benchmark_result = TRUE, store_models = FALSE, check_values = FALSE, callbacks = NULL, rush = NULL, id = NULL )
tuner
(Tuner)
Optimization algorithm.
learner
(mlr3::Learner)
Learner to tune.
resampling
(mlr3::Resampling)
Resampling that is used to evaluate the performance of the hyperparameter configurations.
Uninstantiated resamplings are instantiated during construction so that all configurations are evaluated on the same data splits.
Already instantiated resamplings are kept unchanged.
Specialized Tuner change the resampling e.g. to evaluate a hyperparameter configuration on different data splits.
This field, however, always returns the resampling passed in construction.
measure
(mlr3::Measure)
Measure to optimize. If NULL
, default measure is used.
terminator
(bbotk::Terminator)
Stop criterion of the tuning process.
search_space
(paradox::ParamSet)
Hyperparameter search space. If NULL
(default), the search space is
constructed from the paradox::TuneToken of the learner's parameter set
(learner$param_set).
internal_search_space
(paradox::ParamSet or NULL
)
The internal search space.
store_tuning_instance
(logical(1)
)
If TRUE
(default), stores the internally created TuningInstanceBatchSingleCrit with all intermediate results in slot $tuning_instance
.
store_benchmark_result
(logical(1)
)
If TRUE
(default), store resample result of evaluated hyperparameter
configurations in archive as mlr3::BenchmarkResult.
store_models
(logical(1)
)
If TRUE
, fitted models are stored in the benchmark result
(archive$benchmark_result
). If store_benchmark_result = FALSE
, models
are only stored temporarily and not accessible after the tuning. This
combination is needed for measures that require a model.
check_values
(logical(1)
)
If TRUE
, hyperparameter values are checked before evaluation and
performance scores after. If FALSE
(default), values are unchecked but
computational overhead is reduced.
callbacks
(list of mlr3misc::Callback)
List of callbacks.
rush
(Rush
)
If a rush instance is supplied, the tuning runs without batches.
id
(character(1)
)
Identifier for the new instance.
base_learner()
Extracts the base learner from nested learner objects like GraphLearner
in mlr3pipelines.
If recursive = 0
, the (tuned) learner is returned.
AutoTuner$base_learner(recursive = Inf)
recursive
(integer(1)
)
Depth of recursion for multiple nested objects.
importance()
The importance scores of the final model.
AutoTuner$importance()
Named numeric()
.
selected_features()
The selected features of the final model.
AutoTuner$selected_features()
character()
.
oob_error()
The out-of-bag error of the final model.
AutoTuner$oob_error()
numeric(1)
.
loglik()
The log-likelihood of the final model.
AutoTuner$loglik()
logLik
.
Printer.
print()
AutoTuner$print()
...
(ignored).
marshal()
Marshal the learner.
AutoTuner$marshal(...)
...
(any)
Additional parameters.
self
unmarshal()
Unmarshal the learner.
AutoTuner$unmarshal(...)
...
(any)
Additional parameters.
self
marshaled()
Whether the learner is marshaled.
AutoTuner$marshaled()
clone()
The objects of this class are cloneable with this method.
AutoTuner$clone(deep = FALSE)
deep
Whether to make a deep clone.
# Automatic Tuning # split to train and external set task = tsk("penguins") split = partition(task, ratio = 0.8) # load learner and set search space learner = lrn("classif.rpart", cp = to_tune(1e-04, 1e-1, logscale = TRUE) ) # create auto tuner at = auto_tuner( tuner = tnr("random_search"), learner = learner, resampling = rsmp ("holdout"), measure = msr("classif.ce"), term_evals = 4) # tune hyperparameters and fit final model at$train(task, row_ids = split$train) # predict with final model at$predict(task, row_ids = split$test) # show tuning result at$tuning_result # model slot contains trained learner and tuning instance at$model # shortcut trained learner at$learner # shortcut tuning instance at$tuning_instance # Nested Resampling at = auto_tuner( tuner = tnr("random_search"), learner = learner, resampling = rsmp ("holdout"), measure = msr("classif.ce"), term_evals = 4) resampling_outer = rsmp("cv", folds = 3) rr = resample(task, at, resampling_outer, store_models = TRUE) # retrieve inner tuning results. extract_inner_tuning_results(rr) # performance scores estimated on the outer resampling rr$score() # unbiased performance of the final model trained on the full data set rr$aggregate()
# Automatic Tuning # split to train and external set task = tsk("penguins") split = partition(task, ratio = 0.8) # load learner and set search space learner = lrn("classif.rpart", cp = to_tune(1e-04, 1e-1, logscale = TRUE) ) # create auto tuner at = auto_tuner( tuner = tnr("random_search"), learner = learner, resampling = rsmp ("holdout"), measure = msr("classif.ce"), term_evals = 4) # tune hyperparameters and fit final model at$train(task, row_ids = split$train) # predict with final model at$predict(task, row_ids = split$test) # show tuning result at$tuning_result # model slot contains trained learner and tuning instance at$model # shortcut trained learner at$learner # shortcut tuning instance at$tuning_instance # Nested Resampling at = auto_tuner( tuner = tnr("random_search"), learner = learner, resampling = rsmp ("holdout"), measure = msr("classif.ce"), term_evals = 4) resampling_outer = rsmp("cv", folds = 3) rr = resample(task, at, resampling_outer, store_models = TRUE) # retrieve inner tuning results. extract_inner_tuning_results(rr) # performance scores estimated on the outer resampling rr$score() # unbiased performance of the final model trained on the full data set rr$aggregate()
Function to create a CallbackAsyncTuning.
Predefined callbacks are stored in the dictionary mlr_callbacks and can be retrieved with clbk()
.
Tuning callbacks can be called from different stages of the tuning process.
The stages are prefixed with on_*
.
Start Tuning - on_optimization_begin Start Worker - on_worker_begin Start Optimization on Worker - on_optimizer_before_eval Start Evaluation - on_eval_after_xs - on_eval_after_resample - on_eval_before_archive End Evaluation - on_optimizer_after_eval End Optimization on Worker - on_worker_end End Worker - on_tuning_result_begin - on_result_begin - on_result_end - on_optimization_end End Tuning
See also the section on parameters for more information on the stages. A tuning callback works with ContextAsyncTuning.
callback_async_tuning( id, label = NA_character_, man = NA_character_, on_optimization_begin = NULL, on_worker_begin = NULL, on_optimizer_before_eval = NULL, on_eval_after_xs = NULL, on_eval_after_resample = NULL, on_eval_before_archive = NULL, on_optimizer_after_eval = NULL, on_worker_end = NULL, on_tuning_result_begin = NULL, on_result_begin = NULL, on_result_end = NULL, on_result = NULL, on_optimization_end = NULL )
callback_async_tuning( id, label = NA_character_, man = NA_character_, on_optimization_begin = NULL, on_worker_begin = NULL, on_optimizer_before_eval = NULL, on_eval_after_xs = NULL, on_eval_after_resample = NULL, on_eval_before_archive = NULL, on_optimizer_after_eval = NULL, on_worker_end = NULL, on_tuning_result_begin = NULL, on_result_begin = NULL, on_result_end = NULL, on_result = NULL, on_optimization_end = NULL )
id |
( |
label |
( |
man |
( |
on_optimization_begin |
( |
on_worker_begin |
( |
on_optimizer_before_eval |
( |
on_eval_after_xs |
( |
on_eval_after_resample |
( |
on_eval_before_archive |
( |
on_optimizer_after_eval |
( |
on_worker_end |
( |
on_tuning_result_begin |
( |
on_result_begin |
( |
on_result_end |
( |
on_result |
( |
on_optimization_end |
( |
When implementing a callback, each function must have two arguments named callback
and context
.
A callback can write data to the state ($state
), e.g. settings that affect the callback itself.
Tuning callbacks access ContextAsyncTuning.
Function to create a CallbackBatchTuning.
Predefined callbacks are stored in the dictionary mlr_callbacks and can be retrieved with clbk()
.
Tuning callbacks can be called from different stages of the tuning process.
The stages are prefixed with on_*
.
Start Tuning - on_optimization_begin Start Tuner Batch - on_optimizer_before_eval Start Evaluation - on_eval_after_design - on_eval_after_benchmark - on_eval_before_archive End Evaluation - on_optimizer_after_eval End Tuner Batch - on_tuning_result_begin - on_result_begin - on_result_end - on_optimization_end End Tuning
See also the section on parameters for more information on the stages. A tuning callback works with ContextBatchTuning.
callback_batch_tuning( id, label = NA_character_, man = NA_character_, on_optimization_begin = NULL, on_optimizer_before_eval = NULL, on_eval_after_design = NULL, on_eval_after_benchmark = NULL, on_eval_before_archive = NULL, on_optimizer_after_eval = NULL, on_tuning_result_begin = NULL, on_result_begin = NULL, on_result_end = NULL, on_result = NULL, on_optimization_end = NULL )
callback_batch_tuning( id, label = NA_character_, man = NA_character_, on_optimization_begin = NULL, on_optimizer_before_eval = NULL, on_eval_after_design = NULL, on_eval_after_benchmark = NULL, on_eval_before_archive = NULL, on_optimizer_after_eval = NULL, on_tuning_result_begin = NULL, on_result_begin = NULL, on_result_end = NULL, on_result = NULL, on_optimization_end = NULL )
id |
( |
label |
( |
man |
( |
on_optimization_begin |
( |
on_optimizer_before_eval |
( |
on_eval_after_design |
( |
on_eval_after_benchmark |
( |
on_eval_before_archive |
( |
on_optimizer_after_eval |
( |
on_tuning_result_begin |
( |
on_result_begin |
( |
on_result_end |
( |
on_result |
( |
on_optimization_end |
( |
When implementing a callback, each function must have two arguments named callback
and context
.
A callback can write data to the state ($state
), e.g. settings that affect the callback itself.
Tuning callbacks access ContextBatchTuning.
# write archive to disk callback_batch_tuning("mlr3tuning.backup", on_optimization_end = function(callback, context) { saveRDS(context$instance$archive, "archive.rds") } )
# write archive to disk callback_batch_tuning("mlr3tuning.backup", on_optimization_end = function(callback, context) { saveRDS(context$instance$archive, "archive.rds") } )
Specialized bbotk::CallbackAsync for asynchronous tuning.
Callbacks allow to customize the behavior of processes in mlr3tuning.
The callback_async_tuning()
function creates a CallbackAsyncTuning.
Predefined callbacks are stored in the dictionary mlr_callbacks and can be retrieved with clbk()
.
For more information on tuning callbacks see callback_async_tuning()
.
mlr3misc::Callback
-> bbotk::CallbackAsync
-> CallbackAsyncTuning
on_eval_after_xs
(function()
)
Stage called after xs is passed.
Called in ObjectiveTuningAsync$eval()
.
on_eval_after_resample
(function()
)
Stage called after hyperparameter configurations are evaluated.
Called in ObjectiveTuningAsync$eval()
.
on_eval_before_archive
(function()
)
Stage called before performance values are written to the archive.
Called in ObjectiveTuningAsync$eval()
.
on_tuning_result_begin
(function()
)
Stage called before the results are written.
Called in TuningInstance*$assign_result()
.
clone()
The objects of this class are cloneable with this method.
CallbackAsyncTuning$clone(deep = FALSE)
deep
Whether to make a deep clone.
Specialized bbotk::CallbackBatch for batch tuning.
Callbacks allow to customize the behavior of processes in mlr3tuning.
The callback_batch_tuning()
function creates a CallbackBatchTuning.
Predefined callbacks are stored in the dictionary mlr_callbacks and can be retrieved with clbk()
.
For more information on tuning callbacks see callback_batch_tuning()
.
mlr3misc::Callback
-> bbotk::CallbackBatch
-> CallbackBatchTuning
on_eval_after_design
(function()
)
Stage called after design is created.
Called in ObjectiveTuningBatch$eval_many()
.
on_eval_after_benchmark
(function()
)
Stage called after hyperparameter configurations are evaluated.
Called in ObjectiveTuningBatch$eval_many()
.
on_eval_before_archive
(function()
)
Stage called before performance values are written to the archive.
Called in ObjectiveTuningBatch$eval_many()
.
on_tuning_result_begin
(function()
)
Stage called before the results are written.
Called in TuningInstance*$assign_result()
.
clone()
The objects of this class are cloneable with this method.
CallbackBatchTuning$clone(deep = FALSE)
deep
Whether to make a deep clone.
# write archive to disk callback_batch_tuning("mlr3tuning.backup", on_optimization_end = function(callback, context) { saveRDS(context$instance$archive, "archive.rds") } )
# write archive to disk callback_batch_tuning("mlr3tuning.backup", on_optimization_end = function(callback, context) { saveRDS(context$instance$archive, "archive.rds") } )
A CallbackAsyncTuning accesses and modifies data during the optimization via the ContextAsyncTuning
.
See the section on active bindings for a list of modifiable objects.
See callback_async_tuning()
for a list of stages that access ContextAsyncTuning
.
Changes to $instance
and $optimizer
in the stages executed on the workers are not reflected in the main process.
mlr3misc::Context
-> bbotk::ContextAsync
-> ContextAsyncTuning
xs_learner
(list())
The hyperparameter configuration currently evaluated.
Contains the values on the learner scale i.e. transformations are applied.
resample_result
(mlr3::BenchmarkResult)
The resample result of the hyperparameter configuration currently evaluated.
aggregated_performance
(list()
)
Aggregated performance scores and training time of the evaluated hyperparameter configuration.
This list is passed to the archive.
A callback can add additional elements which are also written to the archive.
result_learner_param_vals
(list())
The learner parameter values passed to instance$assign_result()
.
clone()
The objects of this class are cloneable with this method.
ContextAsyncTuning$clone(deep = FALSE)
deep
Whether to make a deep clone.
A CallbackBatchTuning accesses and modifies data during the optimization via the ContextBatchTuning
.
See the section on active bindings for a list of modifiable objects.
See callback_batch_tuning()
for a list of stages that access ContextBatchTuning
.
mlr3misc::Context
-> bbotk::ContextBatch
-> ContextBatchTuning
xss
(list())
The hyperparameter configurations of the latest batch.
Contains the values on the learner scale i.e. transformations are applied.
See $xdt
for the untransformed values.
design
(data.table::data.table)
The benchmark design of the latest batch.
benchmark_result
(mlr3::BenchmarkResult)
The benchmark result of the latest batch.
aggregated_performance
(data.table::data.table)
Aggregated performance scores and training time of the latest batch.
This data table is passed to the archive.
A callback can add additional columns which are also written to the archive.
result_learner_param_vals
(list())
The learner parameter values passed to instance$assign_result()
.
clone()
The objects of this class are cloneable with this method.
ContextBatchTuning$clone(deep = FALSE)
deep
Whether to make a deep clone.
Extract inner tuning archives of nested resampling.
Implemented for mlr3::ResampleResult and mlr3::BenchmarkResult.
The function iterates over the AutoTuner objects and binds the tuning archives to a data.table::data.table()
.
AutoTuner must be initialized with store_tuning_instance = TRUE
and mlr3::resample()
or mlr3::benchmark()
must be called with store_models = TRUE
.
extract_inner_tuning_archives( x, unnest = "x_domain", exclude_columns = "uhash" )
extract_inner_tuning_archives( x, unnest = "x_domain", exclude_columns = "uhash" )
x |
|
unnest |
( |
exclude_columns |
( |
The returned data table has the following columns:
experiment
(integer(1))
Index, giving the according row number in the original benchmark grid.
iteration
(integer(1))
Iteration of the outer resampling.
One column for each hyperparameter of the search spaces.
One column for each performance measure.
runtime_learners
(numeric(1)
)
Sum of training and predict times logged in learners per mlr3::ResampleResult / evaluation.
This does not include potential overhead time.
timestamp
(POSIXct
)
Time stamp when the evaluation was logged into the archive.
batch_nr
(integer(1)
)
Hyperparameters are evaluated in batches.
Each batch has a unique batch number.
x_domain
(list()
)
List of transformed hyperparameter values.
By default this column is unnested.
x_domain_*
(any
)
Separate column for each transformed hyperparameter.
resample_result
(mlr3::ResampleResult)
Resample result of the inner resampling.
task_id
(character(1)
).
learner_id
(character(1)
).
resampling_id
(character(1)
).
# Nested Resampling on Palmer Penguins Data Set learner = lrn("classif.rpart", cp = to_tune(1e-04, 1e-1, logscale = TRUE)) # create auto tuner at = auto_tuner( tuner = tnr("random_search"), learner = learner, resampling = rsmp ("holdout"), measure = msr("classif.ce"), term_evals = 4) resampling_outer = rsmp("cv", folds = 2) rr = resample(tsk("iris"), at, resampling_outer, store_models = TRUE) # extract inner archives extract_inner_tuning_archives(rr)
# Nested Resampling on Palmer Penguins Data Set learner = lrn("classif.rpart", cp = to_tune(1e-04, 1e-1, logscale = TRUE)) # create auto tuner at = auto_tuner( tuner = tnr("random_search"), learner = learner, resampling = rsmp ("holdout"), measure = msr("classif.ce"), term_evals = 4) resampling_outer = rsmp("cv", folds = 2) rr = resample(tsk("iris"), at, resampling_outer, store_models = TRUE) # extract inner archives extract_inner_tuning_archives(rr)
Extract inner tuning results of nested resampling. Implemented for mlr3::ResampleResult and mlr3::BenchmarkResult.
extract_inner_tuning_results(x, tuning_instance, ...) ## S3 method for class 'ResampleResult' extract_inner_tuning_results(x, tuning_instance = FALSE, ...) ## S3 method for class 'BenchmarkResult' extract_inner_tuning_results(x, tuning_instance = FALSE, ...)
extract_inner_tuning_results(x, tuning_instance, ...) ## S3 method for class 'ResampleResult' extract_inner_tuning_results(x, tuning_instance = FALSE, ...) ## S3 method for class 'BenchmarkResult' extract_inner_tuning_results(x, tuning_instance = FALSE, ...)
x |
|
tuning_instance |
( |
... |
(any) |
The function iterates over the AutoTuner objects and binds the tuning results to a data.table::data.table()
.
The AutoTuner must be initialized with store_tuning_instance = TRUE
and mlr3::resample()
or mlr3::benchmark()
must be called with store_models = TRUE
.
Optionally, the tuning instance can be added for each iteration.
The returned data table has the following columns:
experiment
(integer(1))
Index, giving the according row number in the original benchmark grid.
iteration
(integer(1))
Iteration of the outer resampling.
One column for each hyperparameter of the search spaces.
One column for each performance measure.
learner_param_vals
(list()
)
Hyperparameter values used by the learner.
Includes fixed and proposed hyperparameter values.
x_domain
(list()
)
List of transformed hyperparameter values.
tuning_instance
(TuningInstanceBatchSingleCrit | TuningInstanceBatchMultiCrit)
Optionally, tuning instances.
task_id
(character(1)
).
learner_id
(character(1)
).
resampling_id
(character(1)
).
# Nested Resampling on Palmer Penguins Data Set learner = lrn("classif.rpart", cp = to_tune(1e-04, 1e-1, logscale = TRUE)) # create auto tuner at = auto_tuner( tuner = tnr("random_search"), learner = learner, resampling = rsmp ("holdout"), measure = msr("classif.ce"), term_evals = 4) resampling_outer = rsmp("cv", folds = 2) rr = resample(tsk("iris"), at, resampling_outer, store_models = TRUE) # extract inner results extract_inner_tuning_results(rr)
# Nested Resampling on Palmer Penguins Data Set learner = lrn("classif.rpart", cp = to_tune(1e-04, 1e-1, logscale = TRUE)) # create auto tuner at = auto_tuner( tuner = tnr("random_search"), learner = learner, resampling = rsmp ("holdout"), measure = msr("classif.ce"), term_evals = 4) resampling_outer = rsmp("cv", folds = 2) rr = resample(tsk("iris"), at, resampling_outer, store_models = TRUE) # extract inner results extract_inner_tuning_results(rr)
A simple mlr3misc::Dictionary storing objects of class Tuner.
Each tuner has an associated help page, see mlr_tuners_[id]
.
This dictionary can get populated with additional tuners by add-on packages.
For a more convenient way to retrieve and construct tuner, see tnr()
/tnrs()
.
R6::R6Class object inheriting from mlr3misc::Dictionary.
See mlr3misc::Dictionary.
as.data.table(dict, ..., objects = FALSE)
mlr3misc::Dictionary -> data.table::data.table()
Returns a data.table::data.table()
with fields "key", "label", "param_classes", "properties" and "packages" as columns.
If objects
is set to TRUE
, the constructed objects are returned in the list column named object
.
Sugar functions: tnr()
, tnrs()
Other Tuner:
Tuner
,
mlr_tuners_cmaes
,
mlr_tuners_design_points
,
mlr_tuners_gensa
,
mlr_tuners_grid_search
,
mlr_tuners_internal
,
mlr_tuners_irace
,
mlr_tuners_nloptr
,
mlr_tuners_random_search
as.data.table(mlr_tuners) mlr_tuners$get("random_search") tnr("random_search")
as.data.table(mlr_tuners) mlr_tuners$get("random_search") tnr("random_search")
Subclass for asynchronous design points tuning.
This Tuner can be instantiated with the associated sugar function tnr()
:
tnr("async_design_points")
design
data.table::data.table
Design points to try in search, one per row.
mlr3tuning::Tuner
-> mlr3tuning::TunerAsync
-> mlr3tuning::TunerAsyncFromOptimizerAsync
-> TunerAsyncDesignPoints
new()
Creates a new instance of this R6 class.
TunerAsyncDesignPoints$new()
clone()
The objects of this class are cloneable with this method.
TunerAsyncDesignPoints$clone(deep = FALSE)
deep
Whether to make a deep clone.
Other TunerAsync:
mlr_tuners_async_grid_search
,
mlr_tuners_async_random_search
Subclass for asynchronous grid search tuning.
This Tuner can be instantiated with the associated sugar function tnr()
:
tnr("async_design_points")
batch_size
integer(1)
Maximum number of points to try in a batch.
mlr3tuning::Tuner
-> mlr3tuning::TunerAsync
-> mlr3tuning::TunerAsyncFromOptimizerAsync
-> TunerAsyncGridSearch
new()
Creates a new instance of this R6 class.
TunerAsyncGridSearch$new()
clone()
The objects of this class are cloneable with this method.
TunerAsyncGridSearch$clone(deep = FALSE)
deep
Whether to make a deep clone.
Other TunerAsync:
mlr_tuners_async_design_points
,
mlr_tuners_async_random_search
Subclass for asynchronous random search tuning.
The random points are sampled by paradox::generate_design_random()
.
This Tuner can be instantiated with the associated sugar function tnr()
:
tnr("async_random_search")
mlr3tuning::Tuner
-> mlr3tuning::TunerAsync
-> mlr3tuning::TunerAsyncFromOptimizerAsync
-> TunerAsyncRandomSearch
new()
Creates a new instance of this R6 class.
TunerAsyncRandomSearch$new()
clone()
The objects of this class are cloneable with this method.
TunerAsyncRandomSearch$clone(deep = FALSE)
deep
Whether to make a deep clone.
Bergstra J, Bengio Y (2012). “Random Search for Hyper-Parameter Optimization.” Journal of Machine Learning Research, 13(10), 281–305. https://jmlr.csail.mit.edu/papers/v13/bergstra12a.html.
Other TunerAsync:
mlr_tuners_async_design_points
,
mlr_tuners_async_grid_search
Subclass for Covariance Matrix Adaptation Evolution Strategy (CMA-ES).
Calls adagio::pureCMAES()
from package adagio.
This Tuner can be instantiated with the associated sugar function tnr()
:
tnr("cmaes")
start_values
character(1)
Create random
start values or based on center
of search space?
In the latter case, it is the center of the parameters before a trafo is applied.
For the meaning of the control parameters, see adagio::pureCMAES()
.
Note that we have removed all control parameters which refer to the termination of the algorithm and where our terminators allow to obtain the same behavior.
$optimize()
supports progress bars via the package progressr
combined with a bbotk::Terminator. Simply wrap the function in
progressr::with_progress()
to enable them. We recommend to use package
progress as backend; enable with progressr::handlers("progress")
.
All Tuners use a logger (as implemented in lgr) from package
bbotk.
Use lgr::get_logger("bbotk")
to access and control the logger.
This Tuner is based on bbotk::OptimizerBatchCmaes which can be applied on any black box optimization problem. See also the documentation of bbotk.
There are several sections about hyperparameter optimization in the mlr3book.
Getting started with hyperparameter optimization.
An overview of all tuners can be found on our website.
Tune a support vector machine on the Sonar data set.
Learn about tuning spaces.
Estimate the model performance with nested resampling.
Learn about multi-objective optimization.
Simultaneously optimize hyperparameters and use early stopping with XGBoost.
Automate the tuning.
The gallery features a collection of case studies and demos about optimization.
Learn more advanced methods with the Practical Tuning Series.
Learn about hotstarting models.
Run the default hyperparameter configuration of learners as a baseline.
Use the Hyperband optimizer with different budget parameters.
The cheatsheet summarizes the most important functions of mlr3tuning.
mlr3tuning::Tuner
-> mlr3tuning::TunerBatch
-> mlr3tuning::TunerBatchFromOptimizerBatch
-> TunerBatchCmaes
new()
Creates a new instance of this R6 class.
TunerBatchCmaes$new()
clone()
The objects of this class are cloneable with this method.
TunerBatchCmaes$clone(deep = FALSE)
deep
Whether to make a deep clone.
Hansen N (2016). “The CMA Evolution Strategy: A Tutorial.” 1604.00772.
Other Tuner:
Tuner
,
mlr_tuners
,
mlr_tuners_design_points
,
mlr_tuners_gensa
,
mlr_tuners_grid_search
,
mlr_tuners_internal
,
mlr_tuners_irace
,
mlr_tuners_nloptr
,
mlr_tuners_random_search
# Hyperparameter Optimization # load learner and set search space learner = lrn("classif.rpart", cp = to_tune(1e-04, 1e-1, logscale = TRUE), minsplit = to_tune(p_dbl(2, 128, trafo = as.integer)), minbucket = to_tune(p_dbl(1, 64, trafo = as.integer)) ) # run hyperparameter tuning on the Palmer Penguins data set instance = tune( tuner = tnr("cmaes"), task = tsk("penguins"), learner = learner, resampling = rsmp("holdout"), measure = msr("classif.ce"), term_evals = 10) # best performing hyperparameter configuration instance$result # all evaluated hyperparameter configuration as.data.table(instance$archive) # fit final model on complete data set learner$param_set$values = instance$result_learner_param_vals learner$train(tsk("penguins"))
# Hyperparameter Optimization # load learner and set search space learner = lrn("classif.rpart", cp = to_tune(1e-04, 1e-1, logscale = TRUE), minsplit = to_tune(p_dbl(2, 128, trafo = as.integer)), minbucket = to_tune(p_dbl(1, 64, trafo = as.integer)) ) # run hyperparameter tuning on the Palmer Penguins data set instance = tune( tuner = tnr("cmaes"), task = tsk("penguins"), learner = learner, resampling = rsmp("holdout"), measure = msr("classif.ce"), term_evals = 10) # best performing hyperparameter configuration instance$result # all evaluated hyperparameter configuration as.data.table(instance$archive) # fit final model on complete data set learner$param_set$values = instance$result_learner_param_vals learner$train(tsk("penguins"))
Subclass for tuning w.r.t. fixed design points.
We simply search over a set of points fully specified by the user. The points in the design are evaluated in order as given.
This Tuner can be instantiated with the associated sugar function tnr()
:
tnr("design_points")
In order to support general termination criteria and parallelization, we
evaluate points in a batch-fashion of size batch_size
. Larger batches mean
we can parallelize more, smaller batches imply a more fine-grained checking
of termination criteria. A batch contains of batch_size
times resampling$iters
jobs.
E.g., if you set a batch size of 10 points and do a 5-fold cross validation, you can
utilize up to 50 cores.
Parallelization is supported via package future (see mlr3::benchmark()
's
section on parallelization for more details).
All Tuners use a logger (as implemented in lgr) from package
bbotk.
Use lgr::get_logger("bbotk")
to access and control the logger.
This Tuner is based on bbotk::OptimizerBatchDesignPoints which can be applied on any black box optimization problem. See also the documentation of bbotk.
batch_size
integer(1)
Maximum number of configurations to try in a batch.
design
data.table::data.table
Design points to try in search, one per row.
There are several sections about hyperparameter optimization in the mlr3book.
Getting started with hyperparameter optimization.
An overview of all tuners can be found on our website.
Tune a support vector machine on the Sonar data set.
Learn about tuning spaces.
Estimate the model performance with nested resampling.
Learn about multi-objective optimization.
Simultaneously optimize hyperparameters and use early stopping with XGBoost.
Automate the tuning.
The gallery features a collection of case studies and demos about optimization.
Learn more advanced methods with the Practical Tuning Series.
Learn about hotstarting models.
Run the default hyperparameter configuration of learners as a baseline.
Use the Hyperband optimizer with different budget parameters.
The cheatsheet summarizes the most important functions of mlr3tuning.
$optimize()
supports progress bars via the package progressr
combined with a Terminator. Simply wrap the function in
progressr::with_progress()
to enable them. We recommend to use package
progress as backend; enable with progressr::handlers("progress")
.
mlr3tuning::Tuner
-> mlr3tuning::TunerBatch
-> mlr3tuning::TunerBatchFromOptimizerBatch
-> TunerBatchDesignPoints
new()
Creates a new instance of this R6 class.
TunerBatchDesignPoints$new()
clone()
The objects of this class are cloneable with this method.
TunerBatchDesignPoints$clone(deep = FALSE)
deep
Whether to make a deep clone.
Package mlr3hyperband for hyperband tuning.
Other Tuner:
Tuner
,
mlr_tuners
,
mlr_tuners_cmaes
,
mlr_tuners_gensa
,
mlr_tuners_grid_search
,
mlr_tuners_internal
,
mlr_tuners_irace
,
mlr_tuners_nloptr
,
mlr_tuners_random_search
# Hyperparameter Optimization # load learner and set search space learner = lrn("classif.rpart", cp = to_tune(1e-04, 1e-1), minsplit = to_tune(2, 128), minbucket = to_tune(1, 64) ) # create design design = mlr3misc::rowwise_table( ~cp, ~minsplit, ~minbucket, 0.1, 2, 64, 0.01, 64, 32, 0.001, 128, 1 ) # run hyperparameter tuning on the Palmer Penguins data set instance = tune( tuner = tnr("design_points", design = design), task = tsk("penguins"), learner = learner, resampling = rsmp("holdout"), measure = msr("classif.ce") ) # best performing hyperparameter configuration instance$result # all evaluated hyperparameter configuration as.data.table(instance$archive) # fit final model on complete data set learner$param_set$values = instance$result_learner_param_vals learner$train(tsk("penguins"))
# Hyperparameter Optimization # load learner and set search space learner = lrn("classif.rpart", cp = to_tune(1e-04, 1e-1), minsplit = to_tune(2, 128), minbucket = to_tune(1, 64) ) # create design design = mlr3misc::rowwise_table( ~cp, ~minsplit, ~minbucket, 0.1, 2, 64, 0.01, 64, 32, 0.001, 128, 1 ) # run hyperparameter tuning on the Palmer Penguins data set instance = tune( tuner = tnr("design_points", design = design), task = tsk("penguins"), learner = learner, resampling = rsmp("holdout"), measure = msr("classif.ce") ) # best performing hyperparameter configuration instance$result # all evaluated hyperparameter configuration as.data.table(instance$archive) # fit final model on complete data set learner$param_set$values = instance$result_learner_param_vals learner$train(tsk("penguins"))
Subclass for generalized simulated annealing tuning.
Calls GenSA::GenSA()
from package GenSA.
In contrast to the GenSA::GenSA()
defaults, we set smooth = FALSE
as a default.
This Tuner can be instantiated with the associated sugar function tnr()
:
tnr("gensa")
In order to support general termination criteria and parallelization, we
evaluate points in a batch-fashion of size batch_size
. Larger batches mean
we can parallelize more, smaller batches imply a more fine-grained checking
of termination criteria. A batch contains of batch_size
times resampling$iters
jobs.
E.g., if you set a batch size of 10 points and do a 5-fold cross validation, you can
utilize up to 50 cores.
Parallelization is supported via package future (see mlr3::benchmark()
's
section on parallelization for more details).
All Tuners use a logger (as implemented in lgr) from package
bbotk.
Use lgr::get_logger("bbotk")
to access and control the logger.
This Tuner is based on bbotk::OptimizerBatchGenSA which can be applied on any black box optimization problem. See also the documentation of bbotk.
smooth
logical(1)
temperature
numeric(1)
acceptance.param
numeric(1)
verbose
logical(1)
trace.mat
logical(1)
For the meaning of the control parameters, see GenSA::GenSA()
. Note that we
have removed all control parameters which refer to the termination of the
algorithm and where our terminators allow to obtain the same behavior.
In contrast to the GenSA::GenSA()
defaults, we set trace.mat = FALSE
.
Note that GenSA::GenSA()
uses smooth = TRUE
as a default.
In the case of using this optimizer for Hyperparameter Optimization you may
want to set smooth = FALSE
.
There are several sections about hyperparameter optimization in the mlr3book.
Getting started with hyperparameter optimization.
An overview of all tuners can be found on our website.
Tune a support vector machine on the Sonar data set.
Learn about tuning spaces.
Estimate the model performance with nested resampling.
Learn about multi-objective optimization.
Simultaneously optimize hyperparameters and use early stopping with XGBoost.
Automate the tuning.
The gallery features a collection of case studies and demos about optimization.
Learn more advanced methods with the Practical Tuning Series.
Learn about hotstarting models.
Run the default hyperparameter configuration of learners as a baseline.
Use the Hyperband optimizer with different budget parameters.
The cheatsheet summarizes the most important functions of mlr3tuning.
$optimize()
supports progress bars via the package progressr
combined with a Terminator. Simply wrap the function in
progressr::with_progress()
to enable them. We recommend to use package
progress as backend; enable with progressr::handlers("progress")
.
mlr3tuning::Tuner
-> mlr3tuning::TunerBatch
-> mlr3tuning::TunerBatchFromOptimizerBatch
-> TunerBatchGenSA
new()
Creates a new instance of this R6 class.
TunerBatchGenSA$new()
clone()
The objects of this class are cloneable with this method.
TunerBatchGenSA$clone(deep = FALSE)
deep
Whether to make a deep clone.
Tsallis C, Stariolo DA (1996). “Generalized simulated annealing.” Physica A: Statistical Mechanics and its Applications, 233(1-2), 395–406. doi:10.1016/s0378-4371(96)00271-3.
Xiang Y, Gubian S, Suomela B, Hoeng J (2013). “Generalized Simulated Annealing for Global Optimization: The GenSA Package.” The R Journal, 5(1), 13. doi:10.32614/rj-2013-002.
Other Tuner:
Tuner
,
mlr_tuners
,
mlr_tuners_cmaes
,
mlr_tuners_design_points
,
mlr_tuners_grid_search
,
mlr_tuners_internal
,
mlr_tuners_irace
,
mlr_tuners_nloptr
,
mlr_tuners_random_search
# Hyperparameter Optimization # load learner and set search space learner = lrn("classif.rpart", cp = to_tune(1e-04, 1e-1, logscale = TRUE) ) # run hyperparameter tuning on the Palmer Penguins data set instance = tune( tuner = tnr("gensa"), task = tsk("penguins"), learner = learner, resampling = rsmp("holdout"), measure = msr("classif.ce"), term_evals = 10 ) # best performing hyperparameter configuration instance$result # all evaluated hyperparameter configuration as.data.table(instance$archive) # fit final model on complete data set learner$param_set$values = instance$result_learner_param_vals learner$train(tsk("penguins"))
# Hyperparameter Optimization # load learner and set search space learner = lrn("classif.rpart", cp = to_tune(1e-04, 1e-1, logscale = TRUE) ) # run hyperparameter tuning on the Palmer Penguins data set instance = tune( tuner = tnr("gensa"), task = tsk("penguins"), learner = learner, resampling = rsmp("holdout"), measure = msr("classif.ce"), term_evals = 10 ) # best performing hyperparameter configuration instance$result # all evaluated hyperparameter configuration as.data.table(instance$archive) # fit final model on complete data set learner$param_set$values = instance$result_learner_param_vals learner$train(tsk("penguins"))
Subclass for grid search tuning.
The grid is constructed as a Cartesian product over discretized values per parameter, see paradox::generate_design_grid()
.
If the learner supports hotstarting, the grid is sorted by the hotstart parameter (see also mlr3::HotstartStack).
If not, the points of the grid are evaluated in a random order.
This Tuner can be instantiated with the associated sugar function tnr()
:
tnr("grid_search")
resolution
integer(1)
Resolution of the grid, see paradox::generate_design_grid()
.
param_resolutions
named integer()
Resolution per parameter, named by parameter ID, see paradox::generate_design_grid()
.
batch_size
integer(1)
Maximum number of points to try in a batch.
$optimize()
supports progress bars via the package progressr
combined with a bbotk::Terminator. Simply wrap the function in
progressr::with_progress()
to enable them. We recommend to use package
progress as backend; enable with progressr::handlers("progress")
.
In order to support general termination criteria and parallelization, we
evaluate points in a batch-fashion of size batch_size
. Larger batches mean
we can parallelize more, smaller batches imply a more fine-grained checking
of termination criteria. A batch contains of batch_size
times resampling$iters
jobs.
E.g., if you set a batch size of 10 points and do a 5-fold cross validation, you can
utilize up to 50 cores.
Parallelization is supported via package future (see mlr3::benchmark()
's
section on parallelization for more details).
All Tuners use a logger (as implemented in lgr) from package
bbotk.
Use lgr::get_logger("bbotk")
to access and control the logger.
This Tuner is based on bbotk::OptimizerBatchGridSearch which can be applied on any black box optimization problem. See also the documentation of bbotk.
There are several sections about hyperparameter optimization in the mlr3book.
Getting started with hyperparameter optimization.
An overview of all tuners can be found on our website.
Tune a support vector machine on the Sonar data set.
Learn about tuning spaces.
Estimate the model performance with nested resampling.
Learn about multi-objective optimization.
Simultaneously optimize hyperparameters and use early stopping with XGBoost.
Automate the tuning.
The gallery features a collection of case studies and demos about optimization.
Learn more advanced methods with the Practical Tuning Series.
Learn about hotstarting models.
Run the default hyperparameter configuration of learners as a baseline.
Use the Hyperband optimizer with different budget parameters.
The cheatsheet summarizes the most important functions of mlr3tuning.
mlr3tuning::Tuner
-> mlr3tuning::TunerBatch
-> mlr3tuning::TunerBatchFromOptimizerBatch
-> TunerBatchGridSearch
new()
Creates a new instance of this R6 class.
TunerBatchGridSearch$new()
clone()
The objects of this class are cloneable with this method.
TunerBatchGridSearch$clone(deep = FALSE)
deep
Whether to make a deep clone.
Other Tuner:
Tuner
,
mlr_tuners
,
mlr_tuners_cmaes
,
mlr_tuners_design_points
,
mlr_tuners_gensa
,
mlr_tuners_internal
,
mlr_tuners_irace
,
mlr_tuners_nloptr
,
mlr_tuners_random_search
# Hyperparameter Optimization # load learner and set search space learner = lrn("classif.rpart", cp = to_tune(1e-04, 1e-1, logscale = TRUE) ) # run hyperparameter tuning on the Palmer Penguins data set instance = tune( tuner = tnr("grid_search"), task = tsk("penguins"), learner = learner, resampling = rsmp("holdout"), measure = msr("classif.ce"), term_evals = 10 ) # best performing hyperparameter configuration instance$result # all evaluated hyperparameter configuration as.data.table(instance$archive) # fit final model on complete data set learner$param_set$values = instance$result_learner_param_vals learner$train(tsk("penguins"))
# Hyperparameter Optimization # load learner and set search space learner = lrn("classif.rpart", cp = to_tune(1e-04, 1e-1, logscale = TRUE) ) # run hyperparameter tuning on the Palmer Penguins data set instance = tune( tuner = tnr("grid_search"), task = tsk("penguins"), learner = learner, resampling = rsmp("holdout"), measure = msr("classif.ce"), term_evals = 10 ) # best performing hyperparameter configuration instance$result # all evaluated hyperparameter configuration as.data.table(instance$archive) # fit final model on complete data set learner$param_set$values = instance$result_learner_param_vals learner$train(tsk("penguins"))
Subclass to conduct only internal hyperparameter tuning for a mlr3::Learner.
This Tuner can be instantiated with the associated sugar function tnr()
:
tnr("internal")
$optimize()
supports progress bars via the package progressr
combined with a bbotk::Terminator. Simply wrap the function in
progressr::with_progress()
to enable them. We recommend to use package
progress as backend; enable with progressr::handlers("progress")
.
All Tuners use a logger (as implemented in lgr) from package
bbotk.
Use lgr::get_logger("bbotk")
to access and control the logger.
There are several sections about hyperparameter optimization in the mlr3book.
Getting started with hyperparameter optimization.
An overview of all tuners can be found on our website.
Tune a support vector machine on the Sonar data set.
Learn about tuning spaces.
Estimate the model performance with nested resampling.
Learn about multi-objective optimization.
Simultaneously optimize hyperparameters and use early stopping with XGBoost.
Automate the tuning.
The gallery features a collection of case studies and demos about optimization.
Learn more advanced methods with the Practical Tuning Series.
Learn about hotstarting models.
Run the default hyperparameter configuration of learners as a baseline.
Use the Hyperband optimizer with different budget parameters.
The cheatsheet summarizes the most important functions of mlr3tuning.
mlr3tuning::Tuner
-> mlr3tuning::TunerBatch
-> TunerBatchInternal
new()
Creates a new instance of this R6 class.
TunerBatchInternal$new()
clone()
The objects of this class are cloneable with this method.
TunerBatchInternal$clone(deep = FALSE)
deep
Whether to make a deep clone.
The selected mlr3::Measure does not influence the tuning result. To change the loss-function for the internal tuning, consult the hyperparameter documentation of the tuned mlr3::Learner.
Other Tuner:
Tuner
,
mlr_tuners
,
mlr_tuners_cmaes
,
mlr_tuners_design_points
,
mlr_tuners_gensa
,
mlr_tuners_grid_search
,
mlr_tuners_irace
,
mlr_tuners_nloptr
,
mlr_tuners_random_search
library(mlr3learners) # Retrieve task task = tsk("pima") # Load learner and set search space learner = lrn("classif.xgboost", nrounds = to_tune(upper = 1000, internal = TRUE), early_stopping_rounds = 10, validate = "test", eval_metric = "merror" ) # Internal hyperparameter tuning on the pima indians diabetes data set instance = tune( tnr("internal"), tsk("iris"), learner, rsmp("cv", folds = 3), msr("internal_valid_score", minimize = TRUE, select = "merror") ) # best performing hyperparameter configuration instance$result_learner_param_vals instance$result_learner_param_vals$internal_tuned_values
library(mlr3learners) # Retrieve task task = tsk("pima") # Load learner and set search space learner = lrn("classif.xgboost", nrounds = to_tune(upper = 1000, internal = TRUE), early_stopping_rounds = 10, validate = "test", eval_metric = "merror" ) # Internal hyperparameter tuning on the pima indians diabetes data set instance = tune( tnr("internal"), tsk("iris"), learner, rsmp("cv", folds = 3), msr("internal_valid_score", minimize = TRUE, select = "merror") ) # best performing hyperparameter configuration instance$result_learner_param_vals instance$result_learner_param_vals$internal_tuned_values
Subclass for iterated racing.
Calls irace::irace()
from package irace.
This Tuner can be instantiated with the associated sugar function tnr()
:
tnr("irace")
n_instances
integer(1)
Number of resampling instances.
For the meaning of all other parameters, see irace::defaultScenario()
. Note
that we have removed all control parameters which refer to the termination of
the algorithm. Use bbotk::TerminatorEvals instead. Other terminators do not work
with TunerIrace
.
The ArchiveBatchTuning holds the following additional columns:
"race"
(integer(1)
)
Race iteration.
"step"
(integer(1)
)
Step number of race.
"instance"
(integer(1)
)
Identifies resampling instances across races and steps.
"configuration"
(integer(1)
)
Identifies configurations across races and steps.
The tuning result (instance$result
) is the best-performing elite of the final race.
The reported performance is the average performance estimated on all used instances.
$optimize()
supports progress bars via the package progressr
combined with a bbotk::Terminator. Simply wrap the function in
progressr::with_progress()
to enable them. We recommend to use package
progress as backend; enable with progressr::handlers("progress")
.
All Tuners use a logger (as implemented in lgr) from package
bbotk.
Use lgr::get_logger("bbotk")
to access and control the logger.
This Tuner is based on bbotk::OptimizerBatchIrace which can be applied on any black box optimization problem. See also the documentation of bbotk.
There are several sections about hyperparameter optimization in the mlr3book.
Getting started with hyperparameter optimization.
An overview of all tuners can be found on our website.
Tune a support vector machine on the Sonar data set.
Learn about tuning spaces.
Estimate the model performance with nested resampling.
Learn about multi-objective optimization.
Simultaneously optimize hyperparameters and use early stopping with XGBoost.
Automate the tuning.
The gallery features a collection of case studies and demos about optimization.
Learn more advanced methods with the Practical Tuning Series.
Learn about hotstarting models.
Run the default hyperparameter configuration of learners as a baseline.
Use the Hyperband optimizer with different budget parameters.
The cheatsheet summarizes the most important functions of mlr3tuning.
mlr3tuning::Tuner
-> mlr3tuning::TunerBatch
-> mlr3tuning::TunerBatchFromOptimizerBatch
-> TunerBatchIrace
new()
Creates a new instance of this R6 class.
TunerBatchIrace$new()
optimize()
Performs the tuning on a TuningInstanceBatchSingleCrit until termination. The single evaluations and the final results will be written into the ArchiveBatchTuning that resides in the TuningInstanceBatchSingleCrit. The final result is returned.
TunerBatchIrace$optimize(inst)
inst
clone()
The objects of this class are cloneable with this method.
TunerBatchIrace$clone(deep = FALSE)
deep
Whether to make a deep clone.
Lopez-Ibanez M, Dubois-Lacoste J, Caceres LP, Birattari M, Stuetzle T (2016). “The irace package: Iterated racing for automatic algorithm configuration.” Operations Research Perspectives, 3, 43–58. doi:10.1016/j.orp.2016.09.002.
Other Tuner:
Tuner
,
mlr_tuners
,
mlr_tuners_cmaes
,
mlr_tuners_design_points
,
mlr_tuners_gensa
,
mlr_tuners_grid_search
,
mlr_tuners_internal
,
mlr_tuners_nloptr
,
mlr_tuners_random_search
# retrieve task task = tsk("pima") # load learner and set search space learner = lrn("classif.rpart", cp = to_tune(1e-04, 1e-1, logscale = TRUE)) # hyperparameter tuning on the pima indians diabetes data set instance = tune( tuner = tnr("irace"), task = task, learner = learner, resampling = rsmp("holdout"), measure = msr("classif.ce"), term_evals = 42 ) # best performing hyperparameter configuration instance$result # all evaluated hyperparameter configuration as.data.table(instance$archive) # fit final model on complete data set learner$param_set$values = instance$result_learner_param_vals learner$train(task)
# retrieve task task = tsk("pima") # load learner and set search space learner = lrn("classif.rpart", cp = to_tune(1e-04, 1e-1, logscale = TRUE)) # hyperparameter tuning on the pima indians diabetes data set instance = tune( tuner = tnr("irace"), task = task, learner = learner, resampling = rsmp("holdout"), measure = msr("classif.ce"), term_evals = 42 ) # best performing hyperparameter configuration instance$result # all evaluated hyperparameter configuration as.data.table(instance$archive) # fit final model on complete data set learner$param_set$values = instance$result_learner_param_vals learner$train(task)
Subclass for non-linear optimization (NLopt). Calls nloptr::nloptr from package nloptr.
The termination conditions stopval
, maxtime
and maxeval
of nloptr::nloptr()
are deactivated and replaced by the bbotk::Terminator subclasses.
The x and function value tolerance termination conditions (xtol_rel = 10^-4
, xtol_abs = rep(0.0, length(x0))
, ftol_rel = 0.0
and ftol_abs = 0.0
) are still available and implemented with their package defaults.
To deactivate these conditions, set them to -1
.
This Tuner can be instantiated with the associated sugar function tnr()
:
tnr("nloptr")
All Tuners use a logger (as implemented in lgr) from package
bbotk.
Use lgr::get_logger("bbotk")
to access and control the logger.
This Tuner is based on bbotk::OptimizerBatchNLoptr which can be applied on any black box optimization problem. See also the documentation of bbotk.
algorithm
character(1)
eval_g_ineq
function()
xtol_rel
numeric(1)
xtol_abs
numeric(1)
ftol_rel
numeric(1)
ftol_abs
numeric(1)
start_values
character(1)
Create random
start values or based on center
of search space? In the
latter case, it is the center of the parameters before a trafo is applied.
For the meaning of the control parameters, see nloptr::nloptr()
and
nloptr::nloptr.print.options()
.
The termination conditions stopval
, maxtime
and maxeval
of
nloptr::nloptr()
are deactivated and replaced by the Terminator
subclasses. The x and function value tolerance termination conditions
(xtol_rel = 10^-4
, xtol_abs = rep(0.0, length(x0))
, ftol_rel = 0.0
and
ftol_abs = 0.0
) are still available and implemented with their package
defaults. To deactivate these conditions, set them to -1
.
There are several sections about hyperparameter optimization in the mlr3book.
Getting started with hyperparameter optimization.
An overview of all tuners can be found on our website.
Tune a support vector machine on the Sonar data set.
Learn about tuning spaces.
Estimate the model performance with nested resampling.
Learn about multi-objective optimization.
Simultaneously optimize hyperparameters and use early stopping with XGBoost.
Automate the tuning.
The gallery features a collection of case studies and demos about optimization.
Learn more advanced methods with the Practical Tuning Series.
Learn about hotstarting models.
Run the default hyperparameter configuration of learners as a baseline.
Use the Hyperband optimizer with different budget parameters.
The cheatsheet summarizes the most important functions of mlr3tuning.
$optimize()
supports progress bars via the package progressr
combined with a Terminator. Simply wrap the function in
progressr::with_progress()
to enable them. We recommend to use package
progress as backend; enable with progressr::handlers("progress")
.
mlr3tuning::Tuner
-> mlr3tuning::TunerBatch
-> mlr3tuning::TunerBatchFromOptimizerBatch
-> TunerBatchNLoptr
new()
Creates a new instance of this R6 class.
TunerBatchNLoptr$new()
clone()
The objects of this class are cloneable with this method.
TunerBatchNLoptr$clone(deep = FALSE)
deep
Whether to make a deep clone.
Johnson, G S (2020). “The NLopt nonlinear-optimization package.” https://github.com/stevengj/nlopt.
Other Tuner:
Tuner
,
mlr_tuners
,
mlr_tuners_cmaes
,
mlr_tuners_design_points
,
mlr_tuners_gensa
,
mlr_tuners_grid_search
,
mlr_tuners_internal
,
mlr_tuners_irace
,
mlr_tuners_random_search
# Hyperparameter Optimization # load learner and set search space learner = lrn("classif.rpart", cp = to_tune(1e-04, 1e-1, logscale = TRUE) ) # run hyperparameter tuning on the Palmer Penguins data set instance = tune( tuner = tnr("nloptr", algorithm = "NLOPT_LN_BOBYQA"), task = tsk("penguins"), learner = learner, resampling = rsmp("holdout"), measure = msr("classif.ce") ) # best performing hyperparameter configuration instance$result # all evaluated hyperparameter configuration as.data.table(instance$archive) # fit final model on complete data set learner$param_set$values = instance$result_learner_param_vals learner$train(tsk("penguins"))
# Hyperparameter Optimization # load learner and set search space learner = lrn("classif.rpart", cp = to_tune(1e-04, 1e-1, logscale = TRUE) ) # run hyperparameter tuning on the Palmer Penguins data set instance = tune( tuner = tnr("nloptr", algorithm = "NLOPT_LN_BOBYQA"), task = tsk("penguins"), learner = learner, resampling = rsmp("holdout"), measure = msr("classif.ce") ) # best performing hyperparameter configuration instance$result # all evaluated hyperparameter configuration as.data.table(instance$archive) # fit final model on complete data set learner$param_set$values = instance$result_learner_param_vals learner$train(tsk("penguins"))
Subclass for random search tuning.
The random points are sampled by paradox::generate_design_random()
.
This Tuner can be instantiated with the associated sugar function tnr()
:
tnr("random_search")
In order to support general termination criteria and parallelization, we
evaluate points in a batch-fashion of size batch_size
. Larger batches mean
we can parallelize more, smaller batches imply a more fine-grained checking
of termination criteria. A batch contains of batch_size
times resampling$iters
jobs.
E.g., if you set a batch size of 10 points and do a 5-fold cross validation, you can
utilize up to 50 cores.
Parallelization is supported via package future (see mlr3::benchmark()
's
section on parallelization for more details).
All Tuners use a logger (as implemented in lgr) from package
bbotk.
Use lgr::get_logger("bbotk")
to access and control the logger.
This Tuner is based on bbotk::OptimizerBatchRandomSearch which can be applied on any black box optimization problem. See also the documentation of bbotk.
batch_size
integer(1)
Maximum number of points to try in a batch.
There are several sections about hyperparameter optimization in the mlr3book.
Getting started with hyperparameter optimization.
An overview of all tuners can be found on our website.
Tune a support vector machine on the Sonar data set.
Learn about tuning spaces.
Estimate the model performance with nested resampling.
Learn about multi-objective optimization.
Simultaneously optimize hyperparameters and use early stopping with XGBoost.
Automate the tuning.
The gallery features a collection of case studies and demos about optimization.
Learn more advanced methods with the Practical Tuning Series.
Learn about hotstarting models.
Run the default hyperparameter configuration of learners as a baseline.
Use the Hyperband optimizer with different budget parameters.
The cheatsheet summarizes the most important functions of mlr3tuning.
$optimize()
supports progress bars via the package progressr
combined with a Terminator. Simply wrap the function in
progressr::with_progress()
to enable them. We recommend to use package
progress as backend; enable with progressr::handlers("progress")
.
mlr3tuning::Tuner
-> mlr3tuning::TunerBatch
-> mlr3tuning::TunerBatchFromOptimizerBatch
-> TunerBatchRandomSearch
new()
Creates a new instance of this R6 class.
TunerBatchRandomSearch$new()
clone()
The objects of this class are cloneable with this method.
TunerBatchRandomSearch$clone(deep = FALSE)
deep
Whether to make a deep clone.
Bergstra J, Bengio Y (2012). “Random Search for Hyper-Parameter Optimization.” Journal of Machine Learning Research, 13(10), 281–305. https://jmlr.csail.mit.edu/papers/v13/bergstra12a.html.
Package mlr3hyperband for hyperband tuning.
Other Tuner:
Tuner
,
mlr_tuners
,
mlr_tuners_cmaes
,
mlr_tuners_design_points
,
mlr_tuners_gensa
,
mlr_tuners_grid_search
,
mlr_tuners_internal
,
mlr_tuners_irace
,
mlr_tuners_nloptr
# Hyperparameter Optimization # load learner and set search space learner = lrn("classif.rpart", cp = to_tune(1e-04, 1e-1, logscale = TRUE) ) # run hyperparameter tuning on the Palmer Penguins data set instance = tune( tuner = tnr("random_search"), task = tsk("penguins"), learner = learner, resampling = rsmp("holdout"), measure = msr("classif.ce"), term_evals = 10 ) # best performing hyperparameter configuration instance$result # all evaluated hyperparameter configuration as.data.table(instance$archive) # fit final model on complete data set learner$param_set$values = instance$result_learner_param_vals learner$train(tsk("penguins"))
# Hyperparameter Optimization # load learner and set search space learner = lrn("classif.rpart", cp = to_tune(1e-04, 1e-1, logscale = TRUE) ) # run hyperparameter tuning on the Palmer Penguins data set instance = tune( tuner = tnr("random_search"), task = tsk("penguins"), learner = learner, resampling = rsmp("holdout"), measure = msr("classif.ce"), term_evals = 10 ) # best performing hyperparameter configuration instance$result # all evaluated hyperparameter configuration as.data.table(instance$archive) # fit final model on complete data set learner$param_set$values = instance$result_learner_param_vals learner$train(tsk("penguins"))
This mlr3misc::Callback logs the hyperparameter configurations and the performance of the configurations to MLflow.
clbk("mlr3tuning.async_mlflow", tracking_uri = "http://localhost:5000") ## Not run: rush::rush_plan(n_workers = 4) learner = lrn("classif.rpart", minsplit = to_tune(2, 128), cp = to_tune(1e-04, 1e-1)) instance = TuningInstanceAsyncSingleCrit$new( task = tsk("pima"), learner = learner, resampling = rsmp("cv", folds = 3), measure = msr("classif.ce"), terminator = trm("evals", n_evals = 20), store_benchmark_result = FALSE, callbacks = clbk("mlr3tuning.rush_mlflow", tracking_uri = "http://localhost:8080") ) tuner = tnr("random_search_v2") tuner$optimize(instance) ## End(Not run)
clbk("mlr3tuning.async_mlflow", tracking_uri = "http://localhost:5000") ## Not run: rush::rush_plan(n_workers = 4) learner = lrn("classif.rpart", minsplit = to_tune(2, 128), cp = to_tune(1e-04, 1e-1)) instance = TuningInstanceAsyncSingleCrit$new( task = tsk("pima"), learner = learner, resampling = rsmp("cv", folds = 3), measure = msr("classif.ce"), terminator = trm("evals", n_evals = 20), store_benchmark_result = FALSE, callbacks = clbk("mlr3tuning.rush_mlflow", tracking_uri = "http://localhost:8080") ) tuner = tnr("random_search_v2") tuner$optimize(instance) ## End(Not run)
These CallbackAsyncTuning and CallbackBatchTuning evaluate the default hyperparameter values of a learner.
This CallbackAsyncTuning saves the logs of the learners to the archive.
This mlr3misc::Callback writes the mlr3::BenchmarkResult after each batch to disk.
clbk("mlr3tuning.backup", path = "backup.rds") # tune classification tree on the pima data set instance = tune( tuner = tnr("random_search", batch_size = 2), task = tsk("pima"), learner = lrn("classif.rpart", cp = to_tune(1e-04, 1e-1, logscale = TRUE)), resampling = rsmp("cv", folds = 3), measures = msr("classif.ce"), term_evals = 4, callbacks = clbk("mlr3tuning.backup", path = tempfile(fileext = ".rds")) )
clbk("mlr3tuning.backup", path = "backup.rds") # tune classification tree on the pima data set instance = tune( tuner = tnr("random_search", batch_size = 2), task = tsk("pima"), learner = lrn("classif.rpart", cp = to_tune(1e-04, 1e-1, logscale = TRUE)), resampling = rsmp("cv", folds = 3), measures = msr("classif.ce"), term_evals = 4, callbacks = clbk("mlr3tuning.backup", path = tempfile(fileext = ".rds")) )
This mlr3misc::Callback scores the hyperparameter configurations on additional measures while tuning. Usually, the configurations can be scored on additional measures after tuning (see ArchiveBatchTuning). However, if the memory is not sufficient to store the mlr3::BenchmarkResult, it is necessary to score the additional measures while tuning. The measures are not taken into account by the tuner.
clbk("mlr3tuning.measures") # additionally score the configurations on the accuracy measure instance = tune( tuner = tnr("random_search", batch_size = 2), task = tsk("pima"), learner = lrn("classif.rpart", cp = to_tune(1e-04, 1e-1, logscale = TRUE)), resampling = rsmp("cv", folds = 3), measures = msr("classif.ce"), term_evals = 4, callbacks = clbk("mlr3tuning.measures", measures = msr("classif.acc")) )
clbk("mlr3tuning.measures") # additionally score the configurations on the accuracy measure instance = tune( tuner = tnr("random_search", batch_size = 2), task = tsk("pima"), learner = lrn("classif.rpart", cp = to_tune(1e-04, 1e-1, logscale = TRUE)), resampling = rsmp("cv", folds = 3), measures = msr("classif.ce"), term_evals = 4, callbacks = clbk("mlr3tuning.measures", measures = msr("classif.acc")) )
The one standard error rule takes the number of features into account when selecting the best hyperparameter configuration.
Many learners support internal feature selection, which can be accessed via $selected_features()
.
The callback selects the hyperparameter configuration with the smallest feature set within one standard error of the best performing configuration.
If there are multiple such hyperparameter configurations with the same number of features, the first one is selected.
Kuhn, Max, Johnson, Kjell (2013). “Applied Predictive Modeling.” In chapter Over-Fitting and Model Tuning, 61–92. Springer New York, New York, NY. ISBN 978-1-4614-6849-3.
clbk("mlr3tuning.one_se_rule") # Run optimization on the pima data set with the callback instance = tune( tuner = tnr("random_search", batch_size = 15), task = tsk("pima"), learner = lrn("classif.rpart", cp = to_tune(1e-04, 1e-1, logscale = TRUE)), resampling = rsmp("cv", folds = 3), measures = msr("classif.ce"), term_evals = 30, callbacks = clbk("mlr3tuning.one_se_rule") ) # Hyperparameter configuration with the smallest feature set within one standard error of the best instance$result
clbk("mlr3tuning.one_se_rule") # Run optimization on the pima data set with the callback instance = tune( tuner = tnr("random_search", batch_size = 15), task = tsk("pima"), learner = lrn("classif.rpart", cp = to_tune(1e-04, 1e-1, logscale = TRUE)), resampling = rsmp("cv", folds = 3), measures = msr("classif.ce"), term_evals = 30, callbacks = clbk("mlr3tuning.one_se_rule") ) # Hyperparameter configuration with the smallest feature set within one standard error of the best instance$result
Stores the objective function that estimates the performance of hyperparameter configurations. This class is usually constructed internally by the TuningInstanceBatchSingleCrit or TuningInstanceBatchMultiCrit.
bbotk::Objective
-> ObjectiveTuning
task
(mlr3::Task).
learner
resampling
measures
(list of mlr3::Measure).
store_models
(logical(1)
).
store_benchmark_result
(logical(1)
).
callbacks
(List of mlr3misc::Callback).
default_values
(named list()
).
internal_search_space
(paradox::ParamSet). Internal search space for internal tuning.
new()
Creates a new instance of this R6 class.
ObjectiveTuning$new( task, learner, resampling, measures, store_benchmark_result = TRUE, store_models = FALSE, check_values = FALSE, callbacks = NULL, internal_search_space = NULL )
task
(mlr3::Task)
Task to operate on.
learner
(mlr3::Learner)
Learner to tune.
resampling
(mlr3::Resampling)
Resampling that is used to evaluate the performance of the hyperparameter configurations.
Uninstantiated resamplings are instantiated during construction so that all configurations are evaluated on the same data splits.
Already instantiated resamplings are kept unchanged.
Specialized Tuner change the resampling e.g. to evaluate a hyperparameter configuration on different data splits.
This field, however, always returns the resampling passed in construction.
measures
(list of mlr3::Measure)
Measures to optimize.
store_benchmark_result
(logical(1)
)
If TRUE
(default), store resample result of evaluated hyperparameter
configurations in archive as mlr3::BenchmarkResult.
store_models
(logical(1)
)
If TRUE
, fitted models are stored in the benchmark result
(archive$benchmark_result
). If store_benchmark_result = FALSE
, models
are only stored temporarily and not accessible after the tuning. This
combination is needed for measures that require a model.
check_values
(logical(1)
)
If TRUE
, hyperparameter values are checked before evaluation and
performance scores after. If FALSE
(default), values are unchecked but
computational overhead is reduced.
callbacks
(list of mlr3misc::Callback)
List of callbacks.
internal_search_space
(paradox::ParamSet or NULL
)
The internal search space.
clone()
The objects of this class are cloneable with this method.
ObjectiveTuning$clone(deep = FALSE)
deep
Whether to make a deep clone.
Stores the objective function that estimates the performance of hyperparameter configurations. This class is usually constructed internally by the TuningInstanceBatchSingleCrit or TuningInstanceBatchMultiCrit.
bbotk::Objective
-> mlr3tuning::ObjectiveTuning
-> ObjectiveTuningAsync
clone()
The objects of this class are cloneable with this method.
ObjectiveTuningAsync$clone(deep = FALSE)
deep
Whether to make a deep clone.
Stores the objective function that estimates the performance of hyperparameter configurations. This class is usually constructed internally by the TuningInstanceBatchSingleCrit or TuningInstanceBatchMultiCrit.
bbotk::Objective
-> mlr3tuning::ObjectiveTuning
-> ObjectiveTuningBatch
archive
new()
Creates a new instance of this R6 class.
ObjectiveTuningBatch$new( task, learner, resampling, measures, store_benchmark_result = TRUE, store_models = FALSE, check_values = FALSE, archive = NULL, callbacks = NULL, internal_search_space = NULL )
task
(mlr3::Task)
Task to operate on.
learner
(mlr3::Learner)
Learner to tune.
resampling
(mlr3::Resampling)
Resampling that is used to evaluate the performance of the hyperparameter configurations.
Uninstantiated resamplings are instantiated during construction so that all configurations are evaluated on the same data splits.
Already instantiated resamplings are kept unchanged.
Specialized Tuner change the resampling e.g. to evaluate a hyperparameter configuration on different data splits.
This field, however, always returns the resampling passed in construction.
measures
(list of mlr3::Measure)
Measures to optimize.
store_benchmark_result
(logical(1)
)
If TRUE
(default), store resample result of evaluated hyperparameter
configurations in archive as mlr3::BenchmarkResult.
store_models
(logical(1)
)
If TRUE
, fitted models are stored in the benchmark result
(archive$benchmark_result
). If store_benchmark_result = FALSE
, models
are only stored temporarily and not accessible after the tuning. This
combination is needed for measures that require a model.
check_values
(logical(1)
)
If TRUE
, hyperparameter values are checked before evaluation and
performance scores after. If FALSE
(default), values are unchecked but
computational overhead is reduced.
archive
(ArchiveBatchTuning)
Reference to archive of TuningInstanceBatchSingleCrit | TuningInstanceBatchMultiCrit.
If NULL
(default), benchmark result and models cannot be stored.
callbacks
(list of mlr3misc::Callback)
List of callbacks.
internal_search_space
(paradox::ParamSet or NULL
)
The internal search space.
clone()
The objects of this class are cloneable with this method.
ObjectiveTuningBatch$clone(deep = FALSE)
deep
Whether to make a deep clone.
Configure validation data for the learner that is tuned by the AutoTuner
.
## S3 method for class 'AutoTuner' set_validate(learner, validate, ...)
## S3 method for class 'AutoTuner' set_validate(learner, validate, ...)
learner |
( |
validate |
( |
... |
(any) |
at = auto_tuner( tuner = tnr("random_search"), learner = lrn("classif.debug", early_stopping = TRUE, iter = to_tune(upper = 1000L, internal = TRUE), validate = 0.2), resampling = rsmp("holdout") ) # use the test set as validation data during tuning set_validate(at, validate = "test") at$learner$validate
at = auto_tuner( tuner = tnr("random_search"), learner = lrn("classif.debug", early_stopping = TRUE, iter = to_tune(upper = 1000L, internal = TRUE), validate = 0.2), resampling = rsmp("holdout") ) # use the test set as validation data during tuning set_validate(at, validate = "test") at$learner$validate
Function to construct a TuningInstanceBatchSingleCrit or TuningInstanceBatchMultiCrit.
ti( task, learner, resampling, measures = NULL, terminator, search_space = NULL, internal_search_space = NULL, store_benchmark_result = TRUE, store_models = FALSE, check_values = FALSE, callbacks = NULL )
ti( task, learner, resampling, measures = NULL, terminator, search_space = NULL, internal_search_space = NULL, store_benchmark_result = TRUE, store_models = FALSE, check_values = FALSE, callbacks = NULL )
task |
(mlr3::Task) |
learner |
(mlr3::Learner) |
resampling |
(mlr3::Resampling) |
measures |
(mlr3::Measure or list of mlr3::Measure) |
terminator |
(bbotk::Terminator) |
search_space |
(paradox::ParamSet) |
internal_search_space |
(paradox::ParamSet or |
store_benchmark_result |
( |
store_models |
( |
check_values |
( |
callbacks |
(list of mlr3misc::Callback) |
There are several sections about hyperparameter optimization in the mlr3book.
Getting started with hyperparameter optimization.
An overview of all tuners can be found on our website.
Tune a support vector machine on the Sonar data set.
Learn about tuning spaces.
Estimate the model performance with nested resampling.
Learn about multi-objective optimization.
Simultaneously optimize hyperparameters and use early stopping with XGBoost.
Automate the tuning.
The gallery features a collection of case studies and demos about optimization.
Learn more advanced methods with the Practical Tuning Series.
Learn about hotstarting models.
Run the default hyperparameter configuration of learners as a baseline.
Use the Hyperband optimizer with different budget parameters.
The cheatsheet summarizes the most important functions of mlr3tuning.
If no measure is passed, the default measure is used. The default measure depends on the task type.
Task | Default Measure | Package |
"classif" |
"classif.ce" |
mlr3 |
"regr" |
"regr.mse" |
mlr3 |
"surv" |
"surv.cindex" |
mlr3proba |
"dens" |
"dens.logloss" |
mlr3proba |
"classif_st" |
"classif.ce" |
mlr3spatial |
"regr_st" |
"regr.mse" |
mlr3spatial |
"clust" |
"clust.dunn" |
mlr3cluster |
# Hyperparameter optimization on the Palmer Penguins data set task = tsk("penguins") # Load learner and set search space learner = lrn("classif.rpart", cp = to_tune(1e-04, 1e-1, logscale = TRUE) ) # Construct tuning instance instance = ti( task = task, learner = learner, resampling = rsmp("cv", folds = 3), measures = msr("classif.ce"), terminator = trm("evals", n_evals = 4) ) # Choose optimization algorithm tuner = tnr("random_search", batch_size = 2) # Run tuning tuner$optimize(instance) # Set optimal hyperparameter configuration to learner learner$param_set$values = instance$result_learner_param_vals # Train the learner on the full data set learner$train(task) # Inspect all evaluated configurations as.data.table(instance$archive)
# Hyperparameter optimization on the Palmer Penguins data set task = tsk("penguins") # Load learner and set search space learner = lrn("classif.rpart", cp = to_tune(1e-04, 1e-1, logscale = TRUE) ) # Construct tuning instance instance = ti( task = task, learner = learner, resampling = rsmp("cv", folds = 3), measures = msr("classif.ce"), terminator = trm("evals", n_evals = 4) ) # Choose optimization algorithm tuner = tnr("random_search", batch_size = 2) # Run tuning tuner$optimize(instance) # Set optimal hyperparameter configuration to learner learner$param_set$values = instance$result_learner_param_vals # Train the learner on the full data set learner$train(task) # Inspect all evaluated configurations as.data.table(instance$archive)
Function to construct a TuningInstanceAsyncSingleCrit or TuningInstanceAsyncMultiCrit.
ti_async( task, learner, resampling, measures = NULL, terminator, search_space = NULL, internal_search_space = NULL, store_benchmark_result = TRUE, store_models = FALSE, check_values = FALSE, callbacks = NULL, rush = NULL )
ti_async( task, learner, resampling, measures = NULL, terminator, search_space = NULL, internal_search_space = NULL, store_benchmark_result = TRUE, store_models = FALSE, check_values = FALSE, callbacks = NULL, rush = NULL )
task |
(mlr3::Task) |
learner |
(mlr3::Learner) |
resampling |
(mlr3::Resampling) |
measures |
(mlr3::Measure or list of mlr3::Measure) |
terminator |
(bbotk::Terminator) |
search_space |
(paradox::ParamSet) |
internal_search_space |
(paradox::ParamSet or |
store_benchmark_result |
( |
store_models |
( |
check_values |
( |
callbacks |
(list of mlr3misc::Callback) |
rush |
( |
There are several sections about hyperparameter optimization in the mlr3book.
Getting started with hyperparameter optimization.
An overview of all tuners can be found on our website.
Tune a support vector machine on the Sonar data set.
Learn about tuning spaces.
Estimate the model performance with nested resampling.
Learn about multi-objective optimization.
Simultaneously optimize hyperparameters and use early stopping with XGBoost.
Automate the tuning.
The gallery features a collection of case studies and demos about optimization.
Learn more advanced methods with the Practical Tuning Series.
Learn about hotstarting models.
Run the default hyperparameter configuration of learners as a baseline.
Use the Hyperband optimizer with different budget parameters.
The cheatsheet summarizes the most important functions of mlr3tuning.
If no measure is passed, the default measure is used. The default measure depends on the task type.
Task | Default Measure | Package |
"classif" |
"classif.ce" |
mlr3 |
"regr" |
"regr.mse" |
mlr3 |
"surv" |
"surv.cindex" |
mlr3proba |
"dens" |
"dens.logloss" |
mlr3proba |
"classif_st" |
"classif.ce" |
mlr3spatial |
"regr_st" |
"regr.mse" |
mlr3spatial |
"clust" |
"clust.dunn" |
mlr3cluster |
# Hyperparameter optimization on the Palmer Penguins data set task = tsk("penguins") # Load learner and set search space learner = lrn("classif.rpart", cp = to_tune(1e-04, 1e-1, logscale = TRUE) ) # Construct tuning instance instance = ti( task = task, learner = learner, resampling = rsmp("cv", folds = 3), measures = msr("classif.ce"), terminator = trm("evals", n_evals = 4) ) # Choose optimization algorithm tuner = tnr("random_search", batch_size = 2) # Run tuning tuner$optimize(instance) # Set optimal hyperparameter configuration to learner learner$param_set$values = instance$result_learner_param_vals # Train the learner on the full data set learner$train(task) # Inspect all evaluated configurations as.data.table(instance$archive)
# Hyperparameter optimization on the Palmer Penguins data set task = tsk("penguins") # Load learner and set search space learner = lrn("classif.rpart", cp = to_tune(1e-04, 1e-1, logscale = TRUE) ) # Construct tuning instance instance = ti( task = task, learner = learner, resampling = rsmp("cv", folds = 3), measures = msr("classif.ce"), terminator = trm("evals", n_evals = 4) ) # Choose optimization algorithm tuner = tnr("random_search", batch_size = 2) # Run tuning tuner$optimize(instance) # Set optimal hyperparameter configuration to learner learner$param_set$values = instance$result_learner_param_vals # Train the learner on the full data set learner$train(task) # Inspect all evaluated configurations as.data.table(instance$archive)
Functions to retrieve objects, set parameters and assign to fields in one go.
Relies on mlr3misc::dictionary_sugar_get()
to extract objects from the respective mlr3misc::Dictionary:
tnr()
for a Tuner from mlr_tuners.
tnrs()
for a list of Tuners from mlr_tuners.
trm()
for a bbotk::Terminator from mlr_terminators.
trms()
for a list of Terminators from mlr_terminators.
tnr(.key, ...) tnrs(.keys, ...)
tnr(.key, ...) tnrs(.keys, ...)
.key |
( |
... |
(any) |
.keys |
( |
R6::R6Class object of the respective type, or a list of R6::R6Class objects for the plural versions.
# random search tuner with batch size of 5 tnr("random_search", batch_size = 5) # run time terminator with 20 seconds trm("run_time", secs = 20)
# random search tuner with batch size of 5 tnr("random_search", batch_size = 5) # run time terminator with 20 seconds trm("run_time", secs = 20)
Function to tune a mlr3::Learner.
The function internally creates a TuningInstanceBatchSingleCrit or TuningInstanceBatchMultiCrit which describes the tuning problem.
It executes the tuning with the Tuner (tuner
) and returns the result with the tuning instance ($result
).
The ArchiveBatchTuning and ArchiveAsyncTuning ($archive
) stores all evaluated hyperparameter configurations and performance scores.
You can find an overview of all tuners on our website.
tune( tuner, task, learner, resampling, measures = NULL, term_evals = NULL, term_time = NULL, terminator = NULL, search_space = NULL, store_benchmark_result = TRUE, internal_search_space = NULL, store_models = FALSE, check_values = FALSE, callbacks = NULL, rush = NULL )
tune( tuner, task, learner, resampling, measures = NULL, term_evals = NULL, term_time = NULL, terminator = NULL, search_space = NULL, store_benchmark_result = TRUE, internal_search_space = NULL, store_models = FALSE, check_values = FALSE, callbacks = NULL, rush = NULL )
tuner |
(Tuner) |
task |
(mlr3::Task) |
learner |
(mlr3::Learner) |
resampling |
(mlr3::Resampling) |
measures |
(mlr3::Measure or list of mlr3::Measure) |
term_evals |
( |
term_time |
( |
terminator |
(bbotk::Terminator) |
search_space |
(paradox::ParamSet) |
store_benchmark_result |
( |
internal_search_space |
(paradox::ParamSet or |
store_models |
( |
check_values |
( |
callbacks |
(list of mlr3misc::Callback) |
rush |
( |
The mlr3::Task, mlr3::Learner, mlr3::Resampling, mlr3::Measure and bbotk::Terminator are used to construct a TuningInstanceBatchSingleCrit.
If multiple performance mlr3::Measures are supplied, a TuningInstanceBatchMultiCrit is created.
The parameter term_evals
and term_time
are shortcuts to create a bbotk::Terminator.
If both parameters are passed, a bbotk::TerminatorCombo is constructed.
For other Terminators, pass one with terminator
.
If no termination criterion is needed, set term_evals
, term_time
and terminator
to NULL
.
The search space is created from paradox::TuneToken or is supplied by search_space
.
TuningInstanceBatchSingleCrit | TuningInstanceBatchMultiCrit
If no measure is passed, the default measure is used. The default measure depends on the task type.
Task | Default Measure | Package |
"classif" |
"classif.ce" |
mlr3 |
"regr" |
"regr.mse" |
mlr3 |
"surv" |
"surv.cindex" |
mlr3proba |
"dens" |
"dens.logloss" |
mlr3proba |
"classif_st" |
"classif.ce" |
mlr3spatial |
"regr_st" |
"regr.mse" |
mlr3spatial |
"clust" |
"clust.dunn" |
mlr3cluster |
There are several sections about hyperparameter optimization in the mlr3book.
Getting started with hyperparameter optimization.
An overview of all tuners can be found on our website.
Tune a support vector machine on the Sonar data set.
Learn about tuning spaces.
Estimate the model performance with nested resampling.
Learn about multi-objective optimization.
Simultaneously optimize hyperparameters and use early stopping with XGBoost.
Automate the tuning.
The gallery features a collection of case studies and demos about optimization.
Learn more advanced methods with the Practical Tuning Series.
Learn about hotstarting models.
Run the default hyperparameter configuration of learners as a baseline.
Use the Hyperband optimizer with different budget parameters.
The cheatsheet summarizes the most important functions of mlr3tuning.
For analyzing the tuning results, it is recommended to pass the ArchiveBatchTuning to as.data.table()
.
The returned data table is joined with the benchmark result which adds the mlr3::ResampleResult for each hyperparameter evaluation.
The archive provides various getters (e.g. $learners()
) to ease the access.
All getters extract by position (i
) or unique hash (uhash
).
For a complete list of all getters see the methods section.
The benchmark result ($benchmark_result
) allows to score the hyperparameter configurations again on a different measure.
Alternatively, measures can be supplied to as.data.table()
.
The mlr3viz package provides visualizations for tuning results.
# Hyperparameter optimization on the Palmer Penguins data set task = tsk("pima") # Load learner and set search space learner = lrn("classif.rpart", cp = to_tune(1e-04, 1e-1, logscale = TRUE) ) # Run tuning instance = tune( tuner = tnr("random_search", batch_size = 2), task = tsk("pima"), learner = learner, resampling = rsmp ("holdout"), measures = msr("classif.ce"), terminator = trm("evals", n_evals = 4) ) # Set optimal hyperparameter configuration to learner learner$param_set$values = instance$result_learner_param_vals # Train the learner on the full data set learner$train(task) # Inspect all evaluated configurations as.data.table(instance$archive)
# Hyperparameter optimization on the Palmer Penguins data set task = tsk("pima") # Load learner and set search space learner = lrn("classif.rpart", cp = to_tune(1e-04, 1e-1, logscale = TRUE) ) # Run tuning instance = tune( tuner = tnr("random_search", batch_size = 2), task = tsk("pima"), learner = learner, resampling = rsmp ("holdout"), measures = msr("classif.ce"), terminator = trm("evals", n_evals = 4) ) # Set optimal hyperparameter configuration to learner learner$param_set$values = instance$result_learner_param_vals # Train the learner on the full data set learner$train(task) # Inspect all evaluated configurations as.data.table(instance$archive)
Function to conduct nested resampling.
tune_nested( tuner, task, learner, inner_resampling, outer_resampling, measure = NULL, term_evals = NULL, term_time = NULL, terminator = NULL, search_space = NULL, store_tuning_instance = TRUE, store_benchmark_result = TRUE, store_models = FALSE, check_values = FALSE, callbacks = NULL )
tune_nested( tuner, task, learner, inner_resampling, outer_resampling, measure = NULL, term_evals = NULL, term_time = NULL, terminator = NULL, search_space = NULL, store_tuning_instance = TRUE, store_benchmark_result = TRUE, store_models = FALSE, check_values = FALSE, callbacks = NULL )
tuner |
(Tuner) |
task |
(mlr3::Task) |
learner |
(mlr3::Learner) |
inner_resampling |
(mlr3::Resampling) |
outer_resampling |
mlr3::Resampling) |
measure |
(mlr3::Measure) |
term_evals |
( |
term_time |
( |
terminator |
(bbotk::Terminator) |
search_space |
(paradox::ParamSet) |
store_tuning_instance |
( |
store_benchmark_result |
( |
store_models |
( |
check_values |
( |
callbacks |
(list of mlr3misc::Callback) |
# Nested resampling on Palmer Penguins data set rr = tune_nested( tuner = tnr("random_search", batch_size = 2), task = tsk("penguins"), learner = lrn("classif.rpart", cp = to_tune(1e-04, 1e-1, logscale = TRUE)), inner_resampling = rsmp ("holdout"), outer_resampling = rsmp("cv", folds = 2), measure = msr("classif.ce"), term_evals = 2) # Performance scores estimated on the outer resampling rr$score() # Unbiased performance of the final model trained on the full data set rr$aggregate()
# Nested resampling on Palmer Penguins data set rr = tune_nested( tuner = tnr("random_search", batch_size = 2), task = tsk("penguins"), learner = lrn("classif.rpart", cp = to_tune(1e-04, 1e-1, logscale = TRUE)), inner_resampling = rsmp ("holdout"), outer_resampling = rsmp("cv", folds = 2), measure = msr("classif.ce"), term_evals = 2) # Performance scores estimated on the outer resampling rr$score() # Unbiased performance of the final model trained on the full data set rr$aggregate()
The Tuner
implements the optimization algorithm.
Tuner
is an abstract base class that implements the base functionality each tuner must provide.
Additional tuners are provided by the following packages.
mlr3hyperband adds the Hyperband and Successive Halving algorithm.
mlr3mbo adds Bayesian optimization methods.
There are several sections about hyperparameter optimization in the mlr3book.
Getting started with hyperparameter optimization.
An overview of all tuners can be found on our website.
Tune a support vector machine on the Sonar data set.
Learn about tuning spaces.
Estimate the model performance with nested resampling.
Learn about multi-objective optimization.
Simultaneously optimize hyperparameters and use early stopping with XGBoost.
Automate the tuning.
The gallery features a collection of case studies and demos about optimization.
Learn more advanced methods with the Practical Tuning Series.
Learn about hotstarting models.
Run the default hyperparameter configuration of learners as a baseline.
Use the Hyperband optimizer with different budget parameters.
The cheatsheet summarizes the most important functions of mlr3tuning.
id
(character(1)
)
Identifier of the object.
Used in tables, plot and text output.
param_set
(paradox::ParamSet)
Set of control parameters.
param_classes
(character()
)
Supported parameter classes for learner hyperparameters that the tuner can optimize, as given in the paradox::ParamSet $class
field.
properties
(character()
)
Set of properties of the tuner.
Must be a subset of mlr_reflections$tuner_properties
.
packages
(character()
)
Set of required packages.
Note that these packages will be loaded via requireNamespace()
, and are not attached.
label
(character(1)
)
Label for this object.
Can be used in tables, plot and text output instead of the ID.
man
(character(1)
)
String in the format [pkg]::[topic]
pointing to a manual page for this object.
The referenced help package can be opened via method $help()
.
new()
Creates a new instance of this R6 class.
Tuner$new( id = "tuner", param_set, param_classes, properties, packages = character(), label = NA_character_, man = NA_character_ )
id
(character(1)
)
Identifier for the new instance.
param_set
(paradox::ParamSet)
Set of control parameters.
param_classes
(character()
)
Supported parameter classes for learner hyperparameters that the tuner can optimize, as given in the paradox::ParamSet $class
field.
properties
(character()
)
Set of properties of the tuner.
Must be a subset of mlr_reflections$tuner_properties
.
packages
(character()
)
Set of required packages.
Note that these packages will be loaded via requireNamespace()
, and are not attached.
label
(character(1)
)
Label for this object.
Can be used in tables, plot and text output instead of the ID.
man
(character(1)
)
String in the format [pkg]::[topic]
pointing to a manual page for this object.
The referenced help package can be opened via method $help()
.
format()
Helper for print outputs.
Tuner$format(...)
...
(ignored).
(character()
).
print()
Print method.
Tuner$print()
(character()
).
help()
Opens the corresponding help page referenced by field $man
.
Tuner$help()
clone()
The objects of this class are cloneable with this method.
Tuner$clone(deep = FALSE)
deep
Whether to make a deep clone.
Other Tuner:
mlr_tuners
,
mlr_tuners_cmaes
,
mlr_tuners_design_points
,
mlr_tuners_gensa
,
mlr_tuners_grid_search
,
mlr_tuners_internal
,
mlr_tuners_irace
,
mlr_tuners_nloptr
,
mlr_tuners_random_search
The TunerAsync implements the asynchronous optimization algorithm.
TunerAsync is an abstract base class that implements the base functionality each asynchronous tuner must provide.
There are several sections about hyperparameter optimization in the mlr3book.
Getting started with hyperparameter optimization.
An overview of all tuners can be found on our website.
Tune a support vector machine on the Sonar data set.
Learn about tuning spaces.
Estimate the model performance with nested resampling.
Learn about multi-objective optimization.
Simultaneously optimize hyperparameters and use early stopping with XGBoost.
Automate the tuning.
The gallery features a collection of case studies and demos about optimization.
Learn more advanced methods with the Practical Tuning Series.
Learn about hotstarting models.
Run the default hyperparameter configuration of learners as a baseline.
Use the Hyperband optimizer with different budget parameters.
The cheatsheet summarizes the most important functions of mlr3tuning.
mlr3tuning::Tuner
-> TunerAsync
optimize()
Performs the tuning on a TuningInstanceAsyncSingleCrit or TuningInstanceAsyncMultiCrit until termination. The single evaluations will be written into the ArchiveAsyncTuning that resides in the TuningInstanceAsyncSingleCrit/TuningInstanceAsyncMultiCrit. The result will be written into the instance object.
TunerAsync$optimize(inst)
clone()
The objects of this class are cloneable with this method.
TunerAsync$clone(deep = FALSE)
deep
Whether to make a deep clone.
The TunerBatch implements the optimization algorithm.
TunerBatch is an abstract base class that implements the base functionality each tuner must provide. A subclass is implemented in the following way:
Inherit from Tuner.
Specify the private abstract method $.optimize()
and use it to call into your optimizer.
You need to call instance$eval_batch()
to evaluate design points.
The batch evaluation is requested at the TuningInstanceBatchSingleCrit/TuningInstanceBatchMultiCrit object instance
, so each batch is possibly executed in parallel via mlr3::benchmark()
, and all evaluations are stored inside of instance$archive
.
Before the batch evaluation, the bbotk::Terminator is checked, and if it is positive, an exception of class "terminated_error"
is generated.
In the later case the current batch of evaluations is still stored in instance
, but the numeric scores are not sent back to the handling optimizer as it has lost execution control.
After such an exception was caught we select the best configuration from instance$archive
and return it.
Note that therefore more points than specified by the bbotk::Terminator may be evaluated, as the Terminator is only checked before a batch evaluation, and not in-between evaluation in a batch. How many more depends on the setting of the batch size.
Overwrite the private super-method .assign_result()
if you want to decide yourself how to estimate the final configuration in the instance and its estimated performance.
The default behavior is: We pick the best resample-experiment, regarding the given measure, then assign its configuration and aggregated performance to the instance.
.optimize(instance)
-> NULL
Abstract base method. Implement to specify tuning of your subclass.
See details sections.
.assign_result(instance)
-> NULL
Abstract base method. Implement to specify how the final configuration is selected.
See details sections.
There are several sections about hyperparameter optimization in the mlr3book.
Getting started with hyperparameter optimization.
An overview of all tuners can be found on our website.
Tune a support vector machine on the Sonar data set.
Learn about tuning spaces.
Estimate the model performance with nested resampling.
Learn about multi-objective optimization.
Simultaneously optimize hyperparameters and use early stopping with XGBoost.
Automate the tuning.
The gallery features a collection of case studies and demos about optimization.
Learn more advanced methods with the Practical Tuning Series.
Learn about hotstarting models.
Run the default hyperparameter configuration of learners as a baseline.
Use the Hyperband optimizer with different budget parameters.
The cheatsheet summarizes the most important functions of mlr3tuning.
mlr3tuning::Tuner
-> TunerBatch
new()
Creates a new instance of this R6 class.
TunerBatch$new( id = "tuner_batch", param_set, param_classes, properties, packages = character(), label = NA_character_, man = NA_character_ )
id
(character(1)
)
Identifier for the new instance.
param_set
(paradox::ParamSet)
Set of control parameters.
param_classes
(character()
)
Supported parameter classes for learner hyperparameters that the tuner can optimize, as given in the paradox::ParamSet $class
field.
properties
(character()
)
Set of properties of the tuner.
Must be a subset of mlr_reflections$tuner_properties
.
packages
(character()
)
Set of required packages.
Note that these packages will be loaded via requireNamespace()
, and are not attached.
label
(character(1)
)
Label for this object.
Can be used in tables, plot and text output instead of the ID.
man
(character(1)
)
String in the format [pkg]::[topic]
pointing to a manual page for this object.
The referenced help package can be opened via method $help()
.
optimize()
Performs the tuning on a TuningInstanceBatchSingleCrit or TuningInstanceBatchMultiCrit until termination. The single evaluations will be written into the ArchiveBatchTuning that resides in the TuningInstanceBatchSingleCrit/TuningInstanceBatchMultiCrit. The result will be written into the instance object.
TunerBatch$optimize(inst)
clone()
The objects of this class are cloneable with this method.
TunerBatch$clone(deep = FALSE)
deep
Whether to make a deep clone.
The TuningInstanceAsyncMultiCrit specifies a tuning problem for a Tuner.
The function ti_async()
creates a TuningInstanceAsyncMultiCrit and the function tune()
creates an instance internally.
The instance contains an ObjectiveTuningAsync object that encodes the black box objective function a Tuner has to optimize.
The instance allows the basic operations of querying the objective at design points ($eval_async()
).
This operation is usually done by the Tuner.
Hyperparameter configurations are asynchronously sent to workers and evaluated by calling mlr3::resample()
.
The evaluated hyperparameter configurations are stored in the ArchiveAsyncTuning ($archive
).
Before a batch is evaluated, the bbotk::Terminator is queried for the remaining budget.
If the available budget is exhausted, an exception is raised, and no further evaluations can be performed from this point on.
The tuner is also supposed to store its final result, consisting of a selected hyperparameter configuration and associated estimated performance values, by calling the method instance$.assign_result
.
There are several sections about hyperparameter optimization in the mlr3book.
Getting started with hyperparameter optimization.
An overview of all tuners can be found on our website.
Tune a support vector machine on the Sonar data set.
Learn about tuning spaces.
Estimate the model performance with nested resampling.
Learn about multi-objective optimization.
Simultaneously optimize hyperparameters and use early stopping with XGBoost.
Automate the tuning.
The gallery features a collection of case studies and demos about optimization.
Learn more advanced methods with the Practical Tuning Series.
Learn about hotstarting models.
Run the default hyperparameter configuration of learners as a baseline.
Use the Hyperband optimizer with different budget parameters.
The cheatsheet summarizes the most important functions of mlr3tuning.
For analyzing the tuning results, it is recommended to pass the ArchiveAsyncTuning to as.data.table()
.
The returned data table contains the mlr3::ResampleResult for each hyperparameter evaluation.
bbotk::OptimInstance
-> bbotk::OptimInstanceAsync
-> bbotk::OptimInstanceAsyncMultiCrit
-> TuningInstanceAsyncMultiCrit
internal_search_space
(paradox::ParamSet)
The search space containing those parameters that are internally optimized by the mlr3::Learner.
result_learner_param_vals
(list()
)
List of param values for the optimal learner call.
new()
Creates a new instance of this R6 class.
TuningInstanceAsyncMultiCrit$new( task, learner, resampling, measures, terminator, search_space = NULL, internal_search_space = NULL, store_benchmark_result = TRUE, store_models = FALSE, check_values = FALSE, callbacks = NULL, rush = NULL )
task
(mlr3::Task)
Task to operate on.
learner
(mlr3::Learner)
Learner to tune.
resampling
(mlr3::Resampling)
Resampling that is used to evaluate the performance of the hyperparameter configurations.
Uninstantiated resamplings are instantiated during construction so that all configurations are evaluated on the same data splits.
Already instantiated resamplings are kept unchanged.
Specialized Tuner change the resampling e.g. to evaluate a hyperparameter configuration on different data splits.
This field, however, always returns the resampling passed in construction.
measures
(list of mlr3::Measure)
Measures to optimize.
terminator
(bbotk::Terminator)
Stop criterion of the tuning process.
search_space
(paradox::ParamSet)
Hyperparameter search space. If NULL
(default), the search space is
constructed from the paradox::TuneToken of the learner's parameter set
(learner$param_set).
internal_search_space
(paradox::ParamSet or NULL
)
The internal search space.
store_benchmark_result
(logical(1)
)
If TRUE
(default), store resample result of evaluated hyperparameter
configurations in archive as mlr3::BenchmarkResult.
store_models
(logical(1)
)
If TRUE
, fitted models are stored in the benchmark result
(archive$benchmark_result
). If store_benchmark_result = FALSE
, models
are only stored temporarily and not accessible after the tuning. This
combination is needed for measures that require a model.
check_values
(logical(1)
)
If TRUE
, hyperparameter values are checked before evaluation and
performance scores after. If FALSE
(default), values are unchecked but
computational overhead is reduced.
callbacks
(list of mlr3misc::Callback)
List of callbacks.
rush
(Rush
)
If a rush instance is supplied, the tuning runs without batches.
assign_result()
The TunerAsync writes the best found points and estimated performance values here (probably the Pareto set / front). For internal use.
TuningInstanceAsyncMultiCrit$assign_result( xdt, ydt, learner_param_vals = NULL, extra = NULL, xydt = NULL, ... )
xdt
(data.table::data.table()
)
Hyperparameter values as data.table::data.table()
. Each row is one
configuration. Contains values in the search space. Can contain additional
columns for extra information.
ydt
(numeric(1)
)
Optimal outcomes, e.g. the Pareto front.
learner_param_vals
(List of named list()s
)
Fixed parameter values of the learner that are neither part of the
extra
(data.table::data.table()
)
Additional information.
xydt
(data.table::data.table()
)
Point, outcome, and additional information.
...
(any
)
ignored.
clone()
The objects of this class are cloneable with this method.
TuningInstanceAsyncMultiCrit$clone(deep = FALSE)
deep
Whether to make a deep clone.
The TuningInstanceAsyncSingleCrit
specifies a tuning problem for a TunerAsync.
The function ti_async()
creates a TuningInstanceAsyncSingleCrit and the function tune()
creates an instance internally.
The instance contains an ObjectiveTuningAsync object that encodes the black box objective function a Tuner has to optimize.
The instance allows the basic operations of querying the objective at design points ($eval_async()
).
This operation is usually done by the Tuner.
Hyperparameter configurations are asynchronously sent to workers and evaluated by calling mlr3::resample()
.
The evaluated hyperparameter configurations are stored in the ArchiveAsyncTuning ($archive
).
Before a batch is evaluated, the bbotk::Terminator is queried for the remaining budget.
If the available budget is exhausted, an exception is raised, and no further evaluations can be performed from this point on.
The tuner is also supposed to store its final result, consisting of a selected hyperparameter configuration and associated estimated performance values, by calling the method instance$.assign_result
.
If no measure is passed, the default measure is used. The default measure depends on the task type.
Task | Default Measure | Package |
"classif" |
"classif.ce" |
mlr3 |
"regr" |
"regr.mse" |
mlr3 |
"surv" |
"surv.cindex" |
mlr3proba |
"dens" |
"dens.logloss" |
mlr3proba |
"classif_st" |
"classif.ce" |
mlr3spatial |
"regr_st" |
"regr.mse" |
mlr3spatial |
"clust" |
"clust.dunn" |
mlr3cluster |
For analyzing the tuning results, it is recommended to pass the ArchiveAsyncTuning to as.data.table()
.
The returned data table contains the mlr3::ResampleResult for each hyperparameter evaluation.
There are several sections about hyperparameter optimization in the mlr3book.
Getting started with hyperparameter optimization.
An overview of all tuners can be found on our website.
Tune a support vector machine on the Sonar data set.
Learn about tuning spaces.
Estimate the model performance with nested resampling.
Learn about multi-objective optimization.
Simultaneously optimize hyperparameters and use early stopping with XGBoost.
Automate the tuning.
The gallery features a collection of case studies and demos about optimization.
Learn more advanced methods with the Practical Tuning Series.
Learn about hotstarting models.
Run the default hyperparameter configuration of learners as a baseline.
Use the Hyperband optimizer with different budget parameters.
The cheatsheet summarizes the most important functions of mlr3tuning.
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.
bbotk::OptimInstance
-> bbotk::OptimInstanceAsync
-> bbotk::OptimInstanceAsyncSingleCrit
-> TuningInstanceAsyncSingleCrit
internal_search_space
(paradox::ParamSet)
The search space containing those parameters that are internally optimized by the mlr3::Learner.
result_learner_param_vals
(list()
)
Param values for the optimal learner call.
new()
Creates a new instance of this R6 class.
TuningInstanceAsyncSingleCrit$new( task, learner, resampling, measure = NULL, terminator, search_space = NULL, internal_search_space = NULL, store_benchmark_result = TRUE, store_models = FALSE, check_values = FALSE, callbacks = NULL, rush = NULL )
task
(mlr3::Task)
Task to operate on.
learner
(mlr3::Learner)
Learner to tune.
resampling
(mlr3::Resampling)
Resampling that is used to evaluate the performance of the hyperparameter configurations.
Uninstantiated resamplings are instantiated during construction so that all configurations are evaluated on the same data splits.
Already instantiated resamplings are kept unchanged.
Specialized Tuner change the resampling e.g. to evaluate a hyperparameter configuration on different data splits.
This field, however, always returns the resampling passed in construction.
measure
(mlr3::Measure)
Measure to optimize. If NULL
, default measure is used.
terminator
(bbotk::Terminator)
Stop criterion of the tuning process.
search_space
(paradox::ParamSet)
Hyperparameter search space. If NULL
(default), the search space is
constructed from the paradox::TuneToken of the learner's parameter set
(learner$param_set).
internal_search_space
(paradox::ParamSet or NULL
)
The internal search space.
internal_search_space
(paradox::ParamSet or NULL
)
The internal search space.
store_benchmark_result
(logical(1)
)
If TRUE
(default), store resample result of evaluated hyperparameter
configurations in archive as mlr3::BenchmarkResult.
store_models
(logical(1)
)
If TRUE
, fitted models are stored in the benchmark result
(archive$benchmark_result
). If store_benchmark_result = FALSE
, models
are only stored temporarily and not accessible after the tuning. This
combination is needed for measures that require a model.
check_values
(logical(1)
)
If TRUE
, hyperparameter values are checked before evaluation and
performance scores after. If FALSE
(default), values are unchecked but
computational overhead is reduced.
callbacks
(list of mlr3misc::Callback)
List of callbacks.
rush
(Rush
)
If a rush instance is supplied, the tuning runs without batches.
assign_result()
The TunerAsync object writes the best found point and estimated performance value here. For internal use.
TuningInstanceAsyncSingleCrit$assign_result( xdt, y, learner_param_vals = NULL, extra = NULL, xydt = NULL, ... )
xdt
(data.table::data.table()
)
Hyperparameter values as data.table::data.table()
. Each row is one
configuration. Contains values in the search space. Can contain additional
columns for extra information.
y
(numeric(1)
)
Optimal outcome.
learner_param_vals
(List of named list()s
)
Fixed parameter values of the learner that are neither part of the
extra
(data.table::data.table()
)
Additional information.
xydt
(data.table::data.table()
)
Point, outcome, and additional information (Deprecated).
...
(any
)
ignored.
clone()
The objects of this class are cloneable with this method.
TuningInstanceAsyncSingleCrit$clone(deep = FALSE)
deep
Whether to make a deep clone.
The TuningInstanceBatchMultiCrit specifies a tuning problem for a Tuner.
The function ti()
creates a TuningInstanceBatchMultiCrit and the function tune()
creates an instance internally.
The instance contains an ObjectiveTuningBatch object that encodes the black box objective function a Tuner has to optimize.
The instance allows the basic operations of querying the objective at design points ($eval_batch()
).
This operation is usually done by the Tuner.
Evaluations of hyperparameter configurations are performed in batches by calling mlr3::benchmark()
internally.
The evaluated hyperparameter configurations are stored in the ArchiveBatchTuning ($archive
).
Before a batch is evaluated, the bbotk::Terminator is queried for the remaining budget.
If the available budget is exhausted, an exception is raised, and no further evaluations can be performed from this point on.
The tuner is also supposed to store its final result, consisting of a selected hyperparameter configuration and associated estimated performance values, by calling the method instance$assign_result
.
There are several sections about hyperparameter optimization in the mlr3book.
Getting started with hyperparameter optimization.
An overview of all tuners can be found on our website.
Tune a support vector machine on the Sonar data set.
Learn about tuning spaces.
Estimate the model performance with nested resampling.
Learn about multi-objective optimization.
Simultaneously optimize hyperparameters and use early stopping with XGBoost.
Automate the tuning.
The gallery features a collection of case studies and demos about optimization.
Learn more advanced methods with the Practical Tuning Series.
Learn about hotstarting models.
Run the default hyperparameter configuration of learners as a baseline.
Use the Hyperband optimizer with different budget parameters.
The cheatsheet summarizes the most important functions of mlr3tuning.
For analyzing the tuning results, it is recommended to pass the ArchiveBatchTuning to as.data.table()
.
The returned data table is joined with the benchmark result which adds the mlr3::ResampleResult for each hyperparameter evaluation.
The archive provides various getters (e.g. $learners()
) to ease the access.
All getters extract by position (i
) or unique hash (uhash
).
For a complete list of all getters see the methods section.
The benchmark result ($benchmark_result
) allows to score the hyperparameter configurations again on a different measure.
Alternatively, measures can be supplied to as.data.table()
.
The mlr3viz package provides visualizations for tuning results.
bbotk::OptimInstance
-> bbotk::OptimInstanceBatch
-> bbotk::OptimInstanceBatchMultiCrit
-> TuningInstanceBatchMultiCrit
internal_search_space
(paradox::ParamSet)
The search space containing those parameters that are internally optimized by the mlr3::Learner.
result_learner_param_vals
(list()
)
List of param values for the optimal learner call.
new()
Creates a new instance of this R6 class.
TuningInstanceBatchMultiCrit$new( task, learner, resampling, measures, terminator, search_space = NULL, internal_search_space = NULL, store_benchmark_result = TRUE, store_models = FALSE, check_values = FALSE, callbacks = NULL )
task
(mlr3::Task)
Task to operate on.
learner
(mlr3::Learner)
Learner to tune.
resampling
(mlr3::Resampling)
Resampling that is used to evaluate the performance of the hyperparameter configurations.
Uninstantiated resamplings are instantiated during construction so that all configurations are evaluated on the same data splits.
Already instantiated resamplings are kept unchanged.
Specialized Tuner change the resampling e.g. to evaluate a hyperparameter configuration on different data splits.
This field, however, always returns the resampling passed in construction.
measures
(list of mlr3::Measure)
Measures to optimize.
terminator
(bbotk::Terminator)
Stop criterion of the tuning process.
search_space
(paradox::ParamSet)
Hyperparameter search space. If NULL
(default), the search space is
constructed from the paradox::TuneToken of the learner's parameter set
(learner$param_set).
internal_search_space
(paradox::ParamSet or NULL
)
The internal search space.
internal_search_space
(paradox::ParamSet or NULL
)
The internal search space.
store_benchmark_result
(logical(1)
)
If TRUE
(default), store resample result of evaluated hyperparameter
configurations in archive as mlr3::BenchmarkResult.
store_models
(logical(1)
)
If TRUE
, fitted models are stored in the benchmark result
(archive$benchmark_result
). If store_benchmark_result = FALSE
, models
are only stored temporarily and not accessible after the tuning. This
combination is needed for measures that require a model.
check_values
(logical(1)
)
If TRUE
, hyperparameter values are checked before evaluation and
performance scores after. If FALSE
(default), values are unchecked but
computational overhead is reduced.
callbacks
(list of mlr3misc::Callback)
List of callbacks.
assign_result()
The Tuner object writes the best found points and estimated performance values here. For internal use.
TuningInstanceBatchMultiCrit$assign_result( xdt, ydt, learner_param_vals = NULL, extra = NULL, xydt = NULL, ... )
xdt
(data.table::data.table()
)
Hyperparameter values as data.table::data.table()
. Each row is one
configuration. Contains values in the search space. Can contain additional
columns for extra information.
ydt
(data.table::data.table()
)
Optimal outcomes, e.g. the Pareto front.
learner_param_vals
(List of named list()s
)
Fixed parameter values of the learner that are neither part of the
extra
(data.table::data.table()
)
Additional information.
xydt
(data.table::data.table()
)
Point, outcome, and additional information (Deprecated).
...
(any
)
ignored.
clone()
The objects of this class are cloneable with this method.
TuningInstanceBatchMultiCrit$clone(deep = FALSE)
deep
Whether to make a deep clone.
# Hyperparameter optimization on the Palmer Penguins data set task = tsk("penguins") # Load learner and set search space learner = lrn("classif.rpart", cp = to_tune(1e-04, 1e-1, logscale = TRUE) ) # Construct tuning instance instance = ti( task = task, learner = learner, resampling = rsmp("cv", folds = 3), measures = msrs(c("classif.ce", "time_train")), terminator = trm("evals", n_evals = 4) ) # Choose optimization algorithm tuner = tnr("random_search", batch_size = 2) # Run tuning tuner$optimize(instance) # Optimal hyperparameter configurations instance$result # Inspect all evaluated configurations as.data.table(instance$archive)
# Hyperparameter optimization on the Palmer Penguins data set task = tsk("penguins") # Load learner and set search space learner = lrn("classif.rpart", cp = to_tune(1e-04, 1e-1, logscale = TRUE) ) # Construct tuning instance instance = ti( task = task, learner = learner, resampling = rsmp("cv", folds = 3), measures = msrs(c("classif.ce", "time_train")), terminator = trm("evals", n_evals = 4) ) # Choose optimization algorithm tuner = tnr("random_search", batch_size = 2) # Run tuning tuner$optimize(instance) # Optimal hyperparameter configurations instance$result # Inspect all evaluated configurations as.data.table(instance$archive)
The TuningInstanceBatchSingleCrit specifies a tuning problem for a Tuner.
The function ti()
creates a TuningInstanceBatchSingleCrit and the function tune()
creates an instance internally.
The instance contains an ObjectiveTuningBatch object that encodes the black box objective function a Tuner has to optimize.
The instance allows the basic operations of querying the objective at design points ($eval_batch()
).
This operation is usually done by the Tuner.
Evaluations of hyperparameter configurations are performed in batches by calling mlr3::benchmark()
internally.
The evaluated hyperparameter configurations are stored in the ArchiveBatchTuning ($archive
).
Before a batch is evaluated, the bbotk::Terminator is queried for the remaining budget.
If the available budget is exhausted, an exception is raised, and no further evaluations can be performed from this point on.
The tuner is also supposed to store its final result, consisting of a selected hyperparameter configuration and associated estimated performance values, by calling the method instance$assign_result
.
If no measure is passed, the default measure is used. The default measure depends on the task type.
Task | Default Measure | Package |
"classif" |
"classif.ce" |
mlr3 |
"regr" |
"regr.mse" |
mlr3 |
"surv" |
"surv.cindex" |
mlr3proba |
"dens" |
"dens.logloss" |
mlr3proba |
"classif_st" |
"classif.ce" |
mlr3spatial |
"regr_st" |
"regr.mse" |
mlr3spatial |
"clust" |
"clust.dunn" |
mlr3cluster |
There are several sections about hyperparameter optimization in the mlr3book.
Getting started with hyperparameter optimization.
An overview of all tuners can be found on our website.
Tune a support vector machine on the Sonar data set.
Learn about tuning spaces.
Estimate the model performance with nested resampling.
Learn about multi-objective optimization.
Simultaneously optimize hyperparameters and use early stopping with XGBoost.
Automate the tuning.
The gallery features a collection of case studies and demos about optimization.
Learn more advanced methods with the Practical Tuning Series.
Learn about hotstarting models.
Run the default hyperparameter configuration of learners as a baseline.
Use the Hyperband optimizer with different budget parameters.
The cheatsheet summarizes the most important functions of mlr3tuning.
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.
For analyzing the tuning results, it is recommended to pass the ArchiveBatchTuning to as.data.table()
.
The returned data table is joined with the benchmark result which adds the mlr3::ResampleResult for each hyperparameter evaluation.
The archive provides various getters (e.g. $learners()
) to ease the access.
All getters extract by position (i
) or unique hash (uhash
).
For a complete list of all getters see the methods section.
The benchmark result ($benchmark_result
) allows to score the hyperparameter configurations again on a different measure.
Alternatively, measures can be supplied to as.data.table()
.
The mlr3viz package provides visualizations for tuning results.
bbotk::OptimInstance
-> bbotk::OptimInstanceBatch
-> bbotk::OptimInstanceBatchSingleCrit
-> TuningInstanceBatchSingleCrit
internal_search_space
(paradox::ParamSet)
The search space containing those parameters that are internally optimized by the mlr3::Learner.
result_learner_param_vals
(list()
)
Param values for the optimal learner call.
new()
Creates a new instance of this R6 class.
TuningInstanceBatchSingleCrit$new( task, learner, resampling, measure = NULL, terminator, search_space = NULL, internal_search_space = NULL, store_benchmark_result = TRUE, store_models = FALSE, check_values = FALSE, callbacks = NULL )
task
(mlr3::Task)
Task to operate on.
learner
(mlr3::Learner)
Learner to tune.
resampling
(mlr3::Resampling)
Resampling that is used to evaluate the performance of the hyperparameter configurations.
Uninstantiated resamplings are instantiated during construction so that all configurations are evaluated on the same data splits.
Already instantiated resamplings are kept unchanged.
Specialized Tuner change the resampling e.g. to evaluate a hyperparameter configuration on different data splits.
This field, however, always returns the resampling passed in construction.
measure
(mlr3::Measure)
Measure to optimize. If NULL
, default measure is used.
terminator
(bbotk::Terminator)
Stop criterion of the tuning process.
search_space
(paradox::ParamSet)
Hyperparameter search space. If NULL
(default), the search space is
constructed from the paradox::TuneToken of the learner's parameter set
(learner$param_set).
internal_search_space
(paradox::ParamSet or NULL
)
The internal search space.
store_benchmark_result
(logical(1)
)
If TRUE
(default), store resample result of evaluated hyperparameter
configurations in archive as mlr3::BenchmarkResult.
store_models
(logical(1)
)
If TRUE
, fitted models are stored in the benchmark result
(archive$benchmark_result
). If store_benchmark_result = FALSE
, models
are only stored temporarily and not accessible after the tuning. This
combination is needed for measures that require a model.
check_values
(logical(1)
)
If TRUE
, hyperparameter values are checked before evaluation and
performance scores after. If FALSE
(default), values are unchecked but
computational overhead is reduced.
callbacks
(list of mlr3misc::Callback)
List of callbacks.
assign_result()
The Tuner object writes the best found point and estimated performance value here. For internal use.
TuningInstanceBatchSingleCrit$assign_result( xdt, y, learner_param_vals = NULL, extra = NULL, xydt = NULL, ... )
xdt
(data.table::data.table()
)
Hyperparameter values as data.table::data.table()
. Each row is one
configuration. Contains values in the search space. Can contain additional
columns for extra information.
y
(numeric(1)
)
Optimal outcome.
learner_param_vals
(List of named list()s
)
Fixed parameter values of the learner that are neither part of the
extra
(data.table::data.table()
)
Additional information.
xydt
(data.table::data.table()
)
Point, outcome, and additional information (Deprecated).
...
(any
)
ignored.
clone()
The objects of this class are cloneable with this method.
TuningInstanceBatchSingleCrit$clone(deep = FALSE)
deep
Whether to make a deep clone.
# Hyperparameter optimization on the Palmer Penguins data set task = tsk("penguins") # Load learner and set search space learner = lrn("classif.rpart", cp = to_tune(1e-04, 1e-1, logscale = TRUE) ) # Construct tuning instance instance = ti( task = task, learner = learner, resampling = rsmp("cv", folds = 3), measures = msr("classif.ce"), terminator = trm("evals", n_evals = 4) ) # Choose optimization algorithm tuner = tnr("random_search", batch_size = 2) # Run tuning tuner$optimize(instance) # Set optimal hyperparameter configuration to learner learner$param_set$values = instance$result_learner_param_vals # Train the learner on the full data set learner$train(task) # Inspect all evaluated configurations as.data.table(instance$archive)
# Hyperparameter optimization on the Palmer Penguins data set task = tsk("penguins") # Load learner and set search space learner = lrn("classif.rpart", cp = to_tune(1e-04, 1e-1, logscale = TRUE) ) # Construct tuning instance instance = ti( task = task, learner = learner, resampling = rsmp("cv", folds = 3), measures = msr("classif.ce"), terminator = trm("evals", n_evals = 4) ) # Choose optimization algorithm tuner = tnr("random_search", batch_size = 2) # Run tuning tuner$optimize(instance) # Set optimal hyperparameter configuration to learner learner$param_set$values = instance$result_learner_param_vals # Train the learner on the full data set learner$train(task) # Inspect all evaluated configurations as.data.table(instance$archive)
TuningInstanceMultiCrit
is a deprecated class that is now a wrapper around TuningInstanceBatchMultiCrit.
bbotk::OptimInstance
-> bbotk::OptimInstanceBatch
-> bbotk::OptimInstanceBatchMultiCrit
-> mlr3tuning::TuningInstanceBatchMultiCrit
-> TuningInstanceMultiCrit
new()
Creates a new instance of this R6 class.
TuningInstanceMultiCrit$new( task, learner, resampling, measures, terminator, search_space = NULL, store_benchmark_result = TRUE, store_models = FALSE, check_values = FALSE, callbacks = NULL )
task
(mlr3::Task)
Task to operate on.
learner
(mlr3::Learner)
Learner to tune.
resampling
(mlr3::Resampling)
Resampling that is used to evaluate the performance of the hyperparameter configurations.
Uninstantiated resamplings are instantiated during construction so that all configurations are evaluated on the same data splits.
Already instantiated resamplings are kept unchanged.
Specialized Tuner change the resampling e.g. to evaluate a hyperparameter configuration on different data splits.
This field, however, always returns the resampling passed in construction.
measures
(list of mlr3::Measure)
Measures to optimize.
terminator
(bbotk::Terminator)
Stop criterion of the tuning process.
search_space
(paradox::ParamSet)
Hyperparameter search space. If NULL
(default), the search space is
constructed from the paradox::TuneToken of the learner's parameter set
(learner$param_set).
store_benchmark_result
(logical(1)
)
If TRUE
(default), store resample result of evaluated hyperparameter
configurations in archive as mlr3::BenchmarkResult.
store_models
(logical(1)
)
If TRUE
, fitted models are stored in the benchmark result
(archive$benchmark_result
). If store_benchmark_result = FALSE
, models
are only stored temporarily and not accessible after the tuning. This
combination is needed for measures that require a model.
check_values
(logical(1)
)
If TRUE
, hyperparameter values are checked before evaluation and
performance scores after. If FALSE
(default), values are unchecked but
computational overhead is reduced.
callbacks
(list of mlr3misc::Callback)
List of callbacks.
clone()
The objects of this class are cloneable with this method.
TuningInstanceMultiCrit$clone(deep = FALSE)
deep
Whether to make a deep clone.
TuningInstanceSingleCrit
is a deprecated class that is now a wrapper around TuningInstanceBatchSingleCrit.
bbotk::OptimInstance
-> bbotk::OptimInstanceBatch
-> bbotk::OptimInstanceBatchSingleCrit
-> mlr3tuning::TuningInstanceBatchSingleCrit
-> TuningInstanceSingleCrit
new()
Creates a new instance of this R6 class.
TuningInstanceSingleCrit$new( task, learner, resampling, measure = NULL, terminator, search_space = NULL, store_benchmark_result = TRUE, store_models = FALSE, check_values = FALSE, callbacks = NULL )
task
(mlr3::Task)
Task to operate on.
learner
(mlr3::Learner)
Learner to tune.
resampling
(mlr3::Resampling)
Resampling that is used to evaluate the performance of the hyperparameter configurations.
Uninstantiated resamplings are instantiated during construction so that all configurations are evaluated on the same data splits.
Already instantiated resamplings are kept unchanged.
Specialized Tuner change the resampling e.g. to evaluate a hyperparameter configuration on different data splits.
This field, however, always returns the resampling passed in construction.
measure
(mlr3::Measure)
Measure to optimize. If NULL
, default measure is used.
terminator
(bbotk::Terminator)
Stop criterion of the tuning process.
search_space
(paradox::ParamSet)
Hyperparameter search space. If NULL
(default), the search space is
constructed from the paradox::TuneToken of the learner's parameter set
(learner$param_set).
store_benchmark_result
(logical(1)
)
If TRUE
(default), store resample result of evaluated hyperparameter
configurations in archive as mlr3::BenchmarkResult.
store_models
(logical(1)
)
If TRUE
, fitted models are stored in the benchmark result
(archive$benchmark_result
). If store_benchmark_result = FALSE
, models
are only stored temporarily and not accessible after the tuning. This
combination is needed for measures that require a model.
check_values
(logical(1)
)
If TRUE
, hyperparameter values are checked before evaluation and
performance scores after. If FALSE
(default), values are unchecked but
computational overhead is reduced.
callbacks
(list of mlr3misc::Callback)
List of callbacks.
clone()
The objects of this class are cloneable with this method.
TuningInstanceSingleCrit$clone(deep = FALSE)
deep
Whether to make a deep clone.