使用 H2O 的网格搜索与随机搜索

R 中的超参数调优

Dr. Shirin Elsinghorst

Senior Data Scientist

H2O 模型的超参数

  • 梯度提升的超参数:
?h2o.gbm
  • ntrees:树的数量。默认 50。

  • max_depth:最大树深。默认 5。

  • min_rows:叶节点中允许的最少(加权)观测数。默认 10。

  • learn_rate:学习率(0.0 到 1.0)。默认 0.1。

  • learn_rate_annealing:每棵树后按此因子缩放学习率(如 0.99 或 0.999)。默认 1。
R 中的超参数调优

用 H2O 准备建模数据

  • 转为 H2O frame
seeds_data_hf <- as.h2o(seeds_data)
  • 指定特征与目标
y <- "seed_type"
x <- setdiff(colnames(seeds_data_hf), y)
  • 将数据划分为训练集、验证集和测试集
sframe <- h2o.splitFrame(data = seeds_data_hf, ratios = c(0.7, 0.15), seed = 42)
train <- sframe[[1]]
valid <- sframe[[2]]
test <- sframe[[3]]
R 中的超参数调优

定义超参数网格

  • GBM 超参数
gbm_params <- list(ntrees = c(100, 150, 200), max_depth = c(3, 5, 7), learn_rate = c(0.001, 0.01, 0.1))
  • h2o.grid 函数
gbm_grid <- h2o.grid("gbm", 
                     grid_id = "gbm_grid",
                     x = x, 
                     y = y,
                     training_frame = train,
                     validation_frame = valid,
                     seed = 42,
                     hyper_params = gbm_params)
  • 使用 h2o.getGrid 查看结果
R 中的超参数调优

查看网格对象

  • 使用 h2o.getGrid 查看模型 gbm_grid 的结果。

  • 按验证准确率排序

gbm_gridperf <- h2o.getGrid(grid_id = "gbm_grid", sort_by = "accuracy", decreasing = TRUE)
Grid ID: gbm_grid 
Used hyper parameters: 
  -  learn_rate 
  -  max_depth 
  -  ntrees 
Number of models: 27 
Number of failed models: 0 

Hyper-Parameter Search Summary: ordered by decreasing accuracy
R 中的超参数调优

从网格中提取最优模型

  • 按验证准确率排名第一的 GBM 模型在位置 1
best_gbm <- h2o.getModel(gbm_gridperf@model_ids[[1]])
  • 这是该最优模型的超参数:
print(best_gbm@model[["model_summary"]])
Model Summary: 
 number_of_trees number_of_internal_trees model_size_in_bytes min_depth
             200                      600              100961         2 
 max_depth mean_depth min_leaves max_leaves mean_leaves
         7    5.22667          3         10     8.38833
R 中的超参数调优

从网格中提取最优模型

  • best_gbm 是一个标准 H2O 模型对象,可按常规模型使用!
h2o.performance(best_gbm, test)
MSE: (Extract with `h2o.mse`) 0.04761904
RMSE: (Extract with `h2o.rmse`) 0.2182179
Logloss: (Extract with `h2o.loglos
R 中的超参数调优

H2O 的随机搜索

  • 除了超参数网格外,还要添加搜索条件:
gbm_params <- list(ntrees = c(100, 150, 200),
                   max_depth = c(3, 5, 7),
                   learn_rate = c(0.001, 0.01, 0.1))

search_criteria <- list(strategy = "RandomDiscrete", max_runtime_secs = 60, seed = 42)
gbm_grid <- h2o.grid("gbm", grid_id = "gbm_grid", x = x, y = y, training_frame = train, validation_frame = valid, seed = 42, hyper_params = gbm_params, search_criteria = search_criteria)
R 中的超参数调优
search_criteria <- list(strategy = "RandomDiscrete", 
                        stopping_metric = "mean_per_class_error", 
                        stopping_tolerance = 0.0001, 
                        stopping_rounds = 6)

gbm_grid <- h2o.grid("gbm", x = x, y = y, training_frame = train, validation_frame = valid, seed = 42, hyper_params = gbm_params, search_criteria = search_criteria)
H2O Grid Details
================
Grid ID: gbm_grid 
Used hyper parameters: 
  -  learn_rate 
  -  max_depth 
  -  ntrees 
Number of models: 30 
Number of failed models: 0
R 中的超参数调优

Time to practice!

R 中的超参数调优

Preparing Video For Download...