Python Keras cross_val_score Error

陌路散爱 提交于 2019-12-05 01:53:49

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.

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.

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!