Cross_val_score on Windows 10, error with parallel computing

I ran into 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()

      

Error message:

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 solve this problem?

Added: Code runs 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, another error message appeared:

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

      

Thank!

+3


source to share


1 answer


You can try this, since you are using spyder, it should work fine:

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

      

Hope this helps.

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

EDIT



I changed the last part of your code and it works fine on Windows 8.1.

Also, I am using: Theano backend.

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

      

Screenshot:

enter image description here

+1


source







All Articles