Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Number of columns for task in trafo #309

Closed
MislavSag opened this issue Sep 10, 2021 · 3 comments
Closed

Number of columns for task in trafo #309

MislavSag opened this issue Sep 10, 2021 · 3 comments

Comments

@MislavSag
Copy link

Hi,

I am using transformation (trafo) method for my ranger learner. The problem is that I need the number of features (columns) in task to transform my param_set. For example:

  # random forest
  learner = lrn("classif.ranger", predict_type = "prob", predict_sets = c("train", "test"))
  learner_params = ParamSet$new(
    params = list(
      ParamInt$new("max.depth", lower = 2, upper = 10, tags = "ranger"),
      ParamDbl$new("mtry", lower = 0.1, upper = 0.9, default = 0.5)
    ))
  learner_params$trafo = function(x, param_set) { # This is slight modification (simplification) from mlr automl package
    if ("mtry" %in% names(x)) {
      proposed_mtry = as.integer(length(task$feature_names)^x[["mtry"]]) ### HREE THE PROBLEM ####
      x[["mtry"]] = max(1, proposed_mtry)
    }
    x
  }
  rf = make_auto_tuner(learner, learner_params)

On the line HERE THE PROBLEM I would like to add number of features. Now I use task$features_name, but this works only if I have one task. Is there any more clever way to set number of columns here.

@be-marc
Copy link
Member

be-marc commented Sep 17, 2021

Hey,
we have a working solution in mlr-org/paradox#323. So you need to install the expression_params branch with devtools::install_github("mlr-org/paradox@expression_params", force = TRUE). It seems we are not happy with the syntax and therefore did not merge the PR. You can still use it. Your example would look like this:

library(mlr3verse)
library(paradox) # @expression_params

learner = lrn("classif.ranger", predict_type = "prob",
  max.depth = to_tune(2, 10),
  mtry = to_tune(p_dbl(0.1, 0.9, 
    trafo = function(x) {
      ContextPV(function(task) {
        max(1, round(length(task$feature_names) * x))
      }, x)
    }
  ))
)

learner$param_set$context_available = "task" 

at = auto_tuner(
  method = "random_search",
  learner = learner,
  resampling = rsmp("holdout"),
  measure = msr("classif.ce"),
  term_evals = 100)

at$train(tsk("iris"))

I try to post here again when a working solution is merged to the main branch.

@MislavSag
Copy link
Author

I will to wait to be merged to the main branch, just for stability. Thanks!

@be-marc
Copy link
Member

be-marc commented Sep 29, 2021

We have another solution now. You can tune mtry.ratio for classif.ranger. See https://mlr3learners.mlr-org.com/reference/mlr_learners_classif.ranger.html.

@be-marc be-marc closed this as completed Sep 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants