缩放sklearn RandomForestClassifier用于RandomizedSearchCV

时间:2019-03-27 15:53:34

标签: python scikit-learn random-forest hpc

我正在具有28个CPU和约190GB RAM的单个群集节点上训练sklearn.ensemble.RandomForestClassifier()。单独训练此分类器运行速度非常快,使用了机器上的所有内核并使用了约93GB的RAM:

x_train,x_test,y_train,y_test=sklearn.model_selection.train_test_split(x,y,test_size=.25,random_state=0)

clf=sklearn.ensemble.RandomForestClassifier(n_estimators=100,
                                            random_state=0,
                                            n_jobs=-1,
                                            max_depth=10,
                                            class_weight='balanced',
                                            warm_start=False,
                                            verbose=2)
clf.fit(x_train,y_train)

输出:

[Parallel(n_jobs=-1)]: Done  88 out of 100 | elapsed:  1.9min remaining:   15.2s
[Parallel(n_jobs=-1)]: Done 100 out of 100 | elapsed:  2.0min finished
CPU times: user 43min 10s, sys: 1min 33s, total: 44min 44s
Wall time: 2min 20s

但是,这种特定的模型似乎不是最佳的,性能约80%正确。因此,我想使用sklearn.model_selection.RandomizedSearchCV()为模型优化超参数。所以我像这样设置搜索:

rfc = sklearn.ensemble.RandomForestClassifier()
rf_random = sklearn.model_selection.RandomizedSearchCV(estimator=rfc, 
                                                       param_distributions=random_grid, 
                                                       n_iter=100, 
                                                       cv=3, 
                                                       verbose=2, 
                                                       random_state=0, 
                                                       n_jobs=2, 
                                                       pre_dispatch=1)
rf_random.fit(x, y)

但是我找不到有效使用硬件的n_jobspre_dispatch的设置。这是一些示例运行和结果:

n_jobs   pre_dispatch    Result
===========================================================================
default       default    Utilizes all cores but Job killed - out of memory
    -1              1    Job killed - out of memory
    12              1    Job killed - out of memory
     3              1    Job killed - out of memory
     2              1    Job runs, but only utilizes 2 cores, takes >230min (wall clock) per model

如何在运行超参数搜索时训练独立的RandomForestClassifier时看到的性能?以及独立版本如何并行化,以使其不像网格搜索那样创建大型数据集的副本?


编辑: 以下参数组合有效地利用了所有内核来训练每个单独的RandomForestClassifier,而无需并行化超参数搜索本身或消耗RAM使用量。

model = sklearn.ensemble.RandomForestClassifier(n_jobs=-1, verbose=1)
search = sklearn.model_selection.RandomizedSearchCV(estimator=model, 
                                                    param_distributions=random_grid, 
                                                    n_iter=10, 
                                                    cv=3, 
                                                    verbose=10, 
                                                    random_state=0,
                                                    n_jobs=1,
                                                    pre_dispatch=1)
with joblib.parallel_backend('threading'):
    search.fit(x, y)

1 个答案:

答案 0 :(得分:1)

如果单个分类器训练使您的所有核心都饱和,那么通过并行化gridsearch也没有任何好处。为gridsearch设置n_jobs = 1,并为分类器保留n_jobs = -1。 这样可以避免内存不足的情况。