Recherche en grille et aléatoire avec mlr

Optimisation des hyperparamètres en R

Dr. Shirin Elsinghorst

Senior Data Scientist

Optimisation d’hyperparamètres avec mlr

Dans mlr, vous devez définir

  1. l’espace de recherche pour chaque hyperparamètre
  2. la méthode d’optimisation (p. ex. grille ou aléatoire)
  3. la méthode de rééchantillonnage
Optimisation des hyperparamètres en R

Définir l’espace de recherche

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
Optimisation des hyperparamètres en 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))
Optimisation des hyperparamètres en R

Définir la méthode d’optimisation

  • Recherche en grille
ctrl_grid <- makeTuneControlGrid()
ctrl_grid
Tune control: TuneControlGrid
Same resampling instance: TRUE
Imputation value: <worst>
Start: <NULL>

Tune threshold: FALSE
Further arguments: resolution=10

Ne gère que les ensembles de paramètres discrets !

  • Recherche aléatoire
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
Optimisation des hyperparamètres en R

Définir la stratégie de rééchantillonnage

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)
Optimisation des hyperparamètres en R

Ajuster les hyperparamètres

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] Result: hidden=two; activation=Tanh; l1=0.113; 
l2=0.0973 : mmce.test.mean=0.2000000

# tictoc
26.13 sec elapsed
Optimisation des hyperparamètres en R

Passons à la pratique !

Optimisation des hyperparamètres en R

Preparing Video For Download...