Hyperparameter Tuning in R
Dr. Shirin Elsinghorst
Senior Data Scientist
?h2o.gbm
ntrees
: Number of trees. Defaults to 50.
max_depth
: Maximum tree depth. Defaults to 5.
min_rows
: Fewest allowed (weighted) observations in a leaf. Defaults to 10.
learn_rate
: Learning rate (from 0.0 to 1.0) Defaults to 0.1.
learn_rate_annealing
: Scale the learning rate by this factor after each tree (e.g., 0.99 or 0.999) Defaults to 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
functiongbm_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
Examine results for our model gbm_grid
with h2o.getGrid
function.
Get the grid results sorted by validation accuracy
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
is a regular H2O model object and can be treated as such!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
Hyperparameter Tuning in R