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

Depends and extra trafo #413

Open
MislavSag opened this issue Sep 21, 2024 · 1 comment
Open

Depends and extra trafo #413

MislavSag opened this issue Sep 21, 2024 · 1 comment

Comments

@MislavSag
Copy link

I am confused about paradox behaviour with dependency and extra_trafo.

Here is my reprex:

library(mlr3verse)

# Graph
pca_banch = po("branch",
               options = c("nop_pca", "pca"),
               id = "pca_branch") %>>%
  gunion(list(
    po("nop", id = "nop_pca"),
    po("pca", id = "pca_id")
  )) %>>%
  po("unbranch", id = "pca_unbranch")
graph = pca_banch %>>% 
  po("filter", filter = mlr3filters::flt("variance"), filter.frac = 0.5) %>>% 
  po("learner", learner = lrn("classif.rpart"))
plot(graph)
graph_lrn = as_learner(graph)

# search space
graph$param_set
search_space = ps(
  pca_branch.selection = p_fct(c("nop_pca", "pca")),
  pca_id.scale = p_fct(c("1", "2"), depends = pca_branch.selection == "pca"),
  .extra_trafo = function(x, param_set) {
    if (x$pca_id.scale == "1") {
      x$pca_id.scale = TRUE
    } else if (x$pca_id.scale == "2") {
      x$pca_id.scale = FALSE
    }
    return(x)
  }
)

# task
mlr_tasks
task = tsk("iris")

# tune
tuner = auto_tuner(
  tuner = tnr("grid_search"),
  learner = graph_lrn,
  resampling = rsmp("holdout"),
  measure = msr("classif.acc"),
  search_space = search_space,
  terminator = trm("none")
  # term_evals = term_evals
)
x = tuner$train(task)

This returns an error

Error in if (x$pca_id.scale == "1") { : argument is of length zero

Now, it seems to me the reason fro the error is that in case search space chosees non pca there is no pca scale parameter availabel. IS that TRUE? Should I always check for NULL too?

@mb706
Copy link
Contributor

mb706 commented Sep 23, 2024

in case search space chosees non pca there is no pca scale parameter availabel.

This is exactly what dependencies do: if the dependency is not fulfilled, the entry in the generated configuration list is NULL.

Should I always check for NULL too?

In .extra_trafo, you should anticipate the possibility that an entry with a dependency is NULL. You don't need to check if there is no dependency.

By the way: When you are only changing values for individual parameters (without any interactions with other values), you can use the trafo of the individual Domain object (i.e. p_fct() here). This will respect the depends with no need to check for NULL:

search_space = ps(
  pca_branch.selection = p_fct(c("nop_pca", "pca")),
  pca_id.scale = p_fct(c("1", "2"), depends = pca_branch.selection == "pca",
    trafo = function(x) if (x == "1") TRUE else FALSE)
)

By the way x2 combo: If all you want to do is get specific discrete values from a finite set, you can use p_fct() with a named list of values -- it constructs this trafo for you:

search_space = ps(
  pca_branch.selection = p_fct(c("nop_pca", "pca")),
  pca_id.scale = p_fct(list(`1` = TRUE, `2` = FALSE), depends = pca_branch.selection == "pca")
)

For small ParamSets, you can use generate_design_grid to look at the values this generates. Note the grid without $transpose() are not transformed, these are the pre-trafo values that will be in the archive as well. The $transpose() values are the ones that are given to the Learner by the Tuner.

generate_design_grid(search_space, 2)
#> <Design> with 3 rows:
#>    pca_branch.selection pca_id.scale
#>                  <char>       <char>
#> 1:              nop_pca         <NA>
#> 2:                  pca            1
#> 3:                  pca            2
generate_design_grid(search_space, 2)$transpose()
#> [[1]]
#> [[1]]$pca_branch.selection
#> [1] "nop_pca"
#>
#>
#> [[2]]
#> [[2]]$pca_branch.selection
#> [1] "pca"
#>
#> [[2]]$pca_id.scale
#> [1] TRUE
#>
#>
#> [[3]]
#> [[3]]$pca_branch.selection
#> [1] "pca"
#>
#> [[3]]$pca_id.scale
#> [1] FALSE

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants