如何在 R 中编译神经网络的网格搜索



我正在尝试为我想在 R 中创建的神经网络找到最佳参数。我正在使用 h2o 包并按照 https://www.kaggle.com/wti200/deep-neural-network-parameter-search-r/comments 中的教程进行操作

我拥有的代码似乎在 1 分钟内运行,据我所知,网格搜索应该运行多个模型,直到确定最佳参数并且需要一段时间才能运行。请让我知道我哪里出错了,以及如何进行围栏搜索以优化我的参数。

h2o.init(nthreads=-1,max_mem_size='6G')
testHex = as.h2o(test)
trainHex = as.h2o(training)
predictors <-colnames(training)[!(colnames(training) %in% c("responseVar"))]
response = "responseVar"
hyper_params <- list(
activation=c("Rectifier","Tanh","Maxout","RectifierWithDropout","TanhWithDropout","MaxoutWithDropout"),
hidden=list(c(20,20),c(50,50),c(75,75),c(100,100),c(30,30,30),c(25,25,25,25)),
input_dropout_ratio=c(0,0.03,0.05),
#rate=c(0.01,0.02,0.05),
l1=seq(0,1e-4,1e-6),
l2=seq(0,1e-4,1e-6)
)
h2o.rm("dl_grid_random")
search_criteria = list(strategy = "RandomDiscrete", max_runtime_secs = 360, max_models = 100, seed=1234567, stopping_rounds=5, stopping_tolerance=1e-2)
dl_random_grid <- h2o.grid(
algorithm="deeplearning",
grid_id = "dl_grid_random",
training_frame=trainHex,
x=predictors, 
y=response,
epochs=1,
stopping_metric="RMSE",
stopping_tolerance=1e-2,        ## stop when logloss does not improve by >=1% for 2 scoring events
stopping_rounds=2,
score_validation_samples=10000, ## downsample validation set for faster scoring
score_duty_cycle=0.025,         ## don't score more than 2.5% of the wall time
max_w2=10,                      ## can help improve stability for Rectifier
hyper_params = hyper_params,
search_criteria = search_criteria
)                            
grid <- h2o.getGrid("dl_grid_random",sort_by="mae",decreasing=FALSE)
grid
grid@summary_table[1,]
best_model <- h2o.getModel(grid@model_ids[[1]]) ## model with lowest logloss
best_model

您已在网格设置中设置了通过search_criteriamax_runtime_secs = 360,因此它可能运行的最长时间为 6 分钟。

如果网格在此之前停止,则意味着您的提前停止设置正在触发网格提前停止。 如果您希望它运行更长时间,那么您可以增加网格的stopping_rounds和/或增加stopping_tolerance并且它应该对提前停止和运行更长时间不太敏感(在上面的代码中,您将它们分别设置为51e-2)。 您可能还需要设置stopping_metric = "RMSE",因为回归的默认值是平均残差偏差 ("deviance")。 有关停止指标的更多信息,请参阅用户指南。

我注意到您已经为各个 DNN 模型提供了stopping_rounds = 2(如果您的模型拟合不足,您可能需要尝试增加该值)。

最新更新