tidymodels / tune

Tools for tidy parameter tuning
https://tune.tidymodels.org
Other
277 stars 42 forks source link

tune_grid failed tuning when set plan(multisession) #477

Closed wtbxsjy closed 2 years ago

wtbxsjy commented 2 years ago

Hi, I am using recipeselectors for feature selection to speed up the process, I tried doFuture, tune_grid worded fine with plan(sequential), but failed when set plan(multisession) could you help to solve it? here is an example from https://github.com/stevenpawley/recipeselectors/issues/1

library(tidyverse)
library(tidymodels)
#> Warning: 程辑包'workflowsets'是用R版本4.1.3 来建造的
library(recipeselectors)
data(cells, package = "modeldata")
cells <- cells %>% select(-case)
rec <-
    recipe(class ~ ., data = cells) %>%
    step_corr(all_predictors(), threshold = 0.9) %>% 
    step_select_roc(all_predictors(), outcome = "class", top_p = tune())
mod <- logistic_reg(
    penalty = 0.01,
    mixture = 1
) %>%
    set_engine("LiblineaR") %>%
    set_mode("classification")
wflow <- 
    workflow() %>% 
    add_recipe(rec) %>% 
    add_model(mod)
p_info <- 
    wflow %>% 
    extract_parameter_set_dials() %>% 
    update(top_p = top_p(c(1, 30)))
rs <- vfold_cv(cells)
ctrl <- control_grid(extract = identity)

library(doFuture)
registerDoFuture()
plan(sequential)
# use sequential, succeed
rfe_res <-
    mod %>% 
    tune_grid(
        rec,
        resamples = rs,
        grid = 20,
        param_info = p_info,
        control = ctrl
    )

plan(multisession)
# use multisession, failed
rfe_res <-
    mod %>% 
    tune_grid(
        rec,
        resamples = rs,
        grid = 20,
        param_info = p_info,
        control = ctrl
    )
# x Fold01: preprocessor 1/19: Error in `recipes::prep()`:
# ! You cannot `prep()` a tuneable recipe. Argument...
# x Fold01: preprocessor 2/19: Error in `recipes::prep()`:
# ! You cannot `prep()` a tuneable recipe. Argument...
# more errors...
# full note. is following:
# "Error in `recipes::prep()`:
# ! You cannot `prep()` a tuneable recipe. Argument(s) with `tune()`: 'top_p'. 
# Do you want to use a tuning function such as `tune_grid()`?"

here is my packages versions:

library(tidymodels)
-- Attaching packages -------------------------------------------------------------- tidymodels 0.2.0 --
√ broom        0.7.12     √ recipes      0.2.0 
√ dials        0.1.0      √ rsample      0.1.1 
√ dplyr        1.0.8      √ tibble       3.1.6 
√ ggplot2      3.3.5      √ tidyr        1.2.0 
√ infer        1.0.0      √ tune         0.2.0 
√ modeldata    0.1.1      √ workflows    0.2.6 
√ parsnip      0.2.1      √ workflowsets 0.2.1 
√ purrr        0.3.4      √ yardstick    0.0.9 
packageVersion('doFuture')
[1] ‘0.12.0’

Created on 2022-03-22 by the reprex package (v2.0.1)

DavisVaughan commented 2 years ago

Due to how future looks up global variables, you need to manually specify that the {recipeselectors} package is needed on the workers. You can do this with control_grid(pkgs = "recipeselectors").

library(tidyverse)
library(tidymodels)
library(recipeselectors)

data(cells, package = "modeldata")
cells <- cells %>% select(-case)

rec <-
  recipe(class ~ ., data = cells) %>%
  step_corr(all_predictors(), threshold = 0.9) %>% 
  step_select_roc(all_predictors(), outcome = "class", top_p = tune())

mod <- logistic_reg(
  penalty = 0.01,
  mixture = 1
) %>%
  set_engine("LiblineaR") %>%
  set_mode("classification")

wflow <- 
  workflow() %>% 
  add_recipe(rec) %>% 
  add_model(mod)

p_info <- 
  wflow %>% 
  extract_parameter_set_dials() %>% 
  update(top_p = top_p(c(1, 30)))

rs <- vfold_cv(cells)

# pass in pkgs here
ctrl <- control_grid(extract = identity, pkgs = "recipeselectors")

library(doFuture)
registerDoFuture()

plan(multisession)

# works
rfe_res <-
  mod %>% 
  tune_grid(
    rec,
    resamples = rs,
    grid = 2,
    param_info = p_info,
    control = ctrl
  )

Created on 2022-03-22 by the reprex package (v2.0.1)

wtbxsjy commented 2 years ago

Thanks for your kindly reply, it works.

github-actions[bot] commented 2 years ago

This issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with a reprex: https://reprex.tidyverse.org) and link to this issue.