本文介绍了Windows10上的cross_val_score,并行计算错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试在 n_job 不等于1的情况下使用 cross_val_score 时遇到错误.

I encountered an error when I tried to use cross_val_score with n_job not equal to 1.

我的系统是Intel-i7 cpu,Windows10,python3.6,Spyder.

My system was Intel-i7 cpu, Windows10, python3.6, Spyder.

下面是我的代码:

from numpy.random import randn
import pandas as pd
from keras.wrappers.scikit_learn import KerasClassifier
from sklearn.model_selection import cross_val_score
from keras.models import Sequential
from keras.layers import Dense

# build a data set
dataset = pd.DataFrame(randn(100, 2), columns='X1 X2'.split())
dataset["Y"]=dataset["X1"]+dataset["X2"]

# seperate X and y
X = dataset.iloc[:, 0:2].values
Y = dataset.iloc[:, 2].values

# define classifier
def build_classifier():
    classifier = Sequential()
    classifier.add(Dense(units = 2, kernel_initializer = 'uniform', activation = 'relu', input_dim = 2))
    classifier.add(Dense(units = 1, kernel_initializer = 'uniform', activation = 'sigmoid'))
    classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])
    return classifier
classifier = KerasClassifier(build_fn = build_classifier, batch_size = 1, epochs = 4)

class testnjob():
    def run():
        accuracies = cross_val_score(estimator = classifier, X = X, y = Y, cv = 5, n_jobs = -1)
        return(accuracies)
if __name__ == '__main__':
    accuracies = testnjob.run()

错误消息是:

ImportError: [joblib] Attempting to do parallel computing without protecting
your import on a system that does not support forking. To use parallel-
computing in a script, you must protect your main loop using
"if __name__ == '__main__'". Please see the joblib documentation on Parallel
for more information

如果我设置 n_jobs = 1 ,则代码有效.

The code worked if I set n_jobs=1.

有没有解决此问题的方法?

Is there a way to resolve this problem?

已添加:该代码可在linux虚拟机上使用.我在Virtualbox,anaconda(python 3.6)+ spyder(Tensorflow后端)上使用Ubuntu进行了尝试.

Added: The code works on linux virtual machine. I tried with Ubuntu on Virtualbox, anaconda (python 3.6)+ spyder (Tensorflow backend).

添加:我在pycharm中尝试了相同的代码,但显示了不同的错误消息:

Added: I tried the same code in pycharm, a different error message showed up:

AttributeError: Can't get attribute 'build_classifier' on
<module '__main__' (built-in)>

谢谢!

推荐答案

您可以尝试使用此功能,因为使用spyder可以正常工作:

You can try this since you use spyder it should work fine :

代码

import...

Class Test(object):
    def __init__(self):
        accuracies = cross_val_score(estimator = classifier, X = X_train, y = y_train, cv = 10, n_jobs = -1)
        ###code here
        ###code here

if __name__ == '__main__':
    Test()

希望这会有所帮助.

与spyder和n_jobs类似的问题已通过我的帖子链接

Similar problem with spyder and n_jobs solved by my post here link

编辑

我修改了代码的最后一部分,它在Windows 8.1上可以正常运行.

I modified the last part of your code and it runs fine on Windows 8.1.

我还使用:Theano后端.

Also, I use: Theano backend.

更改的部分:

from numpy.random import randn
...
...
classifier = KerasClassifier(build_fn = build_classifier, batch_size = 1, epochs = 4)

####################################################################
#changed part from here

class run():
    def __init__(self):
        cross_val_score(estimator = classifier, X = X, y = Y, cv = 5, n_jobs = -1)


if __name__ == '__main__':
    run()

屏幕截图:

这篇关于Windows10上的cross_val_score,并行计算错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 15:09