增加n_jobs对GridSearchCV没有影响
我设置了一个简单的实验,以在运行sklearnGridSearchCV时检查多核CPU的重要性KNeighborsClassifier。我得到的结果令我感到惊讶,我想知道我是否误解了多核的好处,或者我做得不好。2-8个工作之间的完成时间没有差异。怎么来的 ?我已经注意到“ CPU性能”选项卡上的差异。在第一个单元运行时,CPU使用率约为13%,而最后一个单元则逐渐增加到100%。我期望它能更快完成。也许不是线性地更快,也就是8个工作将比4个工作快2倍,但要快一点。这是我的设置方式:我正在使用jupyter-notebook,单元格是指jupyter-notebook单元格。我已经加载了MNIST,并在中使用0.05了3000数字的测试大小X_play。from sklearn.datasets import fetch_mldatafrom sklearn.model_selection import train_test_splitmnist = fetch_mldata('MNIST original')X, y = mnist["data"], mnist['target']X_train, X_test, y_train, y_test = X[:60000], X[60000:], y[:60000], y[60000:]_, X_play, _, y_play = train_test_split(X_train, y_train, test_size=0.05, random_state=42, stratify=y_train, shuffle=True)在下一个单元格中,我将进行设置KNN并设置一个GridSearchCVfrom sklearn.neighbors import KNeighborsClassifierfrom sklearn.model_selection import GridSearchCVknn_clf = KNeighborsClassifier()param_grid = [{'weights': ["uniform", "distance"], 'n_neighbors': [3, 4, 5]}]然后,我为8个n_jobs值完成了8个单元格。我的CPU是具有4核8线程的i7-4770。grid_search = GridSearchCV(knn_clf, param_grid, cv=3, verbose=3, n_jobs=N_JOB_1_TO_8)grid_search.fit(X_play, y_play)结果Parallel(n_jobs=1)]: Done 18 out of 18 | elapsed: 2.0min finishedParallel(n_jobs=2)]: Done 18 out of 18 | elapsed: 1.4min finishedParallel(n_jobs=3)]: Done 18 out of 18 | elapsed: 1.3min finishedParallel(n_jobs=4)]: Done 18 out of 18 | elapsed: 1.3min finishedParallel(n_jobs=5)]: Done 18 out of 18 | elapsed: 1.4min finishedParallel(n_jobs=6)]: Done 18 out of 18 | elapsed: 1.4min finishedParallel(n_jobs=7)]: Done 18 out of 18 | elapsed: 1.4min finishedParallel(n_jobs=8)]: Done 18 out of 18 | elapsed: 1.4min finished第二次测试随机森林分类器的使用要好得多。测试大小为0.5,30000图片。from sklearn.ensemble import RandomForestClassifierrf_clf = RandomForestClassifier()param_grid = [{'n_estimators': [20, 30, 40, 50, 60], 'max_features': [100, 200, 300, 400, 500], 'criterion': ['gini', 'entropy']}]
查看完整描述