这是一个合理的请求,目前在 H2O 的网格搜索功能中没有办法做到这一点,但是我已经创建了一张票 here。还有另一个 ticket open 用于类似的请求,即在您继续运行网格搜索时将“前 k”模型保留在网格中(并删除其余模型)。
我们已通过 keep_cross_validation_models 参数为 H2O AutoML 实现了此功能,因此如果您愿意使用 H2O AutoML(主要是 GBM),您可以使用它来代替随机森林网格。如果将此参数设置为FALSE,则将删除 CV 模型,但是当前实现的唯一 problem 是在 AutoML 运行结束时删除 CV 模型,而不是在创建和 CV 后立即删除指标被保存。
因此,与此同时,为了解决问题,我建议以下方法:
您可以通过使用grid_id 参数多次执行网格。每次执行后,您可以手动删除 CV 模型。然后您可以再次“训练”网格并将grid_id 设置为与以前相同,它将向同一网格添加更多模型。如果您使用笛卡尔网格搜索,则应更改搜索空间,如果您使用随机网格搜索,则只需更改种子,以便第二次获得不同/新模型。它是手动的,但它仍然比编写循环并从头开始创建网格要容易一些。
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