gpt4 book ai didi

Python Keras cross_val_score 错误

转载 作者:太空狗 更新时间:2023-10-29 20:10:26 24 4
gpt4 key购买 nike

我正在尝试在 keras 上做这个关于回归的小教程: http://machinelearningmastery.com/regression-tutorial-keras-deep-learning-library-python/

不幸的是,我遇到了无法修复的错误。如果我只是复制并粘贴代码,则在运行此代码段时会出现以下错误:

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 版本的 bug 引入的。当您使用 Keras 包装器类(例如 KerasClassifier 和 KerasRegressor)和 scikit-learn 函数 cross_val_score() 时会发生这种情况。

错误已identifiedpatched在 Keras GitHub 项目中。

我尝试了两个修复方法:

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

类型:

sudo pip install keras==1.2.0

修复 2:使用修复的 Monkey-patch Keras。

导入之后,工作类型之前:

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)。

一些额外的候选修复:

  • 等待下一个版本的 Keras (1.2.2) 发布。
  • 从 Github 检查 Keras,然后手动构建和安装。

关于Python Keras cross_val_score 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41796618/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com