Windows10上的cross_val_score,并行计算错误

时间:2017-07-08 04:05:18

标签: python python-3.x tensorflow scikit-learn keras

当我尝试将 cross_val_score n_job 不等于1时,我遇到错误。

我的系统是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上使用Ubuntu,anaconda(python 3.6)+ spyder(Tensorflow后端)。

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

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

谢谢!

1 个答案:

答案 0 :(得分:1)

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

<强>代码

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

希望这会有所帮助。

我的帖子link

解决了spyder和n_jobs的类似问题

修改

我修改了代码的最后一部分,它在Windows 8.1上运行良好。

另外,我使用:Theano后端。

更改了部分

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