我尝试使用workflowsets包或方法来获得错误。这是R代码(对不起,代码很长(:
# Package ----
library(finetune)
library(themis)
library(tidymodels)
# Data ----
data("PimaIndiansDiabetes", package = "mlbench")
table(PimaIndiansDiabetes$diabetes)
str(PimaIndiansDiabetes)
PimaIndiansDiabetes <-
PimaIndiansDiabetes %>%
mutate(diabetes = relevel(diabetes, "pos"))
# Split ----
set.seed(123)
ind <- initial_split(PimaIndiansDiabetes, strata = diabetes)
dat_train <- training(ind)
dat_test <- testing(ind)
# CV ----
set.seed(123)
dat_cv <- vfold_cv(dat_train, v = 10)
# Recipe ----
dat_rec <-
dat_train %>%
recipe(diabetes ~.) %>%
step_normalize(all_numeric_predictors()) %>%
step_smote(diabetes)
# Model ----
parsnip_nn <-
mlp(hidden_units = tune(),
penalty = tune(),
epochs = tune()) %>%
set_mode("classification") %>%
set_engine("nnet")
parsnip_log <-
logistic_reg(penalty = tune(),
mixture = tune()) %>%
set_engine("glmnet")
# Latin hypercube grid ----
latin_grid <-
grid_latin_hypercube(penalty(),
mixture(),
hidden_units(),
epochs(),
size = 30)
# Tuning ----
race_ctrl <-
control_race(
save_pred = T,
save_workflow = T,
verbose = T
)
class_metrics <- metric_set(accuracy,
f_meas,
j_index,
kap,
precision,
sensitivity,
specificity,
roc_auc,
mcc,
pr_auc)
Tuned_results <-
workflow_set(
preproc = list(rec = dat_rec),
models = list(parsnip_nn = parsnip_nn,
parsnip_log = parsnip_log)
) %>%
workflow_map(
fn = "tune_race_anova",
seed = 123,
grid = latin_grid,
resamples = dat_cv,
verbose = T,
metrics = class_metrics,
control = race_ctrl
)
这是我得到的错误,基本上说tune()
无法识别模型的一些参数。
i 1 of 2 tuning: rec_parsnip_nn
x 1 of 2 tuning: rec_parsnip_nn failed with: Error in check_grid(grid = grid, workflow = workflow, pset = pset) : The provided `grid` has the following parameter columns that have not been marked for tuning by `tune()`: 'mixture'.
i 2 of 2 tuning: rec_parsnip_log
x 2 of 2 tuning: rec_parsnip_log failed with: Error in check_grid(grid = grid, workflow = workflow, pset = pset) : The provided `grid` has the following parameter columns that have not been marked for tuning by `tune()`: 'hidden_units', 'epochs'.
如果我们检查grid_results
:
# A workflow set/tibble: 2 x 4
wflow_id info option result
<chr> <list> <list> <list>
1 rec_parsnip_nn <tibble [1 x 4]> <opts[4]> <try-errr [1]>
2 rec_parsnip_log <tibble [1 x 4]> <opts[4]> <try-errr [1]>
我不知道为什么mixture
、hidden_units
和epochs
等参数不能被tune()
识别。知道我哪里做错了吗?
神经网络没有一个称为mixture
的参数,正则化回归模型也没有一个名为hidden_units
或epochs
的参数。两个模型不能使用相同的grid
参数,因为它们没有相同的超参数。相反,你会想要:
- 为两个模型创建单独的网格
- 使用
option_add()
通过id
参数将每个网格添加到其模型中
还可以查看TMwR的第15章,了解有关如何仅向特定工作流添加选项的更多信息。由于您使用的是拉丁hybercube,这是tidymodels中的默认值,因此您可能希望跳过所有这些,转而使用grid = 30
。