mlr-org / mlr3tuning

Hyperparameter optimization package of the mlr3 ecosystem
https://mlr3tuning.mlr-org.com/
GNU Lesser General Public License v3.0
53 stars 5 forks source link

how to convert classes 'TuningSpace' to class 'ParamSet' #351

Closed ZhangDengwei closed 1 year ago

ZhangDengwei commented 1 year ago

Hi,

I would like to adopt the predefined search space from the package mlr3tuningspaces, but I have no idea how to implant it into the benchmarking module. My script is as follows:

require(mlr3verse)
require(mlr3tuning)
require(mlr3misc)
require(mlr3tuningspaces)

n_folds = 5
grid_search_resolution = 2
measure = msr("classif.acc")
task = tsk("iris")
inner_resampling = rsmp("cv", folds = n_folds)
terminator = trm("none")

## 1.  XGB: no Hyperparameter Tuning
xgb_no_tuning = lrn("classif.xgboost")
set_threads(xgb_no_tuning, n = 6)

## 2. XGB: AutoTuner
xgb_learner_tuning = lrn("classif.xgboost")
xgb_search_space = ps(nrounds = p_int(lower = 100, upper= 500),
                      max_depth = p_int(lower = 3, upper= 10),
                      colsample_bytree = p_dbl(lower = 0.6, upper = 1))
xgb_tuner = tnr("grid_search", resolution = grid_search_resolution)

set_threads(xgb_learner_tuning, n = 6)
xgb_tuned = AutoTuner$new(xgb_learner_tuning, inner_resampling, measure, terminator, xgb_tuner, xgb_search_space, store_tuning_instance = TRUE)

## 3. XGB: AutoTuner with the search space from mlr3tuningspace
as.data.table(mlr_tuning_spaces)
tuning_space = lts("classif.xgboost.default")
learner_search_space = tuning_space
xgb_tuned_2 = AutoTuner$new(xgb_learner_tuning, inner_resampling, measure, terminator, xgb_tuner, learner_search_space, store_tuning_instance = TRUE)

# benchmark
outer_resampling = rsmp("holdout")
outer_resampling$instantiate(task)

bm_design = benchmark_grid(
  tasks = task,
  learners = c(xgb_no_tuning,
               xgb_tuned,
               xgb_tuned_2),
  resamplings = outer_resampling
)

By this, I will receive an error, as follows, because the class TuningSpace from mlr3tuningspaces cannot directly be used.

Error in assert_param_set(search_space) : 
  Assertion on 'param_set' failed: Must inherit from class 'ParamSet', but has classes 'TuningSpace','R6'.

I have no idea how to pass the class TuningSpace to class ParamSet. Any suggestion would be greatly appreciated!

be-marc commented 1 year ago

Sorry for the late reply (holiday). There is a bug. It should be possible to set a tuning space as the search space. You can use a workaround for now:

xgb_tuned_2 = AutoTuner$new(lts(xgb_learner_tuning), inner_resampling, measure, terminator, xgb_tuner, store_tuning_instance = TRUE)

So wrap the learner with lts(). This applies the search space to the learner directly. And don't pass a search space in AutoTuner$new().

be-marc commented 1 year ago

Works in the dev version now. Solved by #352.