如何加速cross_val_score?

how to speed up cross_val_score?

我想使用 sklearn.model_selection.cross_val_score 在 MNIST 数据集上评估 SGDClassifier。 3折我花了大约6分钟。 我怎样才能使用完整的系统电源来加速这个过程(我的意思是使用从 CPU 到显卡等的所有东西) 顺便说一句,我一直在监控 CPU 使用情况,它只使用了 54% 的电量。

from sklearn.datasets import fetch_openml
from sklearn.linear_model import SGDClassifier
from sklearn.model_selection import cross_val_score

mnist = fetch_openml('mnist_784')
X, y = mnist['data'], mnist['target']
X_train, X_test, y_train, y_test = X[:60000], X[60000:], y[:60000], y[60000:]
y_train_5 = (y_train == 5)
y_test_5 = (y_test == 5)

sgd_clf = SGDClassifier(random_state=42)
sgd_clf.fit(X_train, y_train)

cross_val_score(sgd_clf, X_train, y_train, cv=3, scoring='accuracy')

来自docs

n_jobs : int or None, optional (default=None)

The number of CPUs to use to do the computation. None means 1 unless in a joblib.parallel_backend context. -1 means using all processors.

即您可以使用所有可用的内核

cross_val_score(sgd_clf, X_train, y_train, cv=3, scoring='accuracy', n_jobs=-1)

或指定一些其他值 n_jobs=k,如果使用所有内核会使您的机器变慢或无响应。

这将使用更多 CPU 个内核;据我所知,scikit-learn 中没有将计算卸载到 GPU 的功能。