-
-
Notifications
You must be signed in to change notification settings - Fork 25.9k
cross_val_score issue with n_jobs = -1 on Windows #13228
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Put build_classifier in a separate module and import it |
May I ask how this fixes the issue and is this intended behavior? |
@rfernandezv or anyone else having this error, could you please try @jnothman's solution and tell us if this solves the issue? |
For me, this seems to do the trick. My code didn't run through yet (total runtime is many hours), but so far no errors (which was the case before). |
Something can only be unpickled if it can be imported by name. |
This should work with cloudpickle, maybe with the next release of joblib. |
I tried but I still get the same error. from newFile import build_classifier_function
# Evaluating the ANN
classifier = KerasClassifier(build_fn = build_classifier_function, batch_size = 10, epochs = 100)
accuracies = cross_val_score(estimator = classifier, X = X_train, y = y_train, cv = 10, n_jobs = -1) Error: Traceback (most recent call last):
File "<ipython-input-50-cc51c2d2980a>", line 1, in <module>
accuracies = cross_val_score(estimator = classifier, X = X_train, y = y_train, cv = 10, n_jobs = -1)
File "C:\Users\Richar\Anaconda3\lib\site-packages\sklearn\model_selection\_validation.py", line 402, in cross_val_score
error_score=error_score)
File "C:\Users\Richar\Anaconda3\lib\site-packages\sklearn\model_selection\_validation.py", line 240, in cross_validate
for train, test in cv.split(X, y, groups))
File "C:\Users\Richar\Anaconda3\lib\site-packages\sklearn\externals\joblib\parallel.py", line 930, in __call__
self.retrieve()
File "C:\Users\Richar\Anaconda3\lib\site-packages\sklearn\externals\joblib\parallel.py", line 833, in retrieve
self._output.extend(job.get(timeout=self.timeout))
File "C:\Users\Richar\Anaconda3\lib\site-packages\sklearn\externals\joblib\_parallel_backends.py", line 521, in wrap_future_result
return future.result(timeout=timeout)
File "C:\Users\Richar\Anaconda3\lib\concurrent\futures\_base.py", line 432, in result
return self.__get_result()
File "C:\Users\Richar\Anaconda3\lib\concurrent\futures\_base.py", line 384, in __get_result
raise self._exception
BrokenProcessPool: A task has failed to un-serialize. Please ensure that the arguments of the function are all picklable. Updated to version 0.13.2 (joblib) and still have the same error. |
However I don't know why you still have the error when importing from another module. |
I could not reproduce your bug with the latest versiono of For the record, this should work even if you do
8000
not put the |
I tried but the same error :(
Updated to version 0.20.3
|
This is strange. Can you verify that you paste the full error traceback.
There should be an extra indication with `0.20.3` that give you the error
that happened at unpickle. (Above the `BrokenProcessPool`)
Le ven. 8 mars 2019 à 03:14, Richar Marvin Fernandez Vilchez <
notifications@github.com> a écrit :
… I could not reproduce your bug with the latest versiono of sklearn.
Could you update your version to 0.20.2 and report if there is any
changes?
For the record, this should work even if you do not put the
build_classifier function in a new module with newest versions of joblib
as we are relying on cloudpickle.
I tried but the same error :(
BrokenProcessPool: A task has failed to un-serialize. Please ensure that
the arguments of the function are all picklable.
Updated to version 0.20.3
import sklearn print(sklearn.__version__) 0.20.3
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#13228 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ADKs-fW9_A7dw5F-K_fEFqrmIV2yQQIPks5vUcd-gaJpZM4bKNAb>
.
|
Look, this is the complete error: accuracies = cross_val_score(estimator = classifier, X = X_train, y = y_train, cv = 10, n_jobs = -1)
Traceback (most recent call last):
File
8000
"<ipython-input-6-cc51c2d2980a>", line 1, in <module>
accuracies = cross_val_score(estimator = classifier, X = X_train, y = y_train, cv = 10, n_jobs = -1)
File "C:\Users\Richar\Anaconda3\lib\site-packages\sklearn\model_selection\_validation.py", line 402, in cross_val_score
error_score=error_score)
File "C:\Users\Richar\Anaconda3\lib\site-packages\sklearn\model_selection\_validation.py", line 240, in cross_validate
for train, test in cv.split(X, y, groups))
File "C:\Users\Richar\Anaconda3\lib\site-packages\sklearn\externals\joblib\parallel.py", line 930, in __call__
self.retrieve()
File "C:\Users\Richar\Anaconda3\lib\site-packages\sklearn\externals\joblib\parallel.py", line 833, in retrieve
self._output.extend(job.get(timeout=self.timeout))
File "C:\Users\Richar\Anaconda3\lib\site-packages\sklearn\externals\joblib\_parallel_backends.py", line 521, in wrap_future_result
return future.result(timeout=timeout)
File "C:\Users\Richar\Anaconda3\lib\concurrent\futures\_base.py", line 432, in result
return self.__get_result()
File "C:\Users\Richar\Anaconda3\lib\concurrent\futures\_base.py", line 384, in __get_result
raise self._exception
BrokenProcessPool: A task has failed to un-serialize. Please ensure that the arguments of the function are all picklable. |
@rfernandezv I could not reproduce the error either with the following code on windows from keras.layers import Dense
from keras.models import Sequential
from sklearn.model_selection import cross_val_score
from keras.wrappers.scikit_learn import KerasClassifier
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.datasets import make_classification
X, y = make_classification(n_features=11)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=0)
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
def build_classifier():
classifier = Sequential()
classifier.add(Dense(units=6, kernel_initializer='uniform',
activation='relu', input_dim=11))
classifier.add(
Dense(units=6, kernel_initializer='uniform', activation='relu'))
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=10, epochs=100)
accuracies = cross_val_score(
estimator=classifier, X=X_train, y=y_train, cv=10, n_jobs=-1) The versions that I'm using: |
I have the same error with cross_val_score and n_jobs=1 BrokenProcessPool: A task has failed to un-serialize. Please ensure that the arguments of the function are all picklable. Python 3.7.2 |
I've ran into this problem again, in a slightly different form using scikit-learn 0.20.3: Here's a narrowed down version of my code:
And here is the console output including the complete stack trace:
|
@albertcthomas, I ran your code but it does not work Traceback (most recent call last):
File "<ipython-input-5-8e611add1011>", line 2, in <module>
estimator=classifier, X=X_train, y=y_train, cv=10, n_jobs=-1)
File "C:\Users\Richar\Anaconda3\lib\site-packages\sklearn\model_selection\_validation.py", line 402, in cross_val_score
error_score=error_score)
File "C:\Users\Richar\Anaconda3\lib\site-packages\sklearn\model_selection\_validation.py", line 240, in cross_validate
for train, test in cv.split(X, y, groups))
File "C:\Users\Richar\Anaconda3\lib\site-packages\sklearn\externals\joblib\parallel.py", line 930, in __call__
self.retrieve()
File "C:\Users\Richar\Anaconda3\lib\site-packages\sklearn\externals\joblib\parallel.py", line 833, in retrieve
self._output.extend(job.get(timeout=self.timeout))
File "C:\Users\Richar\Anaconda3\lib\site-packages\sklearn\externals\joblib\_parallel_backends.py", line 521, in wrap_future_result
return future.result(timeout=timeout)
File "C:\Users\Richar\Anaconda3\lib\concurrent\futures\_base.py", line 432, in result
return self.__get_result()
File "C:\Users\Richar\Anaconda3\lib\concurrent\futures\_base.py", line 384, in __get_result
raise self._exception
BrokenProcessPool: A task has failed to un-serialize. Please ensure that the arguments of the function are all picklable. |
Hi, I have exactly the same issue like by @rfernandezv Python 3.7.1 |
I have not found the solution yet. @shahabsh92 , did you try to update Sklearn version? |
Has anyone been able to solve this issue, as im getting the same error in conda envt with python 3.7 |
@rfernandezv yes, I created another environment in the Anaconda with the latest version of sklearn 0.20.3 + keras-gpu and it didnt work again. It works only with n_jobs = 1 and nothing else. |
@shahabsh92 you mean that you have the same error as @rfernandezv when you run the code in this comment? |
I feel it's an issue with the ipython console in spyder, i ran the same code in terminal of vscode using the python console and also in jupyter.. It worked. |
I concur, I'm having the same issue with spyder. Restarting the kernel doesn't work... |
@albertcthomas yes exactly. I get both on spyder and Jupiter Errors. Both BrokenProcessPool like @rfernandezv. That's the Error on IPython console of Spyder: `exception calling callback for <Future at 0x2139edbbe10 state=finished raised BrokenProcessPool> The above exception was the direct cause of the following exception: Traceback (most recent call last): File "", line 4, in File "D:\Softwares\Anacoda\lib\site-packages\sklearn\model_selection_validation.py", line 402, in cross_val_score File "D:\Softwares\Anacoda\lib\site-packages\sklearn\model_selection_validation.py", line 240, in cross_validate File "D:\Softwares\Anacoda\lib\site-packages\sklearn\externals\joblib\parallel.py", line 996, in call File "D:\Softwares\Anacoda\lib\site-packages\sklearn\externals\joblib\parallel.py", line 899, in retrieve File "D:\Softwares\Anacoda\lib\site-packages\sklearn\externals\joblib_parallel_backends.py", line 517, in wrap_future_result File "D:\Softwares\Anacoda\lib\concurrent\futures_base.py", line 405, in result File "D:\Softwares\Anacoda\lib\concurrent\futures_base.py", line 357, in __get_result File "D:\Softwares\Anacoda\lib\site-packages\sklearn\externals\joblib\externals\loky_base.py", line 625, in _invoke_callbacks File "D:\Softwares\Anacoda\lib\site-packages\sklearn\externals\joblib\parallel.py", line 375, in call File "D:\Softwares\Anacoda\lib\site-packages\sklearn\externals\joblib\parallel.py", line 797, in dispatch_next File "D:\Softwares\Anacoda\lib\site-packages\sklearn\externals\joblib\parallel.py", line 825, in dispatch_one_batch File "D:\Softwares\Anacoda\lib\site-packages\sklearn\externals\joblib\parallel.py", line 782, in _dispatch File "D:\Softwares\Anacoda\lib\site-packages\sklearn\externals\joblib_parallel_backends.py", line 506, in apply_async File "D:\Softwares\Anacoda\lib\site-packages\sklearn\externals\joblib\externals\loky\reusable_executor.py", line 151, in submit File "D:\Softwares\Anacoda\lib\site-packages\sklearn\externals\joblib\externals\loky\process_executor.py", line 1016, in submit BrokenProcessPool: A task has failed to un-serialize. Please ensure that the arguments of the function are all picklable. Traceback (most recent call last): File "", line 4, in File "D:\Softwares\Anacoda\lib\site-packages\sklearn\model_selection_validation.py", line 402, in cross_val_score File "D:\Softwares\Anacoda\lib\site-packages\sklearn\model_selection_validation.py", line 240, in cross_validate File "D:\Softwares\Anacoda\lib\site-packages\sklearn\externals\joblib\parallel.py", line 996, in call File "D:\Softwares\Anacoda\lib\site-packages\sklearn\externals\joblib\parallel.py", line 899, in retrieve File "D:\Softwares\Anacoda\lib\site-packages\sklearn\externals\joblib_parallel_backends.py", line 517, in wrap_future_result File "D:\Softwares\Anacoda\lib\concurrent\futures_base.py", line 405, in result File "D:\Softwares\Anacoda\lib\concurrent\futures_base.py", line 357, in __get_result File "D:\Softwares\Anacoda\lib\site-packages\sklearn\externals\joblib\externals\loky_base.py", line 625, in _invoke_callbacks File "D:\Softwares\Anacoda\lib\site-packages\sklearn\externals\joblib\parallel.py", line 375, in call File "D:\Softwares\Anacoda\lib\site-packages\sklearn\externals\joblib\parallel.py", line 797, in dispatch_next File "D:\Softwares\Anacoda\lib\site-packages\sklearn\externals\joblib\parallel.py", line 825, in dispatch_one_batch File "D:\Softwares\Anacoda\lib\site-packages\sklearn\externals\joblib\parallel.py", line 782, in _dispatch File "D:\Softwares\Anacoda\lib\site-packages\sklearn\externals\joblib_parallel_backends.py", line 506, in apply_async File "D:\Softwares\Anacoda\lib\site-packages\sklearn\externals\joblib\externals\loky\reusable_executor.py", line 151, in submit File "D:\Softwares\Anacoda\lib\site-packages\sklearn\externals\joblib\externals\loky\process_executor.py", line 1016, in submit BrokenProcessPool: A task has failed to un-serialize. Please ensure that the arguments of the function are all picklable. |
@rfernandezv and @shahabsh92 do you still have the error if you run your script using |
@albertcthomas I tried it in the terminal and I got again an error. I will post the pictures of the Error. Something which I find interesting, is that there are 3 errors and the first error is complaining about the build_classifier and says that it cannot find it (as an AttributeError) |
The errors labeled Traceback (most recent call last):
File "D:\Softwares\Anacoda\lib\site-packages\sklearn\externals\joblib\externals\loky\process_executor.py", line 393, in process_worker
call_item = call_queue.get(block=True, timeout=timeout)
File "D:\Softwares\Anacoda\lib\multiprocessing\queues.py", line 113, in get
return ForkingPickler.loads(res)
AttributeError: Can't get attribute 'build_classifier' on <module '__main__' (build-in)> This is an error with the serialization process for both traceback you showed. (The previous one is because The serialisation process for |
@albertcthomas , I still have the problem, but when I ran it in Visual Studio Code using Jupyter Server everything worked fine. I think the problem is Anaconda. @shahabsh92 you can run it in VS code |
@rfernandezv and @shahabsh92 I cannot reproduce your error when running the code that I posted in the comment above both in cmder and the anaconda prompt in a conda env with the same configuration as @shahabsh92
I'm using conda 4.6.14 |
So still no solution to this problem? I bought a new laptop with an I5 8300H processor, and a GTX 1050 TI MAX-Q, because I thought the problem came from them, but obviously it's not the case. If someone has found a solution to this problem I would very much like to know it. |
@streetr0ck Which versions of Python, scikit-learn and joblib are you using? https://scikit-learn.org/stable/developers/contributing.html#how-to-make-a-good-bug-report |
@rth i'm using : |
@streetr0ck |
@rfernandezv |
@rfernandezv |
Thank you very much @trungnghia2009 for the answer. |
How will n_jobs=-1 will work or will it at all work, if you run your tensorflow backend on gpu? in my opinion, n_jobs=-1 will try and use all the cores for CV and each of tit will try to start a tf session on GPU. In short n_jobs=-1 doesn't work for me, when using GPU |
so you all come from deep learning a-z right? XD |
I am having the same problem. |
For all you deep learning a-z and?? Problem solved in a way. I upgraded joblib from 0.13.2 to joblib 0.14.1 and the program completes using Spyder with Tensor-GPU and n_jobs = -1. However, you do not see all the console output (the animations). conda install joblib=0.14.1 Here are the results, print (accuracies), using joblib 0.13.2 and running from Anaconda command prompt Here are the results, print (accuracies), using joblib 0.14.1 running within Spyder I also updated scikit-learn to 0.22.1, but I feel that really has nothing to do with this working. Edit: to add the mean and variance |
For me, the same error came but it was solved when I removed |
Hi, Just in case is helpful. I had to update Anaconda to 1.9.12 and Spyder to 4.1.3, their current latest versions. Updating joblib and other packages above did not do the trick for me. As other users mentioned, maybe is something with spyder / anaconda. |
Any solution to this issue? I am having the same errors when n_jobs is set to any number greater than 1. I am trying to do cross validation on a Keras model wrapped in sklearn's KerasClassifier. |
Issue still persists, the suggested solution does not fix it. |
Description
The error is thrown when utilizing
n_jobs = -1
with the function: cross_val_score. If I usen_jobs = 1
, it works fine.Steps/Code to Reproduce
Expected Results
Expect my example to run multiple epochs at a time.
Actual Results
Error:
I think the problem is related to Windows, Keras and joblib
Versions
I'm using:
Thanks for the help.
The text was updated successfully, but these errors were encountered: