Vignesh Ramesh Vignesh Ramesh - 3 months ago 37
R Question

Parameter optimization in R and H2O

I need to perform parameter optimization on a gbm model on RH2o. I am relatively new to H2o and I think I need to convert ntrees and learn_rate(below) into a H2o vector before performing the below.
How do I perform this operation?

ntrees <- c(100,200,300,400)
learn_rate <- c(1,0.5,0.1)
for (i in ntrees){
for j in learn_rate{
n = ntrees[i]
l= learn_rate[j]
gbm_model <- h2o.gbm(features, label, training_frame = train, validation_frame = valid, ntrees=ntrees[[i]],max_depth = 5,learn_rate=learn_rate[j])
print(c(ntrees[i],learn_rate[j],h2o.mse(h2o.performance(gbm_model, valid = TRUE))))



you can use h2o.grid() to do your grid search

# specify your hyper parameters
hyper_params = list( ntrees = c(100,200,300,400), learn_rate =  c(1,0.5,0.1) )

# then build your grid
grid <- h2o.grid(
## hyper parameters
hyper_params = hyper_params,

## which algorithm to run
algorithm = "gbm",

## identifier for the grid, to later retrieve it
grid_id = "my_grid", 

## standard model parameters
x = features, 
y = label, 
training_frame = train, 
validation_frame = valid,

## set a seed for reproducibility
seed = 1234)

you can read more about how h2o.grid() works in the R documentation