一种在 GridSearch 中删除交叉验证模型的方法?

A way to drop cross validation models in GridSearch?

我在 Jupyter notebook 中用 H2O 构建随机森林模型时 运行 记不住了。我发现使用我的 20 GB 内存实例,我可以在抛出 "tree model will not fit in driver node's memory" 异常之前构建大约两个具有 10 折交叉验证的 50 树模型(总共 22 个模型)。使用 for 循环,我可以在计算和显示指标后删除交叉验证模型,但是使用 GridSearch,似乎没有任何方法可以在搜索时删除无关的 CV 模型。是这样吗?有什么解决方法吗? (我总是可以增加分配的内存量,但最终在我的本地机器上将是一个有限值 运行)。有没有人对 GridSearch 和有限内存有任何提示?谢谢。

这是一个合理的请求,目前在 H2O 的网格搜索功能中没有办法做到这一点,但是我已经为类似的请求创建了一个票证 here. There is another ticket open,即保留 "top k" 在继续 运行 网格搜索时从网格中删除模型(并删除其余部分)。

我们已经为 H2O AutoML via the keep_cross_validation_models argument, so if you're open to using H2O AutoML (which would be mostly GBMs), you could use that instead of a Random Forest grid. If you set this argument to FALSE, then CV models will be deleted, however the only problem 实现了此功能,当前的实现是在 AutoML 运行 结束时删除 CV 模型,而不是在创建它们并保存 CV 指标后立即删除.

因此,与此同时,为了解决这个问题,我建议您采取以下措施:

您可以使用 grid_id 参数多次执行网格。每次执行后,您可以手动删除 CV 模型。然后你可以再次 "train" 网格并设置 grid_id 与之前相同,它会添加更多模型到同一个网格。如果您使用的是笛卡尔网格搜索,则应更改搜索 space,如果您使用的是随机网格搜索,则只需更改种子即可第二次获得 different/new 个模型.它是手动的,但它仍然比编写循环并从头开始创建网格要容易一些。

Python 示例:

import h2o
from h2o.estimators.random_forest import H2ORandomForestEstimator
from h2o.grid.grid_search import H2OGridSearch

h2o.init()

# Import a sample binary outcome training set into H2O
train = h2o.import_file("https://s3.amazonaws.com/erin-data/higgs/higgs_train_10k.csv")
x = train.columns
y = "response"
x.remove(y)

# For binary classification, response should be a factor
train[y] = train[y].asfactor()

# RF hyperparameters
rf_params = {'max_depth': list(range(5, 30)),
             'sample_rate': [i * 0.1 for i in range(5, 11)],
             'min_rows': list(range(1, 25))}

# Search criteria
search_criteria = {'strategy': 'RandomDiscrete', 'max_models': 20}

rf_grid = H2OGridSearch(model=H2ORandomForestEstimator,
                        grid_id='rf_grid',
                        hyper_params=rf_params,
                        search_criteria=search_criteria)
rf_grid.train(x=x, y=y, 
              training_frame=train, 
              nfolds=5, 
              ntrees=300,
              seed=1)

# Code to delete CV models (you'll have to do this part)

rf_grid.train(x=x, y=y, 
              training_frame=train, 
              nfolds=5, 
              ntrees=300,
              seed=2)  #change seed for second random grid search run