r-在MLR中,如何将逻辑超参数设置为TRUE或FALSE



我有这个数据集来尝试使用classification.ada 进行分类任务

library(mlr)
data("HouseVotes84")
#Using HouseVotes84 as Classification Task Dataset and mtcars as Regression Task Dataset
dummy_data_classif <- HouseVotes84[,2:length(colnames(HouseVotes84))] %>% 
mutate_if(is.factor, as.numeric)
dummy_data_classif <- data.frame(cbind(Class=HouseVotes84[,1], dummy_data_classif))
dummy_data_classif[is.na(dummy_data_classif)] <- 0
dummy_data_classif_numeric <- dummy_data_classif[-1] %>%
mutate_if(is.factor, as.numeric)
dummy_data_classif_numeric <- data.frame(cbind(dummy_data_classif[1], 
dummy_data_classif_numeric))
colnames(dummy_data_classif_numeric) <- colnames(dummy_data_classif)

我在字符串中有这个参数表达式,稍后将对其进行评估以进行分类。MLR 中的参数集

param_to_eval
"hyperparam <<- makeParamSet(
makeIntegerParam("iter",lower = 50,upper=250), 
makeIntegerParam("max.iter",lower = 30,upper=200), 
makeLogicalParam("model.coef",FALSE, tunable=FALSE), 
makeDiscreteParam("loss",values = c("exponential", "logistic")), 
makeDiscreteParam("type",values = c("discrete", "real", "gentle")), 
makeNumericParam("nu",lower = 0,upper=100), 
makeNumericParam("bag.frac",lower = 0,upper=1), 
makeLogicalParam("bag.shift"), 
makeNumericParam("delta",lower = 0,upper=1e-07), 
makeIntegerParam("minsplit",lower = 1,upper=30), 
makeIntegerParam("minbucket",lower = 1,upper=20), 
makeNumericParam("cp",lower = 0,upper=1), 
makeIntegerParam("maxcompete",lower = 0,upper=6), 
makeIntegerParam("maxsurrogate",lower = 0,upper=7.5), 
makeDiscreteParam("usesurrogate",values = c(0, 1, 2)), 
makeDiscreteParam("surrogatestyle",values = c(0, 1)), 
makeIntegerParam("maxdepth",lower = 1,upper=30))"

然后我为classification.ada定义了任务,并计划调整20个随机参数集

task <- makeClassifTask(data = dummy_data_classif_numeric, 
target = "Class")
lrn <- makeLearner(cl = "classif.ada", fix.factors.prediction = TRUE)
eval(parse(text=param_to_eval)) 
hyper_search <- makeTuneControlRandom(maxit = 20)
resampling_method <- makeResampleDesc("cv")

# Perform tuning
lrn_tune <- tuneParams(learner = "classif.ada", task = task, 
resampling = resampling_method, 
control = hyper_search, par.set = hyperparam)

在这些例子中,我希望"model.coef"超参数只包含要调整的FALSE值。运行这些之后,model.coef仍然在TRUEFALSE之间调整,并得到这个错误:

[Tune] Started tuning learner classif.ada for parameter set:
Type len   Def               Constr Req Tunable Trafo
iter            integer   -     -            50 to 250   -    TRUE     -
max.iter        integer   -     -            30 to 200   -    TRUE     -
model.coef      logical   - FALSE                    -   -   FALSE     -
loss           discrete   -     - exponential,logistic   -    TRUE     -
type           discrete   -     - discrete,real,gentle   -    TRUE     -
nu              numeric   -     -             0 to 100   -    TRUE     -
bag.frac        numeric   -     -               0 to 1   -    TRUE     -
bag.shift       logical   -     -                    -   -    TRUE     -
delta           numeric   -     -           0 to 1e-07   -    TRUE     -
minsplit        integer   -     -              1 to 30   -    TRUE     -
minbucket       integer   -     -              1 to 20   -    TRUE     -
cp              numeric   -     -               0 to 1   -    TRUE     -
maxcompete      integer   -     -               0 to 6   -    TRUE     -
maxsurrogate    integer   -     -             0 to 7.5   -    TRUE     -
usesurrogate   discrete   -     -                0,1,2   -    TRUE     -
surrogatestyle discrete   -     -                  0,1   -    TRUE     -
maxdepth        integer   -     -              1 to 30   -    TRUE     -
With control class: TuneControlRandom
Imputation value: 1
[Tune-x] 1: iter=233; max.iter=141; model.coef=FALSE; loss=exponential; type=gentle; nu=63.5; bag.frac=0.686; bag.shift=TRUE; delta=3.49e-08; minsplit=21; minbucket=2; cp=0.881; maxcompete=1; maxsurrogate=2; usesurrogate=2; surrogatestyle=0; maxdepth=17
[Tune-y] 1: mmce.test.mean=0.0598309; time: 0.8 min
[Tune-x] 2: iter=230; max.iter=115; model.coef=TRUE; loss=exponential; type=gentle; nu=39.6; bag.frac=0.35; bag.shift=TRUE; delta=4.87e-08; minsplit=1; minbucket=2; cp=0.523; maxcompete=4; maxsurrogate=7; usesurrogate=0; surrogatestyle=1; maxdepth=21
Error in if (any(wt < 0)) stop("negative weights not allowed") : 
missing value where TRUE/FALSE needed
In addition: Warning message:
In log((1 - errm)/errm) :
Error in if (any(wt < 0)) stop("negative weights not allowed") : 
missing value where TRUE/FALSE needed 

如何使"model.coef"只包含FALSE

如果您希望一个值是固定的,并且不包含在搜索空间中,则必须手动设置此参数的值,并将其从搜索空间中排除:

hyperparam$pars$model.coef = NULL
learner <- makeLearner("classif.ada", par.vals = list(model.coef = FALSE))
lrn_tune <- tuneParams(learner = learner, task = task, 
resampling = resampling_method, 
control = hyper_search, par.set = hyperparam)

直接在搜索空间(hyperparam对象(的定义中省略model.coef更有意义,但您的示例看起来似乎以某种方式预定义了对象。

要为学习者创建固定参数,请在创建学习者时将其设置在par.vals中。看见https://mlr.mlr-org.com/reference/makeLearner.html.

在参数集中指定的参数将始终在指定的范围内进行调整。

PS:使用<<-为GlobalEnv分配一个ParamSet不是一个好主意(一般来说,不仅仅是对mlr(
PS2:请注意,不赞成使用{mlr},而应使用{mlr3}。

相关内容

  • 没有找到相关文章

最新更新