4

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!

Code Learner
  • 191
  • 11
  • Can you provide a mini code-sample in order for possible reader to be able to reproduce your error? – Kruupös Jul 08 '17 at 09:45
  • On Linux you should not have this problem. I posted a possible solution for windows. – seralouk Jul 08 '17 at 12:40
  • @sera Thank you. I tried and it works on Linux (I tried on Ubuntu on Virtualbox). I would use this as a solution for now. – Code Learner Jul 09 '17 at 16:22
  • @WestYang okay. But still it is weird that it does not work for you in pycharm. It works fine for me. I use windows 8 and sklearn 0.18 and Theano backend as I told you. Maybe you can try to re-install sklearn and keras and try my solution again – seralouk Jul 09 '17 at 16:51

1 Answers1

1

You can try this since you use 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 problem with spyder and n_jobs solved by my post here link

EDIT

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

Also, I use: 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

seralouk
  • 30,938
  • 9
  • 118
  • 133
  • Is `class` object necessary to overcome the problem? Why not wrap it around a simple method? Also, you should add `print(accurasies)` inside a wrapper, otherwise you will not receive any output. – E.Z Jul 08 '17 at 16:52
  • @E.Z. It's not necessary but this has solved similar problem with n_jobs and spyder. I think it's worth a try.lets wait feedback from the person who asked the question – seralouk Jul 08 '17 at 16:53
  • @sera I'm still seeing the same error in Spyder. I also tried in pycharm, which yielded a different error message, please refer to the bottom of my edited post. I edited your code to give me return, while I also tried your code, which yielded the same error as mine (in Spyder/pycharm). – Code Learner Jul 09 '17 at 06:33
  • @WestYang I just modified a part of you code and it runs fine from console and using Pycharm on Windows 8. – seralouk Jul 09 '17 at 09:53
  • @sera The same problem persisted on my laptop. I was using Tensorflow backend. I tried to change my backend to Theano, but I countered further errors with m2w64-toolchain... I guess the Tensorflow backend maybe what is causing the problem. If possible, can you switch to Tensorflow backend and see if you will get the error? Thank you! – Code Learner Jul 09 '17 at 12:47
  • @WestYang yes I will change the backend and I will let you know. – seralouk Jul 09 '17 at 13:56
  • how would I do this in tensorflow backend? – Shubham Agarwal Bhewanewala Jul 22 '18 at 20:30
  • @sera Did it work with Tensorflow backend? thank you! – Danny Nov 06 '18 at 12:36