question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

RuntimeError: "Cannot clone object ..." when using clone with an implementation of BaseEstimator that copies objects in get_params method

See original GitHub issue

Description

RuntimeError thrown when using sklearn.base.clone with estimators whose implementation of get_params returns copies instead of references.

Sanity check at the end of the clone function fails when the implementation of the estimator used, copies parameters during its initialisation or in the get_params method.

Either the documentation of __init__ and get_params in BaseEstimator should indicate that parameters should never be copied or the sanity check in clone should be more flexible.

In the case that implementations of estimators should not copy parameters, an issue should be created in the Keras project regarding the 'get_params`method of the classes used for integration with scikit-learn.

Steps/Code to Reproduce

from keras.wrappers.scikit_learn import KerasClassifier
from keras.models import Sequential
from keras.layers import Dense
 
from sklearn.base import clone
 
def create_keras_classifier_model(n_classes):
    """Keras multinomial logistic regression creation model
 
    Args:
        n_classes(int): Number of classes to be classified
 
    Returns:
        Compiled keras model
 
    """
    # create model
    model = Sequential()
    model.add(Dense(n_classes, activation="softmax"))
    # Compile model
    model.compile(
        loss="categorical_crossentropy", optimizer="adam", metrics=["accuracy"]
    )
    return model
 
estimator = KerasClassifier(build_fn=create_keras_classifier_model, n_classes=2, class_weight={0: 1, 1:3})
 
clone(estimator)

Expected Results

No error is thrown.

Actual Results

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-1-520f4ee6e745> in <module>
     26 estimator = KerasClassifier(build_fn=create_keras_classifier_model, n_classes=2, class_weight={0: 1, 1:3})
    27
---> 28 clone(estimator)
 
/usr/local/anaconda/envs/ivan/lib/python3.6/site-packages/sklearn/base.py in clone(estimator, safe)
     73             raise RuntimeError('Cannot clone object %s, as the constructor '
     74                                'either does not set or modifies parameter %s' %
---> 75                                (estimator, name))
     76     return new_object
     77
 
RuntimeError: Cannot clone object <keras.wrappers.scikit_learn.KerasClassifier object at 0x7f7504148f28>, as the constructor either does not set or modifies parameter class_weight

Versions

System: python: 3.6.9 |Anaconda, Inc.| (default, Jul 30 2019, 19:07:31) [GCC 7.3.0] executable: /usr/local/anaconda/envs/ivan/bin/python machine: Linux-3.10.0-957.21.3.el7.x86_64-x86_64-with-redhat-7.6-Maipo

BLAS: macros: SCIPY_MKL_H=None, HAVE_CBLAS=None lib_dirs: /usr/local/anaconda/envs/ivan/lib cblas_libs: blas, cblas, lapack, pthread, blas, cblas, lapack, blas, cblas, lapack

Python deps: pip: 19.2.3 setuptools: 41.4.0 sklearn: 0.20.3 numpy: 1.17.3 scipy: 1.3.1 Cython: 0.29.13 pandas: 0.24.2

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:24 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
ashishsangwan18commented, May 26, 2020

This is sklearn bug. You should reduce the version of sklearn:

conda install scikit-learn==0.21.2

1reaction
wchillcommented, Mar 16, 2022

@cmarmo I’ve confirmed it’s still an issue in 1.0.2, opened #22857 with an example.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Python scikit-learn: Cannot clone object... as the constructor ...
clone because visible_config does not get copied correctly. I'm not sure how to fix this. It says in the documentation that sklearn.base.clone ......
Read more >
sklearn.base — scikit-optimize 0.8.1 documentation
Clone does a deep copy of the model in an estimator without actually copying ... if param1 is not param2: raise RuntimeError('Cannot clone...
Read more >
Developing scikit-learn estimators
The main objects in scikit-learn are (one class can implement multiple interfaces):. Estimator: The base object, implements a fit method to learn from...
Read more >
sklearn.base — gplearn 0.4.2 documentation - Read the Docs
Clone does a deep copy of the model in an estimator without actually copying ... if param1 is not param2: raise RuntimeError( "Cannot...
Read more >
Typeerror: Cannot Clone Object '' (Type ≪Class ''≫) - ADocLib
Description RuntimeError thrown when using sklearn.base.clone with estimators whose implementation of getparams returns copies instead. sklearn.base: ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found