I encountered an error when I tried to use cross_val_score with n_job not equal to 1.
My system was Intel-i7 cpu, Windows10, python3.6, Spyder.
Below is my code:
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()
The error message was:
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
The code worked if I set n_jobs=1.
Is there a way to resolve this problem?
Added: The code works on linux virtual machine. I tried with Ubuntu on Virtualbox, anaconda (python 3.6)+ spyder (Tensorflow backend).
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)>
Thank you!