Recently I follow some tutorials to learn how to use the GraphLearner in mlr3. But I am still confused about the tuning result of the GraphLearner with branch. I set up a simple example, Here is my codes:
# Create a Graph with Branch
graph_branch <-
po("branch", c("nop", "pca", "scale"), id = "preprocess_branch") %>>%
gunion(list(
po("nop"),
po("pca", id = "pca1"),
po("scale") %>>% po("pca", id = "pca2")
)) %>>%
po("unbranch", id = "preprocess_unbranch") %>>%
po("branch", c("classif.kknn", "classif.featureless"), id = "lrn_branch") %>>%
gunion(list(
lrn("classif.kknn", predict_type = "prob"),
lrn("classif.featureless", predict_type = "prob")
)) %>>%
po("unbranch", id = "lrn_unbranch")
# Convert a graph to a learner
graph_branch_lrn <- as_learner(graph_branch)
graph_branch_lrn$graph$plot()
# Set the tuning grid
tune_grid <- ParamSet$new(
list(
ParamFct$new("preprocess_branch.selection", levels = c("nop", "pca", "scale")),
ParamInt$new("pca1.rank.", lower = 1, upper = 10),
ParamInt$new("pca2.rank.", lower = 1, upper = 10),
ParamFct$new("lrn_branch.selection", levels = c("classif.kknn", "classif.featureless")),
ParamInt$new("classif.kknn.k", lower = 1, upper = 10)
))
# Set the instance
instance_rs <- TuningInstanceSingleCrit$new(
task = task_train,
learner = graph_branch_lrn,
resampling = rsmp("cv", folds = 5),
measure = msr("classif.auc"),
search_space = tune_grid,
terminator = trm("evals", n_evals = 20)
)
# Random search tuning
tuner_rs <- tnr("random_search")
plan(multisession, workers = 5)
set.seed(100)
tuner_rs$optimize(instance_rs)
plan(sequential)
The best tuning result is:
# Check the result
instance_rs$result_learner_param_vals
$preprocess_branch.selection
[1] "nop"
$scale.robust
[1] FALSE
$lrn_branch.selection
[1] "classif.kknn"
$classif.featureless.method
[1] "mode"
$pca1.rank.
[1] 9
$pca2.rank.
[1] 9
$classif.kknn.k
[1] 9
I want to know that if it chooses the "nop" branch, why the tuning results of "pca1.rank." and "pca2.rank." appear? I used to consider that the tuning of the GraphLearner with branch will choose the best result according to the branch, for example if the "nop" branch is chosen, the parameters in the other branches will not consider and appear in the process. Do I make something wrong in interpreting the mechanism of GraphLearner tuning or something wrong in the code?