超參數調校

在 R 中使用 tidymodels 建立模型

David Svancer

Data Scientist

超參數

在訓練前設定、用來控制模型複雜度的模型參數

parsnip decision tree

  • cost_complexity
    • 懲罰過多的終端節點數
  • tree_depth
    • 從根到終端節點的最長路徑
  • min_n
    • 節點可再分割所需的最少資料點數

決策樹模型的矩形區域

在 R 中使用 tidymodels 建立模型

預設超參數值

decision_tree() 會設定預設超參數值

  • cost_complexity 預設為 0.01
  • tree_depth 預設為 30
  • min_n 預設為 20

這些值未必適用所有資料集

  • Hyperparameter tuning
    • 使用交叉驗證尋找最佳超參數組合的流程
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

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 指定要產生的隨機組合數
    • 為可重現性,先執行 set.seed() 再呼叫 grid_random()
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...