RandomizedSearchCV可以在多个CPU上运行吗?

时间:2015-04-30 15:19:34

标签: python parallel-processing machine-learning scikit-learn

我正在尝试使用RandomizedSearchCV来学习逻辑回归的超参数。代码如下:

random_searcher = RandomizedSearchCV(
  estimator = MyLogRegClassifier(),
  param_distributions = {'penalty': ['l2', 'l1'], 
                         'class_weight': [None, 'auto'],
                         'C': logspace(-20, 20, 10000), 
                         'intercept_scaling': logspace(-20, 20, 10000)},
  cv = 4,
  scoring = make_scorer(f1_score, average='samples'),
  n_iter = 100,
  n_jobs = -1,
  pre_dispatch = 10,
  refit = False
)
random_searcher.fit(X_tr, y_tr)

然而,它无法使用多个CPU,它始终在一个CPU上运行。但是当我切换到GridSearchCV时,它会生成多个进程并加载所有CPU。

grid_searcher = GridSearchCV(
  estimator = MyLogRegClassifier(),
  param_grid = {'penalty': ['l2', 'l1'], 
                'class_weight': [None, 'auto'],
                'C': logspace(-20, 20, 10000), 
                'intercept_scaling': logspace(-20, 20, 10000)},
  cv = 4,
  scoring = make_scorer(f1_score, average='samples'),
  n_jobs = -1,
  pre_dispatch = 10,
  refit = False
)
grid_searcher.fit(X_tr, y_tr)

X_tr是具有形状的稀疏矩阵(6700,25640),y_tr是具有形状的密集矩阵(6700,83) MyLogRegClassifier基本上是OneVsRestClassifier(LogisticRegression())

class MyLogRegClassifier(BaseEstimator, ClassifierMixin):
    def __init__(self, C=1.0, penalty='l2', 
                 intercept_scaling=1, class_weight=None, n_jobs=1):
        self.C = C
        self.n_jobs = n_jobs
        self.penalty = penalty
        self.intercept_scaling = intercept_scaling
        self.class_weight = class_weight

    def fit(self, X, y):
        self.clf = OneVsRestClassifier(LogisticRegression(
            C=self.C, penalty=self.penalty,
            intercept_scaling=self.intercept_scaling, 
            class_weight=self.class_weight),
                                       n_jobs=self.n_jobs)
        self.clf.fit(X, y)
        return self

    def predict(self, X):
        return self.clf.predict(X)

我错过了什么吗?

0 个答案:

没有答案