我正在尝试使用regr.cvglment对内部循环的10个CV和对外部循环的10个CV进行嵌套重采样。 Mlr使用包装函数(https://mlr-org.github.io/mlr/articles/tutorial/devel/nested_resampling.html)
提供代码现在,我只是从提供的代码中交换了两件事 1)使用“ regr.cvglmnet”代替支持向量机(ksvm) 2)内循环和外循环的迭代次数
在lrn函数之后,出现以下指定的错误。有人可以向我解释吗?我是编码和机器学习的新手,所以我可能在代码中做过一些非常愚蠢的事情。...
ps = makeParamSet(
makeDiscreteParam("C", values = 2^(-12:12)),
makeDiscreteParam("sigma", values = 2^(-12:12))
)
ctrl = makeTuneControlGrid()
inner = makeResampleDesc("Subsample", iters = 10)
lrn = makeTuneWrapper("regr.cvglmnet", resampling = inner, par.set = ps,
control = ctrl, show.info = FALSE)
# Error in checkTunerParset(learner, par.set, measures, control) :
# Can only tune parameters for which learner parameters exist: C,sigma
### Outer resampling loop
outer = makeResampleDesc("CV", iters = 10)
r = resample(lrn, iris.task, resampling = outer, extract = getTuneResult,
show.info = FALSE)
答案 0 :(得分:2)
将LASSO与glmnet
一起使用时,只需要调整s
。这是模型预测新数据时使用的重要参数。
参数lambda
绝对没有影响,这是因为在预测中对程序包进行编码的方式。如果您将s
设置为与选择的任何lambda
值不同,则将使用s
作为惩罚项来重新拟合模型。
默认情况下,在lambda
调用过程中会拟合多个具有不同train
值的模型。但是,为了进行预测,将使用最佳lambda
值来拟合新模型。因此,实际上,调整是在预测步骤中完成的。
可以通过
选择s
的良好默认范围
glmnet
中的默认值训练模型lambda
的最小值和最大值s
的上下限,然后使用mlr
进行调整另请参阅this讨论。
library(mlr)
#> Loading required package: ParamHelpers
lrn_glmnet <- makeLearner("regr.glmnet",
alpha = 1,
intercept = FALSE)
# check lambda
glmnet_train = mlr::train(lrn_glmnet, bh.task)
summary(glmnet_train$learner.model$lambda)
#> Min. 1st Qu. Median Mean 3rd Qu. Max.
#> 143.5 157.4 172.8 174.3 189.6 208.1
# set limits
ps_glmnet <- makeParamSet(makeNumericParam("s", lower = 140, upper = 208))
# tune params in parallel using a grid search for simplicity
tune.ctrl = makeTuneControlGrid()
inner <- makeResampleDesc("CV", iters = 2)
configureMlr(on.learner.error = "warn", on.error.dump = TRUE)
library(parallelMap)
parallelStart(mode = "multicore", level = "mlr.tuneParams", cpus = 4,
mc.set.seed = TRUE) # only parallelize the tuning
#> Starting parallelization in mode=multicore with cpus=4.
set.seed(12345)
params_tuned_glmnet = tuneParams(lrn_glmnet, task = bh.task, resampling = inner,
par.set = ps_glmnet, control = tune.ctrl,
measure = list(rmse))
#> [Tune] Started tuning learner regr.glmnet for parameter set:
#> Type len Def Constr Req Tunable Trafo
#> s numeric - - 140 to 208 - TRUE -
#> With control class: TuneControlGrid
#> Imputation value: Inf
#> Mapping in parallel: mode = multicore; cpus = 4; elements = 10.
#> [Tune] Result: s=140 : rmse.test.rmse=17.9803086
parallelStop()
#> Stopped parallelization. All cleaned up.
# train the model on the whole dataset using the `s` value from the tuning
lrn_glmnet_tuned <- makeLearner("regr.glmnet",
alpha = 1,
s = 140,
intercept = FALSE)
#lambda = sort(seq(0, 5, length.out = 100), decreasing = T))
glmnet_train_tuned = mlr::train(lrn_glmnet_tuned, bh.task)
由reprex package(v0.2.0)于2018-07-03创建。
devtools::session_info()
#> Session info -------------------------------------------------------------
#> setting value
#> version R version 3.5.0 (2018-04-23)
#> system x86_64, linux-gnu
#> ui X11
#> language (EN)
#> collate en_US.UTF-8
#> tz Europe/Berlin
#> date 2018-07-03
#> Packages -----------------------------------------------------------------
#> package * version date source
#> backports 1.1.2 2017-12-13 CRAN (R 3.5.0)
#> base * 3.5.0 2018-06-04 local
#> BBmisc 1.11 2017-03-10 CRAN (R 3.5.0)
#> bit 1.1-14 2018-05-29 cran (@1.1-14)
#> bit64 0.9-7 2017-05-08 CRAN (R 3.5.0)
#> blob 1.1.1 2018-03-25 CRAN (R 3.5.0)
#> checkmate 1.8.5 2017-10-24 CRAN (R 3.5.0)
#> codetools 0.2-15 2016-10-05 CRAN (R 3.5.0)
#> colorspace 1.3-2 2016-12-14 CRAN (R 3.5.0)
#> compiler 3.5.0 2018-06-04 local
#> data.table 1.11.4 2018-05-27 CRAN (R 3.5.0)
#> datasets * 3.5.0 2018-06-04 local
#> DBI 1.0.0 2018-05-02 cran (@1.0.0)
#> devtools 1.13.6 2018-06-27 CRAN (R 3.5.0)
#> digest 0.6.15 2018-01-28 CRAN (R 3.5.0)
#> evaluate 0.10.1 2017-06-24 CRAN (R 3.5.0)
#> fastmatch 1.1-0 2017-01-28 CRAN (R 3.5.0)
#> foreach 1.4.4 2017-12-12 CRAN (R 3.5.0)
#> ggplot2 2.2.1 2016-12-30 CRAN (R 3.5.0)
#> git2r 0.21.0 2018-01-04 CRAN (R 3.5.0)
#> glmnet 2.0-16 2018-04-02 CRAN (R 3.5.0)
#> graphics * 3.5.0 2018-06-04 local
#> grDevices * 3.5.0 2018-06-04 local
#> grid 3.5.0 2018-06-04 local
#> gtable 0.2.0 2016-02-26 CRAN (R 3.5.0)
#> htmltools 0.3.6 2017-04-28 CRAN (R 3.5.0)
#> iterators 1.0.9 2017-12-12 CRAN (R 3.5.0)
#> knitr 1.20 2018-02-20 CRAN (R 3.5.0)
#> lattice 0.20-35 2017-03-25 CRAN (R 3.5.0)
#> lazyeval 0.2.1 2017-10-29 CRAN (R 3.5.0)
#> magrittr 1.5 2014-11-22 CRAN (R 3.5.0)
#> Matrix 1.2-14 2018-04-09 CRAN (R 3.5.0)
#> memoise 1.1.0 2017-04-21 CRAN (R 3.5.0)
#> memuse 4.0-0 2017-11-10 CRAN (R 3.5.0)
#> methods * 3.5.0 2018-06-04 local
#> mlr * 2.13 2018-07-01 local
#> munsell 0.5.0 2018-06-12 CRAN (R 3.5.0)
#> parallel 3.5.0 2018-06-04 local
#> parallelMap * 1.3 2015-06-10 CRAN (R 3.5.0)
#> ParamHelpers * 1.11 2018-06-25 CRAN (R 3.5.0)
#> pillar 1.2.3 2018-05-25 CRAN (R 3.5.0)
#> plyr 1.8.4 2016-06-08 CRAN (R 3.5.0)
#> Rcpp 0.12.17 2018-05-18 cran (@0.12.17)
#> rlang 0.2.1 2018-05-30 CRAN (R 3.5.0)
#> rmarkdown 1.10 2018-06-11 CRAN (R 3.5.0)
#> rprojroot 1.3-2 2018-01-03 CRAN (R 3.5.0)
#> RSQLite 2.1.1 2018-05-06 cran (@2.1.1)
#> scales 0.5.0 2017-08-24 CRAN (R 3.5.0)
#> splines 3.5.0 2018-06-04 local
#> stats * 3.5.0 2018-06-04 local
#> stringi 1.2.3 2018-06-12 CRAN (R 3.5.0)
#> stringr 1.3.1 2018-05-10 CRAN (R 3.5.0)
#> survival 2.42-3 2018-04-16 CRAN (R 3.5.0)
#> tibble 1.4.2 2018-01-22 CRAN (R 3.5.0)
#> tools 3.5.0 2018-06-04 local
#> utils * 3.5.0 2018-06-04 local
#> withr 2.1.2 2018-03-15 CRAN (R 3.5.0)
#> XML 3.98-1.11 2018-04-16 CRAN (R 3.5.0)
#> yaml 2.1.19 2018-05-01 CRAN (R 3.5.0)
答案 1 :(得分:1)
该错误消息告诉您,您无法调整mlr对于该学习者不了解的参数-regr.cvglmnet
没有C
和sigma
参数。您可以使用getLearnerParamSet()
函数获得mlr知道的学习者参数:
> getLearnerParamSet(makeLearner("regr.cvglmnet")) Type len Def Constr Req family discrete - gaussian gaussian,poisson - alpha numeric - 1 0 to 1 - nfolds integer - 10 3 to Inf - type.measure discrete - mse mse,mae - s discrete - lambda.1se lambda.1se,lambda.min - nlambda integer - 100 1 to Inf - lambda.min.ratio numeric - - 0 to 1 - standardize logical - TRUE - - intercept logical - TRUE - - thresh numeric - 1e-07 0 to Inf - dfmax integer - - 0 to Inf - pmax integer - - 0 to Inf - exclude integervector - 1 to Inf - penalty.factor numericvector - 0 to 1 - lower.limits numericvector - -Inf to 0 - upper.limits numericvector - 0 to Inf - maxit integer - 100000 1 to Inf - type.gaussian discrete - - covariance,naive - fdev numeric - 1e-05 0 to 1 - devmax numeric - 0.999 0 to 1 - eps numeric - 1e-06 0 to 1 - big numeric - 9.9e+35 -Inf to Inf - mnlam integer - 5 1 to Inf - pmin numeric - 1e-09 0 to 1 - exmx numeric - 250 -Inf to Inf - prec numeric - 1e-10 -Inf to Inf - mxit integer - 100 1 to Inf - Tunable Trafo family TRUE - alpha TRUE - nfolds TRUE - type.measure TRUE - s TRUE - nlambda TRUE - lambda.min.ratio TRUE - standardize TRUE - intercept TRUE - thresh TRUE - dfmax TRUE - pmax TRUE - exclude TRUE - penalty.factor TRUE - lower.limits TRUE - upper.limits TRUE - maxit TRUE - type.gaussian TRUE - fdev TRUE - devmax TRUE - eps TRUE - big TRUE - mnlam TRUE - pmin TRUE - exmx TRUE - prec TRUE - mxit TRUE -
您可以使用这些参数中的任何一个来定义用于调整此特定学习者的有效参数集,例如:
ps = makeParamSet( makeDiscreteParam("family", values = c("gaussian", "poisson")), makeDiscreteParam("alpha", values = 0.1*0:10) )