Skip to content

Error thrown when calling fit on RFECV of a pipeline with n_jobs=-1 in version 0.20.0 #12250

Closed

Description

@rpalmeida

Description

Error thrown when calling fit on RFECV of a pipeline with n_jobs=-1 in version 0.20.0
This wasn't a problem on version 0.19.2 and previous.
It also works when n_jobs in RFECV is not declared or is equal to 1.
Why is a pipeline not pickable?

Steps/Code to Reproduce

# Load libraries
import numpy as np
import pandas as pd
import seaborn as sns
from sklearn import linear_model, datasets
from sklearn.model_selection import GridSearchCV
from sklearn import feature_selection
from sklearn import preprocessing
from sklearn.model_selection import StratifiedKFold
from sklearn.ensemble import ExtraTreesRegressor
from sklearn.pipeline import Pipeline

# Load data
iris = sns.load_dataset("iris")
iris.head()

# 1. Instatiate
le = preprocessing.LabelEncoder()

# 2/3. Fit and transform
X = iris.apply(le.fit_transform)
target = X['species']
del X['species']

#Class defining
class PipelineRFE(Pipeline):

    def fit(self, X, y=None, **fit_params):
        super(PipelineRFE, self).fit(X, y, **fit_params)
        self.feature_importances_ = self.steps[-1][-1].feature_importances_
        return self

#pipeline
pipe = PipelineRFE([
    ('std_scaler', preprocessing.StandardScaler()),
    ("ET", ExtraTreesRegressor(random_state=42, n_estimators=250))
])

# Sets RNG seed to reproduce results
kf = StratifiedKFold(random_state=42)

feature_selector_cv = feature_selection.RFECV(pipe, cv=kf, step=1, scoring="neg_mean_squared_error", n_jobs=-1)
feature_selector_cv.fit(X, target)

selected_features = X.columns.values[feature_selector_cv.support_].tolist()
print(selected_features)

Expected Results

No error is thrown. Prints selected_features.

Actual Results

No handlers could be found for logger "concurrent.futures"
---------------------------------------------------------------------------
BrokenProcessPool                         Traceback (most recent call last)
<ipython-input-11-676fd87a9b51> in <module>()
     10 
     11 feature_selector_cv = feature_selection.RFECV(pipe, cv=10, step=1, scoring="neg_mean_squared_error", n_jobs=-1)
---> 12 feature_selector_cv.fit(X, target)

/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sklearn/feature_selection/rfe.pyc in fit(self, X, y, groups)
    510         scores = parallel(
    511             func(rfe, self.estimator, X, y, train, test, scorer)
--> 512             for train, test in cv.split(X, y, groups))
    513 
    514         scores = np.sum(scores, axis=0)

/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sklearn/externals/joblib/parallel.pyc in __call__(self, iterable)
    994 
    995             with self._backend.retrieval_context():
--> 996                 self.retrieve()
    997             # Make sure that we get a last message telling us we are done
    998             elapsed_time = time.time() - self._start_time

/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sklearn/externals/joblib/parallel.pyc in retrieve(self)
    897             try:
    898                 if getattr(self._backend, 'supports_timeout', False):
--> 899                     self._output.extend(job.get(timeout=self.timeout))
    900                 else:
    901                     self._output.extend(job.get())

/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sklearn/externals/joblib/_parallel_backends.pyc in wrap_future_result(future, timeout)
    515         AsyncResults.get from multiprocessing."""
    516         try:
--> 517             return future.result(timeout=timeout)
    518         except LokyTimeoutError:
    519             raise TimeoutError()

/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sklearn/externals/joblib/externals/loky/_base.pyc in result(self, timeout)
    431                     raise CancelledError()
    432                 elif self._state == FINISHED:
--> 433                     return self.__get_result()
    434                 else:
    435                     raise TimeoutError()

/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sklearn/externals/joblib/externals/loky/_base.pyc in __get_result(self)
    379         def __get_result(self):
    380             if self._exception:
--> 381                 raise self._exception
    382             else:
    383                 return self._result

BrokenProcessPool: A task has failed to un-serialize. Please ensure that the arguments of the function are all picklable.

Versions

System

python: 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 12:39:47)  [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)]

machine: Darwin-17.7.0-x86_64-i386-64bit
executable: /Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python

BLAS

macros: NO_ATLAS_INFO=3, HAVE_CBLAS=None

cblas_libs: cblas
lib_dirs:

Python deps

Cython: None
 scipy: 1.1.0

setuptools: 39.1.0
pip: 18.0
numpy: 1.14.3
pandas: 0.22.0
sklearn: 0.20.0

Activity

  1. added this to the 0.20.1 milestone on Oct 2, 2018
  2. amueller commented on Oct 2, 2018

    @amueller
    Member

    Looks like this has something to do with the use of a custom class (possibly because it's defined in the same file).
    In this case it doesn't look like you need it because the scaling is unnecessary for the tree. But in general this should work (in the sense that we want this to work).

    For the record, this works:

    feature_selector_cv = feature_selection.RFECV( ExtraTreesRegressor(random_state=42, n_estimators=250), cv=kf, step=1, scoring="neg_mean_squared_error", n_jobs=-1)
    feature_selector_cv.fit(X, target)
  3. glemaitre commented on Oct 2, 2018

    @glemaitre
    Member

    possibly because it's defined in the same file

    Moving the the class to another class and importing it will solve the issue as you mentioned.

    Probably @ogrisel @tomMoral will have some insights.

  4. ogrisel commented on Oct 3, 2018

    @ogrisel
    Member

    Thanks @rpalmeida for the detailed report and taking the time to provide a reproduction case.

    Apparently this is caused by a problem of cloudpickle handling metaclasses (it inherits from Pipeline which is a meta class) defined in dynamic modules such as __main__. We will see how to fix that.

  5. ogrisel commented on Oct 3, 2018

    @ogrisel
    Member

    Actually after further investigation cloudpickle is working fine, it's joblib that fails to detect that it's necessary to use cloudpickle in this case. There is a heuristic to try to only use cloudpickle when necessary to avoid incurring unnecessary performance overhead in general.

  6. amueller commented on Oct 3, 2018

    @amueller
    Member

    @ogrisel fixable for 0.20.1?

  7. kimardenmiller commented on Oct 27, 2018

    @kimardenmiller

    UPDATE: Solved: Both Classes AND Functions must be imported into __main__.

    Is my error going to be the same as above?

    import numpy as np
    from sklearn.model_selection import GridSearchCV
    from keras.wrappers.scikit_learn import KerasClassifier
    from LSTM.experiment import Experiment
    from LSTM.models import define_model
    from Utility.Optimize_Grid.opt_grid_utility import \
        load_cv_data, select_scale_cv_data, create_3d_cv_data, fit_and_summarize
    
    # create experiment settings & fix random seed
    np.random.seed(7)
    experiment = Experiment()
    
    
    def create_compiled_model(optimizer):
        # create & compile model
        model = define_model(experiment, inputted_batch_size=experiment.training_batch_size)
        model.compile(loss='mean_squared_error', optimizer=optimizer, metrics=['mean_squared_error', 'accuracy'])
        return model
    
    
    # load, scale, prep & split into input (X) and output (Y) variables
    lag_trim, sma_trim, train_df, val_df = load_cv_data(experiment)
    train_scaled_df, val_scaled_df = select_scale_cv_data(experiment, train_df, val_df)
    X, Y = create_3d_cv_data(experiment, train_scaled_df, lag_trim)
    
    # create model & define grid search parameters
    keras_model = KerasClassifier(build_fn=create_compiled_model, epochs=100, batch_size=270, verbose=0)
    optimizer_list = ['SGD', 'RMSprop', 'Adagrad', 'Adadelta', 'Adam', 'Adamax', 'Nadam']
    param_grid = dict(optimizer=optimizer_list)
    grid = GridSearchCV(cv=3, estimator=keras_model, param_grid=param_grid, n_jobs=-1,
                        scoring='neg_mean_squared_error', iid=False)
    fit_and_summarize(grid, X, Y)
    

    My custom Experiment class is in fact imported.

  8. amueller commented on Oct 28, 2018

    @amueller
    Member

    the "create_compiled_model" function is not, though.

  9. rpalmeida commented on Oct 28, 2018

    @rpalmeida
    Author

    @glemaitre solution of importing the class works for me.
    In the mean time, I analysed the runtimes and it is slower to run the RFECV with n_jobs=-1 than it is with n_jobs=1.
    @ogrisel may this be due to the joblib detection failure you mentioned?

  10. ogrisel commented on Nov 2, 2018

    @ogrisel
    Member

    @rpalmeida yes we are working on a new version of joblib that will always use the cloudpickle pickler by default instead of our broken heuristic.

  11. ivannz commented on Nov 6, 2018

    @ivannz

    @ogrisel and team, thank you for your hard work and exceptional effort you put into scikit!

    Regarding this issue, I found that patching the line 643 of _search.py in model_selection submodule of scikit with explicit regression to backend="multiprocessing" seems to have solved the issue for me, when prototyping in ipython notbook.

           parallel = Parallel(n_jobs=self.n_jobs, backend="multiprocessing",
                                verbose=self.verbose, pre_dispatch=self.pre_dispatch)

    I haven't tested its performance penalties, since it is a temporary hacky fix.

  12. hermidalc commented on Nov 13, 2018

    @hermidalc
    Contributor

    Is it a good idea to expose a backend parameter to GridSearchCV so that users can chose the joblib backend?

  13. amueller commented on Nov 13, 2018

    @amueller
    Member

    @hermidalc you already can configure it afaik.

  14. hermidalc commented on Nov 13, 2018

    @hermidalc
    Contributor
  15. hermidalc commented on Nov 13, 2018

    @hermidalc
    Contributor

    @amueller from

    parallel = Parallel(n_jobs=self.n_jobs, verbose=self.verbose,
    pre_dispatch=self.pre_dispatch)

    it looks like it always just uses whatever the default backend is for the current embedded joblib version

  16. amueller commented on Nov 13, 2018

    @amueller
    Member
  17. hermidalc commented on Nov 13, 2018

    @hermidalc
    Contributor

    Which can be configured:
    https://joblib.readthedocs.io/en/latest/parallel.html#thread-based-parallelism-vs-process-based-parallelism

    Sorry if I'm missing something, when using sklearn how does one specify the backend GridSearchCV will use? There is not GridSearchCV parameter to change the backend.

    I can certainly make a pull request to add this feature

  18. hermidalc commented on Nov 13, 2018

    @hermidalc
    Contributor

    Which can be configured:
    https://joblib.readthedocs.io/en/latest/parallel.html#thread-based-parallelism-vs-process-based-parallelism

    I see in joblib you can establish a joblib.parallel_backend context with statements like:

    with parallel_backend('threading'):

    but this alternative is more annoying then just providing a GridSearchCV backend param that can be set.

  19. ivannz commented on Nov 13, 2018

    @ivannz

    @amueller Thank you! IMO, this context manager solution is really clean and elegant!

  20. amueller commented on Nov 13, 2018

    @amueller
    Member

    I don't see what's wrong with the context manager @hermidalc. We would need to add this option to all sklearn functions and models that use joblib otherwise.

  21. ivannz commented on Nov 15, 2018

    @ivannz

    This is slightly non trivial, but in order to use the context manager for setting parallel backends I had to import from scikit's utils module, instead of joblib. It seems that using the latter does not affect the actually used backend in scikit.

    The reason for explicit backend management arose from the errors during pickling of forward-inverse functions for FunctionTransformer defined in an interactive session in IPython. The errors were much like the ones in the initial post of this issue thread.

    Below is a slightly contrived example that illustrates the details. The root of the problem here is stable_log. If i use np.log instead, all tests return OK. However, if stable_log is used, then only sklearn-multiprocessing pair returns OK, others fail with A task has failed to un-serialize. Please ensure that the arguments of the function are all picklable.

    import numpy as np
    from sklearn.datasets import make_regression
    from sklearn.preprocessing import FunctionTransformer
    from sklearn.ensemble import RandomForestRegressor
    from sklearn.model_selection import GridSearchCV
    from sklearn.pipeline import Pipeline
    
    # toy dataset
    X, y = make_regression(random_state=42)
    X = np.exp(X)
    
    # the real input data might have small negative values -- clip them
    def stable_log(x):
        return np.log(np.maximum(x, 1e-4))
    
    # set up the estimator with preprocessing
    estimator = Pipeline([
        ("txform", FunctionTransformer(stable_log, np.exp,
                                       validate=False, check_inverse=False)),
        ("forest", RandomForestRegressor(random_state=123)),
    ])
    
    cv_results = GridSearchCV(estimator, [{
        "forest__n_estimators": [10, 100],
        "forest__min_samples_split": [5, 25],
        "forest__max_features": [0.50, 1.00],
    }], cv=7, n_jobs=2, return_train_score=False, iid=False)
    
    ## >>>> Test starts HERE
    # import managers from different libs
    from sklearn.utils import parallel_backend as sk_pb
    from joblib import parallel_backend as jl_pb
    
    for lib, ctx, back in [("joblib", jl_pb, "multiprocessing"),
                           ("sklearn", sk_pb, "multiprocessing"),
                           ("joblib", jl_pb, "loky"),
                           ("sklearn", sk_pb, "loky")]:
        with ctx(backend=back):
            status = "OK"
            try:
                cv_results.fit(X, y)
            except Exception as e:
                status = f"{e}"
                # raise
        # end with
    
        print(f"backend {lib}-{back}: {status}")
    # end for
  22. amueller commented on Nov 15, 2018

    @amueller
    Member

    try either updating to the development version of scikit-learn or defining the function in a separate file. Either should fix the error. There's an active discussion on how to best handle the issue of importing from utils vs joblib in the near future in #12345

  23. avriiil commented on May 15, 2021

    @avriiil

    Which can be configured:
    https://joblib.readthedocs.io/en/latest/parallel.html#thread-based-parallelism-vs-process-based-parallelism

    Had the same error today, running sklearn v.0.24. Configuring the joblib backend with a context manager as mentioned above solved it for me. thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions