Skip to content

Commit

Permalink
compatibility: mlr3 0.22.0 (#465)
Browse files Browse the repository at this point in the history
* compatibility: mlr3 0.22.0

* ...

* ...

* ...

* ...
  • Loading branch information
be-marc authored Nov 7, 2024
1 parent c6cabfc commit 27e5fd6
Show file tree
Hide file tree
Showing 15 changed files with 64 additions and 31 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# mlr3tuning (development version)

* compatibility: mlr3 0.22.0

# mlr3tuning 1.1.0

* fix: The `as_data_table()` functions do not unnest the `x_domain` colum anymore by default.
Expand Down
8 changes: 6 additions & 2 deletions R/AutoTuner.R
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
#' @template param_check_values
#' @template param_callbacks
#' @template param_rush
#' @template param_id
#'
#' @export
#' @examples
Expand Down Expand Up @@ -144,7 +145,8 @@ AutoTuner = R6Class("AutoTuner",
store_models = FALSE,
check_values = FALSE,
callbacks = NULL,
rush = NULL
rush = NULL,
id = NULL
) {
learner = assert_learner(as_learner(learner, clone = TRUE))

Expand All @@ -170,8 +172,10 @@ AutoTuner = R6Class("AutoTuner",
if (!is.null(rush)) ia$rush = assert_class(rush, "Rush")
self$instance_args = ia

id = assert_string(id, null.ok = TRUE) %??% paste0(learner$id, ".tuned")

super$initialize(
id = paste0(learner$id, ".tuned"),
id = id,
task_type = learner$task_type,
packages = c("mlr3tuning", learner$packages),
feature_types = learner$feature_types,
Expand Down
7 changes: 5 additions & 2 deletions R/auto_tuner.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#' @template param_check_values
#' @template param_callbacks
#' @template param_rush
#' @template param_id
#'
#' @export
#' @examples
Expand All @@ -49,7 +50,8 @@ auto_tuner = function(
store_models = FALSE,
check_values = FALSE,
callbacks = NULL,
rush = NULL
rush = NULL,
id = NULL
) {
terminator = terminator %??% terminator_selection(term_evals, term_time)

Expand All @@ -66,5 +68,6 @@ auto_tuner = function(
store_models = store_models,
check_values = check_values,
callbacks = callbacks,
rush = rush)
rush = rush,
id = id)
}
2 changes: 1 addition & 1 deletion inst/testthat/helper_misc.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ TEST_MAKE_INST2 = function(measure = msr("dummy.cp.regr"), term_evals = 5L) {
ll = LearnerRegrDepParams$new()
rs = rsmp("holdout")
term = trm("evals", n_evals = term_evals)
inst = TuningInstanceBatchSingleCrit$new(tsk("boston_housing"), ll, rs, measure, term, ps)
inst = TuningInstanceBatchSingleCrit$new(tsk("mtcars"), ll, rs, measure, term, ps)
return(inst)
}

Expand Down
2 changes: 1 addition & 1 deletion inst/testthat/helper_tuner.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ test_tuner = function(key, ..., n_dim = 1L, term_evals = 2L, real_evals = term_e
test_tuner_dependencies = function(key, ..., term_evals = 2L) {
term = trm("evals", n_evals = term_evals)
ll = LearnerRegrDepParams$new()
inst = TuningInstanceBatchSingleCrit$new(tsk("boston_housing"), ll, rsmp("holdout"), msr("regr.mse"), term, ll$param_set)
inst = TuningInstanceBatchSingleCrit$new(tsk("mtcars"), ll, rsmp("holdout"), msr("regr.mse"), term, ll$param_set)
tuner = tnr(key, ...)
expect_tuner(tuner)
tuner$optimize(inst)
Expand Down
6 changes: 5 additions & 1 deletion man/AutoTuner.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/TuningInstanceAsyncMultiCrit.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/TuningInstanceAsyncSingleCrit.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion man/auto_tuner.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions man/mlr_tuners_cmaes.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions tests/testthat/test_AutoTuner.R
Original file line number Diff line number Diff line change
Expand Up @@ -337,14 +337,16 @@ test_that("AutoTuner hash works #647 in mlr3", {
resampling = rsmp("holdout"),
measure = msr("classif.ce"),
terminator = trm("evals", n_evals = 4),
tuner = tnr("grid_search", resolution = 3))
tuner = tnr("grid_search", resolution = 3),
id = "at_1")

at_2 = AutoTuner$new(
learner = lrn("classif.rpart", cp = to_tune(0.01, 0.1)),
resampling = rsmp("holdout"),
measure = msr("classif.ce"),
terminator = trm("evals", n_evals = 4),
tuner = tnr("grid_search", resolution = 3))
tuner = tnr("grid_search", resolution = 3),
id = "at_2")

expect_true(at_1$hash != at_2$hash)

Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test_TuningInstanceBatchMultiCrit.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ test_that("check_values flag with parameter set dependencies", {
tuner = tnr("random_search")

inst = TuningInstanceBatchMultiCrit$new(
tsk("boston_housing"), learner,
tsk("mtcars"), learner,
rsmp("holdout"), msrs(c("regr.mse", "regr.rmse")), terminator, search_space, check_values = FALSE)
tuner$optimize(inst)
expect_named(inst$result_learner_param_vals[[1]], c("xx", "cp", "yy"))

inst = TuningInstanceBatchMultiCrit$new(tsk("boston_housing"), learner,
inst = TuningInstanceBatchMultiCrit$new(tsk("mtcars"), learner,
rsmp("holdout"), msr("regr.mse"), terminator, search_space,
check_values = TRUE)
expect_error(tuner$optimize(inst),
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test_TuningInstanceBatchSingleCrit.R
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,12 @@ test_that("check_values flag with parameter set dependencies", {
terminator = trm("evals", n_evals = 20)
tuner = tnr("random_search")

inst = TuningInstanceBatchSingleCrit$new(tsk("boston_housing"), learner,
inst = TuningInstanceBatchSingleCrit$new(tsk("mtcars"), learner,
rsmp("holdout"), msr("regr.mse"), terminator, search_space, check_values = FALSE)
tuner$optimize(inst)
expect_named(inst$result_learner_param_vals, c("xx", "cp", "yy"))

inst = TuningInstanceBatchSingleCrit$new(tsk("boston_housing"), learner,
inst = TuningInstanceBatchSingleCrit$new(tsk("mtcars"), learner,
rsmp("holdout"), msr("regr.mse"), terminator, search_space,
check_values = TRUE)
expect_error(tuner$optimize(inst),
Expand Down
12 changes: 6 additions & 6 deletions tests/testthat/test_extract_inner_tuning_archives.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ test_that("extract_inner_tuning_archives function works", {
expect_named(irr, c("iteration", "cp", "classif.ce", "x_domain_cp", "runtime_learners", "timestamp", "batch_nr", "resample_result", "task_id", "learner_id", "resampling_id", "errors", "warnings"), ignore.order = TRUE)

# cv
at_1 = AutoTuner$new(lrn("classif.rpart"), rsmp("holdout"), ms, te, tuner = tuner, search_space)
at_2 = AutoTuner$new(lrn("classif.rpart"), rsmp("holdout"), ms, te, tuner = tuner, search_space)
at_1 = AutoTuner$new(lrn("classif.rpart"), rsmp("holdout"), ms, te, tuner = tuner, search_space, id = "at_1")
at_2 = AutoTuner$new(lrn("classif.rpart"), rsmp("holdout"), ms, te, tuner = tuner, search_space, id = "at_2")
resampling_outer = rsmp("cv", folds = 2)
grid = benchmark_grid(task, list(at_1, at_2), resampling_outer)
bmr = benchmark(grid, store_models = TRUE)
Expand All @@ -36,8 +36,8 @@ test_that("extract_inner_tuning_archives function works", {
expect_equal(unique(ibmr$experiment), c(1, 2))

# repeated cv
at_1 = AutoTuner$new(lrn("classif.rpart"), rsmp("holdout"), ms, te, tuner = tuner, search_space)
at_2 = AutoTuner$new(lrn("classif.rpart"), rsmp("holdout"), ms, te, tuner = tuner, search_space)
at_1 = AutoTuner$new(lrn("classif.rpart"), rsmp("holdout"), ms, te, tuner = tuner, search_space, id = "at_1")
at_2 = AutoTuner$new(lrn("classif.rpart"), rsmp("holdout"), ms, te, tuner = tuner, search_space, id = "at_2")
resampling_outer = rsmp("repeated_cv", folds = 2, repeats = 3)
grid = benchmark_grid(task, list(at_1, at_2), resampling_outer)
bmr = benchmark(grid, store_models = TRUE)
Expand Down Expand Up @@ -75,8 +75,8 @@ test_that("extract_inner_tuning_archives function works", {
expect_data_table(extract_inner_tuning_archives(rr), nrows = 0, ncols = 0)

# no models
at_1 = AutoTuner$new(lrn("classif.rpart"), rsmp("holdout"), ms, te, tuner = tuner, search_space)
at_2 = AutoTuner$new(lrn("classif.rpart"), rsmp("holdout"), ms, te, tuner = tuner, search_space)
at_1 = AutoTuner$new(lrn("classif.rpart"), rsmp("holdout"), ms, te, tuner = tuner, search_space, id = "at_1")
at_2 = AutoTuner$new(lrn("classif.rpart"), rsmp("holdout"), ms, te, tuner = tuner, search_space, id = "at_2")
resampling_outer = rsmp("cv", folds = 2)
grid = benchmark_grid(task, list(at_1, at_2), resampling_outer)
bmr = benchmark(grid, store_models = FALSE)
Expand Down
30 changes: 21 additions & 9 deletions tests/testthat/test_extract_inner_tuning_results.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ test_that("extract_inner_tuning_results function works", {
expect_named(irr, c("iteration", "cp", "classif.ce", "learner_param_vals", "x_domain", "task_id", "learner_id", "resampling_id"))

# cv
at_1 = AutoTuner$new(lrn("classif.rpart"), rsmp("holdout"), ms, te, tuner = tuner, search_space)
at_2 = AutoTuner$new(lrn("classif.rpart"), rsmp("holdout"), ms, te, tuner = tuner, search_space)
at_1 = AutoTuner$new(lrn("classif.rpart"), rsmp("holdout"), ms, te, tuner = tuner, search_space, id = "at_1")
at_2 = AutoTuner$new(lrn("classif.rpart"), rsmp("holdout"), ms, te, tuner = tuner, search_space, id = "at_2")
resampling_outer = rsmp("cv", folds = 2)
grid = benchmark_grid(task, list(at_1, at_2), resampling_outer)
bmr = benchmark(grid, store_models = TRUE)
Expand All @@ -36,8 +36,8 @@ test_that("extract_inner_tuning_results function works", {
expect_equal(unique(ibmr$experiment), c(1, 2))

# repeated cv
at_1 = AutoTuner$new(lrn("classif.rpart"), rsmp("holdout"), ms, te, tuner = tuner, search_space)
at_2 = AutoTuner$new(lrn("classif.rpart"), rsmp("holdout"), ms, te, tuner = tuner, search_space)
at_1 = AutoTuner$new(lrn("classif.rpart"), rsmp("holdout"), ms, te, tuner = tuner, search_space, id = "at_1")
at_2 = AutoTuner$new(lrn("classif.rpart"), rsmp("holdout"), ms, te, tuner = tuner, search_space, id = "at_2")
resampling_outer = rsmp("repeated_cv", folds = 2, repeats = 3)
grid = benchmark_grid(task, list(at_1, at_2), resampling_outer)
bmr = benchmark(grid, store_models = TRUE)
Expand Down Expand Up @@ -75,8 +75,8 @@ test_that("extract_inner_tuning_results function works", {
expect_data_table(extract_inner_tuning_results(rr), nrows = 0, ncols = 0)

# no models
at_1 = AutoTuner$new(lrn("classif.rpart"), rsmp("holdout"), ms, te, tuner = tuner, search_space)
at_2 = AutoTuner$new(lrn("classif.rpart"), rsmp("holdout"), ms, te, tuner = tuner, search_space)
at_1 = AutoTuner$new(lrn("classif.rpart"), rsmp("holdout"), ms, te, tuner = tuner, search_space, id = "at_1")
at_2 = AutoTuner$new(lrn("classif.rpart"), rsmp("holdout"), ms, te, tuner = tuner, search_space, id = "at_2")
resampling_outer = rsmp("cv", folds = 2)
grid = benchmark_grid(task, list(at_1, at_2), resampling_outer)
bmr = benchmark(grid, store_models = FALSE)
Expand Down Expand Up @@ -135,16 +135,28 @@ test_that("extract_inner_tuning_results returns tuning_instance", {
expect_named(tab, c("iteration", "cp", "classif.ce", "learner_param_vals", "x_domain", "tuning_instance", "task_id", "learner_id", "resampling_id"))
expect_class(tab$tuning_instance[[1]], "TuningInstanceBatchSingleCrit")

at = auto_tuner(
at_1 = auto_tuner(
tuner = tnr("random_search"),
learner = lrn("classif.rpart"),
resampling = rsmp("holdout"),
term_evals = 4,
search_space = TEST_MAKE_PS1(n_dim = 1),
store_tuning_instance = TRUE
store_tuning_instance = TRUE,
id = "at_1"
)

at_2 = auto_tuner(
tuner = tnr("random_search"),
learner = lrn("classif.rpart"),
resampling = rsmp("holdout"),
term_evals = 4,
search_space = TEST_MAKE_PS1(n_dim = 1),
store_tuning_instance = TRUE,
id = "at_2"
)

resampling_outer = rsmp("cv", folds = 2)
grid = benchmark_grid(tsk("iris"), list(at, at), resampling_outer)
grid = benchmark_grid(tsk("iris"), list(at_1, at_2), resampling_outer)
bmr = benchmark(grid, store_models = TRUE)

tab = extract_inner_tuning_results(bmr, tuning_instance = TRUE)
Expand Down

0 comments on commit 27e5fd6

Please sign in to comment.