我需要对RH2o上的gbm模型进行参数优化。我对H2o比较陌生,我认为在执行下面的操作之前,我需要将ntree和learn_rate(下面)转换为H2o向量。我该怎么做这个手术?谢谢!
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))))
}
}发布于 2016-10-12 23:30:09
您可以使用h2o.grid()进行网格搜索。
# 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)您可以阅读更多关于h2o.grid()如何在R文档package.pdf中工作的内容。
发布于 2016-10-13 07:49:56
使用网格的Lauren's answer是这里最好的。我将很快地指出,您所编写的是一种有用的方法,当网格不做您需要的事情时,您可以依靠它。
您的示例没有包含任何数据(请参阅https://stackoverflow.com/help/mcve),因此我无法运行它,但是我纠正了我注意到的两个语法问题(R的for-in循环直接给出了值,而不是索引,第二个for循环的圆括号):
ntrees <- c(100,200,300,400)
learn_rate <- c(1,0.5,0.1)
for (n in ntrees){
for (l in learn_rate){
gbm_model <- h2o.gbm(
features, label, training_frame = train, validation_frame = valid,
ntrees = n,max_depth = 5,learn_rate = l
)
print(c(n,l,h2o.mse(h2o.performance(gbm_model, valid = TRUE))))
}
}在何时使用嵌套循环的一个例子是,当您想要跳过某些组合时。例如,您可能决定只测试100的ntree,其学习速率为0.1,然后如下所示:
ntrees <- c(100,200,300,400)
learn_rate <- c(1,0.5,0.1)
for (n in ntrees){
for (l in learn_rate){
if(l == 0.1 && n > 100)next #Skip when n is 200,300,400
gbm_model <- h2o.gbm(
features, label, training_frame = train, validation_frame = valid,
ntrees = n,max_depth = 5,learn_rate = l
)
print(c(n,l,h2o.mse(h2o.performance(gbm_model, valid = TRUE))))
}
}https://stackoverflow.com/questions/40006311
复制相似问题