RuntimeError: "Cannot clone object ..." when using clone with an implementation of BaseEstimator that copies objects in get_params method
See original GitHub issueDescription
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:
- Created 4 years ago
- Comments:24 (9 by maintainers)
This is sklearn bug. You should reduce the version of sklearn:
conda install scikit-learn==0.21.2
@cmarmo I’ve confirmed it’s still an issue in 1.0.2, opened #22857 with an example.