Windows10上的cross_val_score,并行计算错误

当我尝试使用与n_job不等于1的cross_val_score时遇到错误 .

我的系统是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,代码就有效 .

有没有办法解决这个问题?

补充:代码适用于linux虚拟机 . 我在Virtualbox,anaconda(python 3.6)spyder(Tensorflow后端)上尝试使用Ubuntu .

补充:我在pycharm中尝试了相同的代码,出现了不同的错误消息:

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

谢谢!

回答(1)

2 years ago

你可以尝试这个,因为你使用spyder它应该工作正常:

Code

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的类似问题通过我的帖子解决link

EDIT

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

另外,我使用:Theano后端 .

Changed part:

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()

截图:

enter image description here