Kim*_*m.L 6 workflow r machine-learning r-recipes tidymodels
我尝试使用 tidymodels 通过配方和模型参数来调整工作流程。调整单个工作流程时没有问题。但是,当调整具有多个工作流程的工作流程集时,它总是会失败。这是我的代码:
\n# read the training data\ntrain <- read_csv("../../train.csv")\ntrain <- train %>% \n mutate(\n id = row_number(),\n across(where(is.double), as.integer),\n across(where(is.character), as.factor),\n r_yn = fct_relevel(r_yn, "yes")) %>% \n select(id, r_yn, everything())\n\n# setting the recipes\n\n# no precess\nrec_no <- recipe(r_yn ~ ., data = train) %>%\n update_role(id, new_role = "ID")\n\n# downsample: tuning the under_ratio\nrec_ds_tune <- rec_no %>% \n step_downsample(r_yn, under_ratio = tune(), skip = TRUE, seed = 100) %>%\n step_nzv(all_predictors(), freq_cut = 100)\n\n# setting the models\n\n# randomforest\nspec_rf_tune <- rand_forest(trees = 100, mtry = tune(), min_n = tune()) %>%\n set_engine("ranger", seed = 100) %>%\n set_mode("classification")\n\n# xgboost\nspec_xgb_tune <- boost_tree(trees = 100, mtry = tune(), tree_depth = tune(), learn_rate = tune(), min_n = tune()) %>% \n set_engine("xgboost") %>% \n set_mode("classification")\n\n# setting the workflowsets\nwf_tune_list <- workflow_set(\n preproc = list(no = rec_no, ds = rec_ds_tune),\n models = list(rf = spec_rf_tune, xgb = spec_xgb_tune),\n cross = TRUE)\n\n# finalize the parameters, I\'m not sure it is correct or not\nrf_params <- spec_rf_tune %>% parameters() %>% update(mtry = mtry(c(1, 15)))\nxgb_params <- spec_xgb_tune %>% parameters() %>% update(mtry = mtry(c(1, 15)))\nds_params <- rec_ds_tune %>% parameters() %>% update(under_ratio = under_ratio(c(1, 5)))\n\nwf_tune_list_finalize <- wf_tune_list %>% \n option_add(param = ds_params, id = c("ds_rf", "ds_xgb")) %>% \n option_add(param = rf_params, id = c("no_rf", "ds_rf")) %>% \n option_add(param = xgb_params, id = c("no_xgb", "ds_xgb"))\nRun Code Online (Sandbox Code Playgroud)\n我检查了wf_tune_list_finalize中的选项,它显示:
\n> wf_tune_list_finalize$option\n[[1]]\na list of options with names: \'param\'\n\n[[2]]\na list of options with names: \'param\'\n\n[[3]]\na list of options with names: \'param\'\n\n[[4]]\na list of options with names: \'param\'\nRun Code Online (Sandbox Code Playgroud)\n然后我调整这个工作流程集:
\n# tuning the workflowset\ncl <- makeCluster(detectCores())\nregisterDoParallel(cl)\nwf_tune_race <- wf_tune_list_finalize %>%\n workflow_map(fn = "tune_race_anova",\n seed = 100,\n resamples = cv_5,\n grid = 3,\n metrics = metric_auc,\n control = control_race(parallel_over = "everything"), \n verbose = TRUE)\nstopCluster(cl)\nRun Code Online (Sandbox Code Playgroud)\n详细消息表明工作流程ds_rf和ds_xgb中的参数有问题:
\ni 1 of 4 tuning: no_rf\ni Creating pre-processing data to finalize unknown parameter: mtry\n\xef\xbf\xbd\xef\xbf\xbd 1 of 4 tuning: no_rf (1m 44.4s)\ni 2 of 4 tuning: no_xgb\ni Creating pre-processing data to finalize unknown parameter: mtry\n\xef\xbf\xbd\xef\xbf\xbd 2 of 4 tuning: no_xgb (28.9s)\ni 3 of 4 tuning: ds_rf\nx 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.\ni 4 of 4 tuning: ds_xgb\nx 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.\nRun Code Online (Sandbox Code Playgroud)\n结果是:
\n> wf_tune_race\n# A workflow set/tibble: 4 x 4\n wflow_id info option result \n <chr> <list> <list> <list> \n1 no_rf <tibble [1 x 4]> <wrkflw__ > <race[+]> \n2 no_xgb <tibble [1 x 4]> <wrkflw__ > <race[+]> \n3 ds_rf <tibble [1 x 4]> <wrkflw__ > <try-errr [1]>\n4 ds_xgb <tibble [1 x 4]> <wrkflw__ > <try-errr [1]>\nRun Code Online (Sandbox Code Playgroud)\n另外,虽然no_rf和no_xgb都有调优结果,但我发现这两个工作流程中mtry的范围不是我上面设置的范围,这意味着参数范围设置步骤完全失败。我已经按照https://www.tmwr.org/workflow-sets.html和https://workflowsets.tidymodels.org/的教程进行操作,但仍然没有任何想法。
\n那么在调整工作流程集时如何正确设置配方和模型参数呢?
\n我的代码中的train.csv在这里:https ://github.com/liuyifeikim/Some-data
\n我修改了参数设置步骤,现在调整结果是正确的:
# 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")
Run Code Online (Sandbox Code Playgroud)
其余部分保持不变。我认为可能有一些有效的方法来设置参数。
| 归档时间: |
|
| 查看次数: |
1306 次 |
| 最近记录: |