使用 mlr 的格點與隨機搜尋

R 的超參數調校

Dr. Shirin Elsinghorst

Senior Data Scientist

使用 mlr 進行超參數調校

mlr 中,你需要定義:

  1. 每個超參數{{1}}的「搜尋空間」
  2. 「調參方法」(例如格點或隨機搜尋)
  3. 「重抽樣方法」
R 的超參數調校

定義搜尋空間

makeParamSet(
  makeNumericParam(),
  makeIntegerParam(),
  makeDiscreteParam(),
  makeLogicalParam(),
  makeDiscreteVectorParam()
)
                                       Type  len             Def
autoencoder                         logical    -           FALSE
use_all_factor_level                logical    -            TRUE 
activation                         discrete    -       Rectifier
hidden                        integervector <NA>         200,200 
epochs                              numeric    -              10
train_samples_per_iteration         numeric    -              -2 
seed                                integer    -               -
getParamSet("classif.h2o.deeplearning")
adaptive_rate                        logical    -            TRUE
rho                                  numeric    -            0.99
epsilon                              numeric    -           1e-08
rate                                 numeric    -           0.005
R 的超參數調校
                                       Type  len             Def
autoencoder                         logical    -           FALSE
use_all_factor_level                logical    -            TRUE
activation                         discrete    -       Rectifier
hidden                        integervector <NA>         200,200
epochs                              numeric    -              10
train_samples_per_iteration         numeric    -              -2
seed                                integer    -               -
adaptive_rate                       logical    -            TRUE           
rho                                 numeric    -            0.99
epsilon                             numeric    -           1e-08
rate                                numeric    -           0.005
param_set <- makeParamSet(
  makeDiscreteParam("hidden", values = list(one = 10, two = c(10, 5, 10))),
  makeDiscreteParam("activation", values = c("Rectifier", "Tanh")),
  makeNumericParam("l1", lower = 0.0001, upper = 1),
  makeNumericParam("l2", lower = 0.0001, upper = 1))
R 的超參數調校

定義調參方法

  • 格點搜尋
ctrl_grid <- makeTuneControlGrid()
ctrl_grid
Tune control: TuneControlGrid
Same resampling instance: TRUE
Imputation value: <worst>
Start: <NULL>

Tune threshold: FALSE
Further arguments: resolution=10

只能處理「離散」參數集合!

  • 隨機搜尋
ctrl_random <- makeTuneControlRandom()
ctrl_random
Tune control: TuneControlRandom
Same resampling instance: TRUE
Imputation value: <worst>
Start: <NULL>
Budget: 100
Tune threshold: FALSE
Further arguments: maxit=100
R 的超參數調校

定義重抽樣策略

cross_val <- makeResampleDesc("RepCV", 
                              predict = "both",
                              folds = 5 * 3)

param_set <- makeParamSet(...) ctrl_grid <- makeTuneControlGrid()
task <- makeClassifTask(data = knowledge_train_data, target = "UNS") lrn <- makeLearner("classif.h2o.deeplearning", predict.type = "prob", fix.factors.prediction = TRUE)
lrn_tune <- tuneParams(lrn, task, resampling = cross_val, control = ctrl_grid, par.set = param_set)
R 的超參數調校

調校超參數

lrn_tune <- tuneParams(lrn, task, resampling = cross_val, control = ctrl_grid, par.set = param_set)
[Tune-y] 27: mmce.test.mean=0.6200000; time: 0.0 min
[Tune-x] 28: hidden=two; activation=Rectifier; l1=0.578; l2=1
[Tune-y] 28: mmce.test.mean=0.6800000; time: 0.0 min
[Tune-x] 29: hidden=one; activation=Rectifier; l1=0.156; l2=0.68
[Tune-y] 29: mmce.test.mean=0.4400000; time: 0.0 min
[Tune-x] 30: hidden=one; activation=Rectifier; l1=0.717; l2=0.427
[Tune-y] 30: mmce.test.mean=0.6600000; time: 0.0 min

[Tune] 結果:hidden=two; activation=Tanh; l1=0.113; 
l2=0.0973 :mmce.test.mean=0.2000000

# tictoc
經過 26.13 秒
R 的超參數調校

一起來練習吧!

R 的超參數調校

Preparing Video For Download...