使用 tidymodels 调整工作流集时如何正确设置参数网格?
How to set the parameters grids correctly when tuning the workflowset with tidymodels?
我尝试使用 tidymodels 通过配方和模型参数调整工作流程。调整单个工作流时没有问题。但是,在调整具有多个工作流的工作流集时,它总是会失败。这是我的代码:
# read the training data
train <- read_csv("../../train.csv")
train <- train %>%
mutate(
id = row_number(),
across(where(is.double), as.integer),
across(where(is.character), as.factor),
r_yn = fct_relevel(r_yn, "yes")) %>%
select(id, r_yn, everything())
# setting the recipes
# no precess
rec_no <- recipe(r_yn ~ ., data = train) %>%
update_role(id, new_role = "ID")
# downsample: tuning the under_ratio
rec_ds_tune <- rec_no %>%
step_downsample(r_yn, under_ratio = tune(), skip = TRUE, seed = 100) %>%
step_nzv(all_predictors(), freq_cut = 100)
# setting the models
# randomforest
spec_rf_tune <- rand_forest(trees = 100, mtry = tune(), min_n = tune()) %>%
set_engine("ranger", seed = 100) %>%
set_mode("classification")
# xgboost
spec_xgb_tune <- boost_tree(trees = 100, mtry = tune(), tree_depth = tune(), learn_rate = tune(), min_n = tune()) %>%
set_engine("xgboost") %>%
set_mode("classification")
# setting the workflowsets
wf_tune_list <- workflow_set(
preproc = list(no = rec_no, ds = rec_ds_tune),
models = list(rf = spec_rf_tune, xgb = spec_xgb_tune),
cross = TRUE)
# finalize the parameters, I'm not sure it is correct or not
rf_params <- spec_rf_tune %>% parameters() %>% update(mtry = mtry(c(1, 15)))
xgb_params <- spec_xgb_tune %>% parameters() %>% update(mtry = mtry(c(1, 15)))
ds_params <- rec_ds_tune %>% parameters() %>% update(under_ratio = under_ratio(c(1, 5)))
wf_tune_list_finalize <- wf_tune_list %>%
option_add(param = ds_params, id = c("ds_rf", "ds_xgb")) %>%
option_add(param = rf_params, id = c("no_rf", "ds_rf")) %>%
option_add(param = xgb_params, id = c("no_xgb", "ds_xgb"))
我检查了 wf_tune_list_finalize 中的 选项 它显示:
> wf_tune_list_finalize$option
[[1]]
a list of options with names: 'param'
[[2]]
a list of options with names: 'param'
[[3]]
a list of options with names: 'param'
[[4]]
a list of options with names: 'param'
然后我调整这个工作流集:
# tuning the workflowset
cl <- makeCluster(detectCores())
registerDoParallel(cl)
wf_tune_race <- wf_tune_list_finalize %>%
workflow_map(fn = "tune_race_anova",
seed = 100,
resamples = cv_5,
grid = 3,
metrics = metric_auc,
control = control_race(parallel_over = "everything"),
verbose = TRUE)
stopCluster(cl)
详细消息表明我在工作流 ds_rf 和 ds_xgb[=41= 中的参数有问题]:
i 1 of 4 tuning: no_rf
i Creating pre-processing data to finalize unknown parameter: mtry
�� 1 of 4 tuning: no_rf (1m 44.4s)
i 2 of 4 tuning: no_xgb
i Creating pre-processing data to finalize unknown parameter: mtry
�� 2 of 4 tuning: no_xgb (28.9s)
i 3 of 4 tuning: ds_rf
x 3 of 4 tuning: ds_rf failed with: Some tuning parameters require finalization but there are recipe parameters that require tuning. Please use `parameters()` to finalize the parameter ranges.
i 4 of 4 tuning: ds_xgb
x 4 of 4 tuning: ds_xgb failed with: Some tuning parameters require finalization but there are recipe parameters that require tuning. Please use `parameters()` to finalize the parameter ranges.
结果是:
> wf_tune_race
# A workflow set/tibble: 4 x 4
wflow_id info option result
<chr> <list> <list> <list>
1 no_rf <tibble [1 x 4]> <wrkflw__ > <race[+]>
2 no_xgb <tibble [1 x 4]> <wrkflw__ > <race[+]>
3 ds_rf <tibble [1 x 4]> <wrkflw__ > <try-errr [1]>
4 ds_xgb <tibble [1 x 4]> <wrkflw__ > <try-errr [1]>
另外,no_rf和no_xgb虽然有调优结果,但我发现范围这两个工作流程中的mtry不是我上面设置的范围,也就是说参数范围设置步骤完全失败。我已经按照 https://www.tmwr.org/workflow-sets.html and https://workflowsets.tidymodels.org/ 的教程进行操作,但仍然没有任何想法。
那么在调整工作流集时如何正确设置配方和模型参数呢?
我代码中的train.csv在这里:https://github.com/liuyifeikim/Some-data
修改了参数设置步骤,现在调优结果正确:
# setting the parameters on each workflow seperately
no_rf_params <- wf_set_tune_list %>%
extract_workflow("no_rf") %>%
parameters() %>%
update(mtry = mtry(c(1, 15)))
no_xgb_params <- wf_set_tune_list %>%
extract_workflow("no_xgb") %>%
parameters() %>%
update(mtry = mtry(c(1, 15)))
ds_rf_params <- wf_set_tune_list %>%
extract_workflow("ds_rf") %>%
parameters() %>%
update(mtry = mtry(c(1, 15)), under_ratio = under_ratio(c(1, 5)))
ds_xgb_params <- wf_set_tune_list %>%
extract_workflow("ds_xgb") %>%
parameters() %>%
update(mtry = mtry(c(1, 15)), under_ratio = under_ratio(c(1, 5)))
# update the workflowset
wf_set_tune_list_finalize <- wf_set_tune_list %>%
option_add(param_info = no_rf_params, id = "no_rf") %>%
option_add(param_info = no_xgb_params, id = "no_xgb") %>%
option_add(param_info = ds_rf_params, id = "ds_rf") %>%
option_add(param_info = ds_xgb_params, id = "ds_xgb")
其余不变。我认为可能有一些有效的方法来设置参数。
我尝试使用 tidymodels 通过配方和模型参数调整工作流程。调整单个工作流时没有问题。但是,在调整具有多个工作流的工作流集时,它总是会失败。这是我的代码:
# read the training data
train <- read_csv("../../train.csv")
train <- train %>%
mutate(
id = row_number(),
across(where(is.double), as.integer),
across(where(is.character), as.factor),
r_yn = fct_relevel(r_yn, "yes")) %>%
select(id, r_yn, everything())
# setting the recipes
# no precess
rec_no <- recipe(r_yn ~ ., data = train) %>%
update_role(id, new_role = "ID")
# downsample: tuning the under_ratio
rec_ds_tune <- rec_no %>%
step_downsample(r_yn, under_ratio = tune(), skip = TRUE, seed = 100) %>%
step_nzv(all_predictors(), freq_cut = 100)
# setting the models
# randomforest
spec_rf_tune <- rand_forest(trees = 100, mtry = tune(), min_n = tune()) %>%
set_engine("ranger", seed = 100) %>%
set_mode("classification")
# xgboost
spec_xgb_tune <- boost_tree(trees = 100, mtry = tune(), tree_depth = tune(), learn_rate = tune(), min_n = tune()) %>%
set_engine("xgboost") %>%
set_mode("classification")
# setting the workflowsets
wf_tune_list <- workflow_set(
preproc = list(no = rec_no, ds = rec_ds_tune),
models = list(rf = spec_rf_tune, xgb = spec_xgb_tune),
cross = TRUE)
# finalize the parameters, I'm not sure it is correct or not
rf_params <- spec_rf_tune %>% parameters() %>% update(mtry = mtry(c(1, 15)))
xgb_params <- spec_xgb_tune %>% parameters() %>% update(mtry = mtry(c(1, 15)))
ds_params <- rec_ds_tune %>% parameters() %>% update(under_ratio = under_ratio(c(1, 5)))
wf_tune_list_finalize <- wf_tune_list %>%
option_add(param = ds_params, id = c("ds_rf", "ds_xgb")) %>%
option_add(param = rf_params, id = c("no_rf", "ds_rf")) %>%
option_add(param = xgb_params, id = c("no_xgb", "ds_xgb"))
我检查了 wf_tune_list_finalize 中的 选项 它显示:
> wf_tune_list_finalize$option
[[1]]
a list of options with names: 'param'
[[2]]
a list of options with names: 'param'
[[3]]
a list of options with names: 'param'
[[4]]
a list of options with names: 'param'
然后我调整这个工作流集:
# tuning the workflowset
cl <- makeCluster(detectCores())
registerDoParallel(cl)
wf_tune_race <- wf_tune_list_finalize %>%
workflow_map(fn = "tune_race_anova",
seed = 100,
resamples = cv_5,
grid = 3,
metrics = metric_auc,
control = control_race(parallel_over = "everything"),
verbose = TRUE)
stopCluster(cl)
详细消息表明我在工作流 ds_rf 和 ds_xgb[=41= 中的参数有问题]:
i 1 of 4 tuning: no_rf
i Creating pre-processing data to finalize unknown parameter: mtry
�� 1 of 4 tuning: no_rf (1m 44.4s)
i 2 of 4 tuning: no_xgb
i Creating pre-processing data to finalize unknown parameter: mtry
�� 2 of 4 tuning: no_xgb (28.9s)
i 3 of 4 tuning: ds_rf
x 3 of 4 tuning: ds_rf failed with: Some tuning parameters require finalization but there are recipe parameters that require tuning. Please use `parameters()` to finalize the parameter ranges.
i 4 of 4 tuning: ds_xgb
x 4 of 4 tuning: ds_xgb failed with: Some tuning parameters require finalization but there are recipe parameters that require tuning. Please use `parameters()` to finalize the parameter ranges.
结果是:
> wf_tune_race
# A workflow set/tibble: 4 x 4
wflow_id info option result
<chr> <list> <list> <list>
1 no_rf <tibble [1 x 4]> <wrkflw__ > <race[+]>
2 no_xgb <tibble [1 x 4]> <wrkflw__ > <race[+]>
3 ds_rf <tibble [1 x 4]> <wrkflw__ > <try-errr [1]>
4 ds_xgb <tibble [1 x 4]> <wrkflw__ > <try-errr [1]>
另外,no_rf和no_xgb虽然有调优结果,但我发现范围这两个工作流程中的mtry不是我上面设置的范围,也就是说参数范围设置步骤完全失败。我已经按照 https://www.tmwr.org/workflow-sets.html and https://workflowsets.tidymodels.org/ 的教程进行操作,但仍然没有任何想法。
那么在调整工作流集时如何正确设置配方和模型参数呢?
我代码中的train.csv在这里:https://github.com/liuyifeikim/Some-data
修改了参数设置步骤,现在调优结果正确:
# setting the parameters on each workflow seperately
no_rf_params <- wf_set_tune_list %>%
extract_workflow("no_rf") %>%
parameters() %>%
update(mtry = mtry(c(1, 15)))
no_xgb_params <- wf_set_tune_list %>%
extract_workflow("no_xgb") %>%
parameters() %>%
update(mtry = mtry(c(1, 15)))
ds_rf_params <- wf_set_tune_list %>%
extract_workflow("ds_rf") %>%
parameters() %>%
update(mtry = mtry(c(1, 15)), under_ratio = under_ratio(c(1, 5)))
ds_xgb_params <- wf_set_tune_list %>%
extract_workflow("ds_xgb") %>%
parameters() %>%
update(mtry = mtry(c(1, 15)), under_ratio = under_ratio(c(1, 5)))
# update the workflowset
wf_set_tune_list_finalize <- wf_set_tune_list %>%
option_add(param_info = no_rf_params, id = "no_rf") %>%
option_add(param_info = no_xgb_params, id = "no_xgb") %>%
option_add(param_info = ds_rf_params, id = "ds_rf") %>%
option_add(param_info = ds_xgb_params, id = "ds_xgb")
其余不变。我认为可能有一些有效的方法来设置参数。