R 的超參數調校
Dr. Shirin Elsinghorst
Senior Data Scientist
?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。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]]
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 檢視結果使用 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
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
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
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)
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 的超參數調校