How to do it...

The section will focus on optimizing hyper parameters in H2O using grid searches.

  1. In our case, we will optimize for the activation function, the number of hidden layers (along with the number of neurons in each layer), epochs, and regularization lambda (l1 and l2):
# Perform hyper parameter tuning
activation_opt <- c("Rectifier","RectifierWithDropout", "Maxout","MaxoutWithDropout")
hidden_opt <- list(5, c(5,5))
epoch_opt <- c(10,50,100)
l1_opt <- c(0,1e-3,1e-4)
l2_opt <- c(0,1e-3,1e-4)

hyper_params <- list(activation = activation_opt,
hidden = hidden_opt,
epochs = epoch_opt,
l1 = l1_opt,
l2 = l2_opt)
  1. The following search criteria have been set to perform a grid search. Adding to the following list, one can also specify the type of stopping metric, the minimum tolerance for stopping, and the maximum number of rounds for stopping:
#set search criteria
search_criteria <- list(strategy = "RandomDiscrete", max_models=300)
  1. Now, let's perform a grid search on the training data as follows:
# Perform grid search on training data
dl_grid <- h2o.grid(x = x,
y = y,
algorithm = "deeplearning",
grid_id = "deep_learn",
hyper_params = hyper_params,
search_criteria = search_criteria,
training_frame = occupancy_train.hex,
nfolds = 5)
  1. Once the grid search is complete (here, there are 216 different models), the best model can be selected based on multiple metrics such as logloss, residual deviance, mean squared error, AUC, accuracy, precision, recall, f1, and so on. In our scenario, let's select the best model with the highest AUC:
#Select best model based on auc
d_grid <- h2o.getGrid("deep_learn",sort_by = "auc", decreasing = T)
best_dl_model <- h2o.getModel(d_grid@model_ids[[1]])
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset