Python Keras cross_val_score错误 [英] Python Keras cross_val_score Error

查看:246
本文介绍了Python Keras cross_val_score错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写有关回归的keras小教程: http://machinelearningmastery.com/regression-tutorial-keras-deep- learning-library-python/

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)

错误提示:

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

感谢您的帮助.

这是完整的追溯:

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'

推荐答案

报告的具体错误是:

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

该错误是由Keras 1.2.1版中的一个错误引起的.当您使用Keras包装器类(例如KerasClassifier和KerasRegressor)和scikit-learn函数cross_val_score()时,就会发生这种情况.

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().

该错误已已识别我尝试了两种修复方法:

There are two fixes that I have tried:

修复1:回滚到Keras版本1.2.0.

类型:

sudo pip install keras==1.2.0

修复2:修复了猴子补丁Keras.

在您导入之后但在您的工作之前:

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

这两个修复程序都对我有用(Python 2和3/sklearn 0.18.1).

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

一些其他候选修补程序:

Some additional candidate fixes:

  • 等待发布下一版的Keras(1.2.2).
  • 从Github签出Keras,然后手动构建和安装.

这篇关于Python Keras cross_val_score错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆