First I'd like to share my appreciation about mlr3.
As a mlr user I was a bit skeptical about mlr3 since the whole R6 class thing was a nuisance that I had to learn in order to transition effectively.
After a few days of playing with mlr3 I must say I love it.
To the point.
I was very fond of mlr::makeModelMultiplexer
and I could not find tutorials which would explain how to recreate it within mlr3 so I decided to create one. It looks like the code does what I intended, so I figured it might help other mlr3 users. Anyway the tutorial is below, if you like it and think it might be beneficial to mlr3 users you have my permission to modify it and share it any way you deem fit.
An example of tuning over whole model spaces with mlr3.
This example consists of tuning the filter method and the number of features selected, as well as the type of learner along with its hyper parameters
library(mlr3)
library(mlr3pipelines)
library(mlr3learners)
library(mlr3tuning)
library(mlr3filters)
library(visNetwork)
library(paradox)
define filters to be used
filt_null <- mlr_pipeops$get("nop")
filt_ig <- mlr_pipeops$get("filter",
mlr_filters$get("information_gain"))
filt_mrmr <- mlr_pipeops$get("filter",
mlr_filters$get("mrmr"))
filt_names <- c("nop",
"information_gain",
"mrmr")
create a branch to these filters
graph <- mlr_pipeops$get("branch", filt_names, id = "branch1") %>>%
gunion(list(
filt_null,
filt_ig,
filt_mrmr
))
unbranch
graph <- graph %>>%
mlr_pipeops$get("unbranch",
filt_names,
id = "unbranch1")
define learners to be used
rpart_lrn <- mlr_pipeops$get("learner",
learner = mlr_learners$get("classif.rpart"))
rpart_lrn$learner$predict_type <- "prob"
ranger_lrn <- mlr_pipeops$get("learner",
learner = mlr_learners$get("classif.ranger"))
ranger_lrn $learner$predict_type <- "prob"
lrn_names <- c("classif.rpart",
"classif.ranger")
add learners to the graph
graph <- graph %>>%
mlr_pipeops$get("branch", lrn_names, id = "branch2") %>>%
gunion(list(
rpart_lrn,
ranger_lrn)) %>>%
mlr_pipeops$get("unbranch", lrn_names, id = "unbranch2")
how does it look?
graph$plot(html = TRUE)
create a parameter set of all the things we would like tuned
ps <- ParamSet$new(list(
ParamDbl$new("classif.rpart.cp", lower = 0, upper = 0.05),
ParamInt$new("classif.ranger.mtry", lower = 1L, upper = 20L),
ParamFct$new("classif.ranger.splitrule", levels = c("extratrees",
"gini")),
ParamDbl$new("classif.ranger.sample.fraction", lower = 0.3, upper = 1),
ParamInt$new("classif.ranger.num.trees", lower = 100L, upper = 2000L),
ParamInt$new("classif.ranger.num.random.splits", lower = 1L, upper = 20L),
ParamInt$new("information_gain.filter.nfeat", lower = 20L, upper = 60L),
ParamFct$new("information_gain.type", levels = c("infogain", "symuncert")),
ParamInt$new("mrmr.filter.nfeat", lower = 20L, upper = 60L),
ParamFct$new("branch1.selection", levels = filt_names),
ParamFct$new("branch2.selection", levels = lrn_names )
))
define dependencies in the parameter set
ps$add_dep("classif.rpart.cp",
"branch2.selection", CondEqual$new("classif.rpart"))
ps$add_dep("classif.ranger.mtry",
"branch2.selection", CondEqual$new("classif.ranger"))
ps$add_dep("classif.ranger.splitrule",
"branch2.selection", CondEqual$new("classif.ranger"))
ps$add_dep("classif.ranger.sample.fraction",
"branch2.selection", CondEqual$new("classif.ranger"))
ps$add_dep("classif.ranger.num.trees",
"branch2.selection", CondEqual$new("classif.ranger"))
ps$add_dep("classif.ranger.num.random.splits",
"classif.ranger.splitrule", CondEqual$new("extratrees"))
ps$add_dep("information_gain.filter.nfeat",
"branch1.selection", CondEqual$new("information_gain"))
ps$add_dep("information_gain.type",
"branch1.selection", CondEqual$new("information_gain"))
ps$add_dep("mrmr.filter.nfeat",
"branch1.selection", CondEqual$new("mrmr"))
convert graph to learner and define resampling, measures and random search
glrn <- GraphLearner$new(graph)
glrn$predict_type <- "prob"
cv5 <- rsmp("cv", folds = 5)
tsk <- mlr_tasks$get("sonar")
stratified sampling based on target
tsk$col_roles$stratum <- tsk$target_names
create instance and tune
instance <- TuningInstance$new(
task = tsk,
learner = glrn,
resampling = cv5,
measures = msr("classif.auc"),
param_set = ps,
terminator = term("evals", n_evals = 20)
)
tuner <- TunerRandomSearch$new()
tuner$tune(instance)
instance$result
Kind regards,
Milan