自适应重采样

R 中的超参数调优

Dr. Shirin Elsinghorst

Senior Data Scientist

什么是自适应重采样?

网格搜索

  • 计算所有超参数组合。

随机搜索

  • 随机计算超参数组合的子集。

→ 最后再评估最优组合。

自适应重采样

  • 在表现良好的组合附近,对超参数组合进行再采样。
  • 因此,自适应重采样更快、更高效!

"Futility Analysis in the Cross-Validation of Machine Learning Models." Max Kuhn;ARXIV 2014

R 中的超参数调优

caret 中的自适应重采样

trainControl: method = "adaptive_cv" + search = "random" + adaptive =

  • min:每个超参数的最少重采样次数

  • alpha:移除超参数的置信水平

  • method:线性模型用 "gls",Bradley-Terry 用 "BT"

  • complete:若为 TRUE,生成完整重采样集

fitControl <- trainControl(method = "adaptive_cv",
                             adaptive = list(min = 2, alpha = 0.05, 
                                             method = "gls", complete = TRUE),
                             search = "random")
R 中的超参数调优
  • trainControl() + tuneLength = x
fitControl <- trainControl(method = "adaptive_cv", number = 3, repeats = 3,
                           adaptive = list(min = 2, 
                                           alpha = 0.05, 
                                           method = "gls", 
                                           complete = TRUE),
                             search = "random")

tic() set.seed(42) gbm_model_voters_adaptive <- train(turnout16_2016 ~ ., data = voters_train_data, method = "gbm", trControl = fitControl, verbose = FALSE, tuneLength = 7) toc()
用时 1239.837 秒
R 中的超参数调优

自适应重采样

gbm_model_voters_adaptive
...
各调参与值的重采样结果:
  shrinkage   interaction.depth  n.minobsinnode  n.trees  Accuracy   Kappa       Resamples
  0.07137493   5                  6              4152     0.9564654  0.02856571  9        
  0.08408739   5                 14               674     0.9547185  0.02098853  4        
  0.28552325   8                 15              3209     0.9568141  0.03024238  3        
  0.33663932  10                 13              2595     0.9571130  0.04250979  9        
  0.54251480   3                 24              3683     0.9482171  0.03568586  2        
  0.56406870   7                 25              4685     0.9549898  0.05284333  5        
  0.58695763   8                 24              1431     0.9520286  0.02742592  2        
以最大 Accuracy 选择最优模型。
最终模型参数:n.trees = 2595,interaction.depth = 10,shrinkage = 0.3366393,n.minobsinnode = 13。
R 中的超参数调优

开始编码!

R 中的超参数调优

Preparing Video For Download...