使用深度学习网格构建集合模型时H2O:NullPointerException错误

时间:2017-06-26 00:46:16

标签: java r deep-learning h2o

我正在尝试使用R(版本3.3.3)和h2o(版本3.10.5.1)中的深度学习来构建堆叠集合模型来预测商家流失。响应变量是二进制的。目前我正在尝试运行代码,使用网格搜索开发的前5个模型构建堆叠集合模型。但是,当代码运行时,我得到java.lang.NullPointerException错误,输出如下:

java.lang.NullPointerException
    at hex.StackedEnsembleModel.checkAndInheritModelProperties(StackedEnsembleModel.java:265)
    at hex.ensemble.StackedEnsemble$StackedEnsembleDriver.computeImpl(StackedEnsemble.java:115)
    at hex.ModelBuilder$Driver.compute2(ModelBuilder.java:173)
    at water.H2O$H2OCountedCompleter.compute(H2O.java:1349)
    at jsr166y.CountedCompleter.exec(CountedCompleter.java:468)
    at jsr166y.ForkJoinTask.doExec(ForkJoinTask.java:263)
    at jsr166y.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:974)
    at jsr166y.ForkJoinPool.runWorker(ForkJoinPool.java:1477)
    at jsr166y.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:104)

下面是我用来进行超参数网格搜索并构建整体模型的代码:

hyper_params <- list(
                  activation=c("Rectifier","Tanh","Maxout","RectifierWithDropout","TanhWithDropout","MaxoutWithDropout"),
                  hidden=list(c(50,50),c(30,30,30),c(32,32,32,32,32),c(64,64,64,64,64),c(100,100,100,100,100)),
                  input_dropout_ratio=seq(0,0.2,0.05),
                  l1=seq(0,1e-4,1e-6),
                  l2=seq(0,1e-4,1e-6),
                  rho = c(0.9,0.95,0.99,0.999),
                  epsilon=c(1e-10,1e-09,1e-08,1e-07,1e-06,1e-05,1e-04)
                )

search_criteria <- list(
                      strategy = "RandomDiscrete",
                      max_runtime_secs = 3600,
                      max_models = 100,
                      seed=1234,
                      stopping_metric="misclassification",      
                      stopping_tolerance=0.01,                  
                      stopping_rounds=5
                    )

dl_ensemble_grid <- h2o.grid(
                          hyper_params = hyper_params,
                          search_criteria = search_criteria,
                          algorithm="deeplearning",
                          grid_id = "final_grid_ensemble_dl",
                          x=predictors,
                          y=response,
                          training_frame = h2o.rbind(train, valid, test),
                          nfolds=5,
                          fold_assignment="Modulo",
                          keep_cross_validation_predictions = TRUE,
                          keep_cross_validation_fold_assignment = TRUE,
                          epochs=12,
                          max_runtime_secs = 3600,
                          stopping_metric="misclassification",
                          stopping_tolerance=0.01,        
                          stopping_rounds=5,
                          seed = 1234,
                          max_w2=10
                        )           

DLsortedGridEnsemble_logloss <- h2o.getGrid("final_grid_ensemble_dl",sort_by="logloss",decreasing=FALSE)

ensemble <- h2o.stackedEnsemble(x = predictors, 
                            y = response, 
                            training_frame = h2o.rbind(train,valid,test), 
                            base_models = list(                                                   
                                                DLsortedGridEnsemble_logloss@model_ids[[1]],
                                                DLsortedGridEnsemble_logloss@model_ids[[2]],
                                                DLsortedGridEnsemble_logloss@model_ids[[3]],
                                                DLsortedGridEnsemble_logloss@model_ids[[4]],
                                                DLsortedGridEnsemble_logloss@model_ids[[5]],
                                              )

注意:到目前为止我所意识到的是,当只有一个基本模型时,h2o.stackedEnsemble函数会工作,并且只要有两个或更多基本模型就会出现Java错误。

如果我能得到一些关于如何解决这个问题的反馈,我将非常感激。

1 个答案:

答案 0 :(得分:2)

错误是指StackedEnsembleModel.java code的一行,用于检查基本模型中的training_frametraining_frame中的h2o.stackedEnsemble()是否具有相同的校验和。我认为问题是由于您动态创建了训练框架而不是明确定义它(即使应该工作,因为它最终是相同的数据)。因此,不是在网格和集合函数中设置training_frame = h2o.rbind(train, valid, test),而是在代码顶部设置以下内容:

df <- h2o.rbind(train, valid, test)

然后在网格和合奏函数中设置training_frame = df

作为旁注,如果您使用验证框架(用于提前停止),您可能会获得更好的DL模型,而不是将所有数据用于训练框架。此外,如果您想使用网格中的所有模型(可能会带来更好的性能,但并非总是如此),您可以在base_models = DLsortedGridEnsemble_logloss@model_ids函数中设置h2o.stackedEnsemble()