超参数调优

在 R 中使用 tidymodels 建模

David Svancer

Data Scientist

超参数

在训练前设定、控制模型复杂度的模型参数

parsnip决策树

  • cost_complexity
    • 惩罚过多的叶节点数
  • tree_depth
    • 根到叶节点的最长路径
  • min_n
    • 继续分裂所需的最少样本数

决策树模型的矩形区域

在 R 中使用 tidymodels 建模

默认超参数值

decision_tree() 为超参数设置默认值

  • cost_complexity 默认为 0.01
  • tree_depth 默认为 30
  • min_n 默认为 20

这些值并不一定适用于所有数据集

  • 进行超参数调优
    • 用交叉验证寻找最优超参数组合的过程
dt_model <- decision_tree() %>% 
  set_engine('rpart') %>% 
  set_mode('classification')
在 R 中使用 tidymodels 建模

标记待调优的超参数

tune 包的 tune() 函数

  • parsnip 模型规范中,将待调优的超参数设为 tune()
  • 创建带可调参数的模型对象
    • 提示其他函数需要对其进行优化
dt_tune_model <- decision_tree(cost_complexity = tune(),
                               tree_depth = tune(),
                               min_n = tune()) %>% 
  set_engine('rpart') %>% 
  set_mode('classification')

dt_tune_model
Decision Tree Model Specification (classification)

Main Arguments:
  cost_complexity = tune()
  tree_depth = tune()
  min_n = tune()

Computational engine: rpart
在 R 中使用 tidymodels 建模

创建调参工作流

可以轻松更新 workflow 对象

  • 现有 leads_wkfl
    • 线索评分数据的特征工程步骤,决策树模型使用默认超参数
  • leads_wkfl 传给 update_model(),并提供带可调参数的决策树模型
leads_tune_wkfl <- leads_wkfl %>% 

update_model(dt_tune_model)
leads_tune_wkfl
== Workflow ===============
Preprocessor: Recipe
Model: decision_tree()
-- Preprocessor -----------
3 Recipe Steps
* step_corr()
* step_normalize()
* step_dummy()
-- Model ------------------
Decision Tree Model Specification (classification)
Main Arguments: cost_complexity = tune()
                tree_depth = tune()
                min_n = tune()
Computational engine: rpart
在 R 中使用 tidymodels 建模

网格搜索

最常见的超参数调优方法

  • 生成超参数取值的唯一组合网格
    • 对每个组合,用交叉验证估计模型性能
  • 选择表现最佳的组合

 

cost_complexity tree_depth min_n
0.001 20 35
0.001 20 15
0.001 35 35
0.001 35 15
0.2 20 35
... ... ...
在 R 中使用 tidymodels 建模

识别超参数

dials 包的 parameters() 函数

  • 接收一个 parsnip 模型对象
  • 返回包含由 tune() 标记的超参数的 tibble(如有)
    • 用于配合 dials 生成调参网格
parameters(dt_tune_model)
Collection of 3 parameters for tuning

  identifier            type     object
cost_complexity  cost_complexity nparam[+]
tree_depth       tree_depth      nparam[+]
min_n            min_n           nparam[+]
在 R 中使用 tidymodels 建模

随机网格

生成随机组合

  • 此方法往往更易找到更优的超参数取值

grid_random() 函数

  • 第一个参数为 parameters() 的结果
  • size 指定要生成的随机组合数
    • grid_random() 前执行 set.seed() 以保证可复现
set.seed(214)
grid_random(parameters(dt_tune_model),

size = 5)
# A tibble: 5 x 3
  cost_complexity  tree_depth min_n
            <dbl>      <int>  <int>
1    0.0000000758        14     39
2    0.0243               5     34
3    0.00000443          11      8
4    0.000000600          3      5
5    0.00380              5     36
在 R 中使用 tidymodels 建模

保存调参网格

调参的第一步

  • 创建并保存一个调参网格
  • dt_grid 包含 5 组随机的超参数组合
set.seed(214)
dt_grid <- grid_random(parameters(dt_tune_model),
                       size = 5)

dt_grid
# A tibble: 5 x 3
  cost_complexity  tree_depth min_n
            <dbl>      <int>  <int>
1    0.0000000758        14     39
2    0.0243               5     34
3    0.00000443          11      8
4    0.000000600          3      5
5    0.00380              5     36
在 R 中使用 tidymodels 建模

用交叉验证进行超参数调优

tune_grid() 函数执行超参数调优

需要以下参数:

  • workflowparsnip 模型
  • 交叉验证对象 resamples
  • 调参网格 grid
  • 可选的 metrics 函数

返回结果 tibble

  • .metrics
    • 每个折的结果列表列
dt_tuning <- leads_tune_wkfl %>%

tune_grid(resamples = leads_folds,
grid = dt_grid,
metrics = leads_metrics)
dt_tuning
# Tuning results
# 10-fold cross-validation using stratification 
# A tibble: 10 x 4
   splits             id       .metrics         ..
   <list>            <chr>      <list>          ..
<split [896/100]>   Fold01   <tibble [15 x 7]>  ..
................    ......   ...............    ..
<split [897/99]>    Fold09   <tibble [15 x 7]>  ..
<split [897/99]>    Fold10   <tibble [15 x 7]>  ..
在 R 中使用 tidymodels 建模

查看调参结果

collect_metrics() 默认给出汇总结果

  • 对每个组合,计算各折指标的平均值
dt_tuning %>% 
  collect_metrics()
# A tibble: 15 x 9
   cost_complexity tree_depth min_n .metric .estimator  mean     n std_err .config
             <dbl>      <int> <int> <chr>   <chr>      <dbl> <int>   <dbl> <chr>  
 1    0.0000000758         14    39 roc_auc binary     0.827    10 0.0147  Model1 
 2    0.0000000758         14    39 sens    binary     0.728    10 0.0277  Model1 
 3    0.0000000758         14    39 spec    binary     0.865    10 0.0156  Model1 
 4    0.0243                5    34 roc_auc binary     0.823    10 0.0147  Model2 
 .    ......               ..    .. ....    ......     .....    .. .....   ......
14    0.00380               5    36 sens    binary     0.747    10 0.0209  Model5 
15    0.00380               5    36 spec    binary     0.858    10 0.0161  Model5
在 R 中使用 tidymodels 建模

开始调参吧!

在 R 中使用 tidymodels 建模

Preparing Video For Download...