Python Keras cross_val_score Error

2020-08-15 02:12发布

问题:

I am trying to do this little tutorial on keras about regression: http://machinelearningmastery.com/regression-tutorial-keras-deep-learning-library-python/

Unfortunately I am running into an error I cannot fix. If i just copy and paste the code I get the following error when running this snippet:

import numpy
import pandas
from keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasRegressor
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import KFold
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline
# load dataset
dataframe = pandas.read_csv("housing.csv", delim_whitespace=True,header=None)
dataset = dataframe.values
# split into input (X) and output (Y) variables
X = dataset[:,0:13]
Y = dataset[:,13]
# define base mode
def baseline_model():
    # create model
    model = Sequential()
    model.add(Dense(13, input_dim=13, init='normal', activation='relu'))
    model.add(Dense(1, init='normal'))
    # Compile model
    model.compile(loss='mean_squared_error', optimizer='adam')
    return model
# fix random seed for reproducibility
seed = 7
numpy.random.seed(seed)
# evaluate model with standardized dataset
estimator = KerasRegressor(build_fn=baseline_model, nb_epoch=100,batch_size=5, verbose=0)

kfold = KFold(n_splits=10, random_state=seed)
results = cross_val_score(estimator, X, Y, cv=kfold)

The error says:

TypeError: get_params() got an unexpected keyword argument 'deep'

Thanks for any help.

Here is the full traceback:

Traceback (most recent call last):


File "<stdin>", line 1, in <module>
  File "C:\Users\myname\Anaconda3\lib\site-packages\sklearn\model_selection\_validation.py", line 140, in cross_val_score
    for train, test in cv_iter)
  File "C:\Users\myname\Anaconda3\lib\site-packages\sklearn\externals\joblib\parallel.py", line 758, in __call__
    while self.dispatch_one_batch(iterator):
  File "C:\Users\myname\Anaconda3\lib\site-packages\sklearn\externals\joblib\parallel.py", line 603, in dispatch_one_batch
    tasks = BatchedCalls(itertools.islice(iterator, batch_size))
  File "C:\Users\myname\Anaconda3\lib\site-packages\sklearn\externals\joblib\parallel.py", line 127, in __init__
    self.items = list(iterator_slice)
  File "C:\Users\myname\Anaconda3\lib\site-packages\sklearn\model_selection\_validation.py", line 140, in <genexpr>
    for train, test in cv_iter)
  File "C:\Users\myname\Anaconda3\lib\site-packages\sklearn\base.py", line 67, in clone
    new_object_params = estimator.get_params(deep=False)
TypeError: get_params() got an unexpected keyword argument 'deep'

回答1:

The specific error reported is:

TypeError: get_params() got an unexpected keyword argument 'deep'

The fault was introduced by a bug in Keras version 1.2.1. It occurs when you use the Keras wrapper classes (e.g. KerasClassifier and KerasRegressor) and scikit-learn function cross_val_score().

The bug has been identified and patched in the Keras GitHub project.

There are two fixes that I have tried:

Fix 1: Roll-back to Keras version 1.2.0.

Type:

sudo pip install keras==1.2.0

Fix 2: Monkey-patch Keras with the fix.

After your imports, but before your work type:

from keras.wrappers.scikit_learn import BaseWrapper
import copy

def custom_get_params(self, **params):
    res = copy.deepcopy(self.sk_params)
    res.update({'build_fn': self.build_fn})
    return res

BaseWrapper.get_params = custom_get_params

Both fixes work for me (Python 2 and 3/sklearn 0.18.1).

Some additional candidate fixes:

  • Wait for the next version of Keras (1.2.2) to be released.
  • Checkout Keras from Github then build and install manually.


回答2:

EDIT (25/01/2017): This solution works because with the conda environment the version of Keras that is installed is 1.1.1, not the one with the bug (1.2.1). Jason's solution is the correct one. I left here my solution in case it can helps but Jason's solution is the actual solution.

I had the same problem after upgrading Keras (1.2.1). I think the problem is with the versions of the software. What I recommend you is to install Anaconda , then to create a new environment where you install tensorflow. Basically follow these steps: https://www.tensorflow.org/get_started/os_setup#anaconda_installation

Activate the environment and install using the conda option. Then you can install other libraries that you're going to need. With the environment tensorflow activated you install with conda install name_of_the_package.

You can change between theano and tensorflow with the backends of Keras (https://keras.io/backend/).

Basically, with conda environments you're creating a protected, encapsulated area where you can install and uninstall what you want and it doesn't affect your other programs out of the environment. What you're doing is like a delete and re-install everything afresh, with the latest and working versions.

Hope it helps.



回答3:

Had the same problem. After upgrading keras version to 1.2.2 the problem went away.

If you use pip to manage your packages you can upgrade keras with the following command:

sudo pip install --upgrade keras