KerasRegressor无法克隆对象-不知道为什么会引发此错误 [英] KerasRegressor cannot clone object - no idea why this error is being thrown

查看:46
本文介绍了KerasRegressor无法克隆对象-不知道为什么会引发此错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用scikit学习包装器构建基本的Keras模型,以便可以进行随机跨值搜索来优化参数.代码如下:

I am building a basic Keras model with a scikit learn wrapper so that I can do a random cross-val search to optimize the parameters. The code is the following:

def build_model(n_hidden=1, n_neurons=30, learning_rate=3e-3, input_shape=[8]):
     model = models.Sequential()
     model.add(layers.InputLayer(input_shape=input_shape))
     for layer in range(n_hidden):
          model.add(layers.Dense(n_neurons, activation="relu"))
     model.add(layers.Dense(1))
     optimizer = optimizers.SGD(lr=learning_rate)
     model.compile(loss="mse", optimizer=optimizer)
     return model

keras_reg = wrappers.scikit_learn.KerasRegressor(build_fn=build_model)
keras_reg.fit(X_train, y_train, epochs=100, validation_data=(X_valid, y_valid), callbacks= 
              [callbacks.EarlyStopping(patience=10)])
mse_test = keras_reg.score(X_test, y_test)
y_pred = keras_reg.predict(X_new)

from scipy.stats import reciprocal
from sklearn.model_selection import RandomizedSearchCV

param_distribs = {
    "n_hidden": [0, 1, 2, 3],
    "n_neurons": (np.arange(1,100)),
    "learning_rate": (reciprocal(3e-4, 3e-2))
}

rnd_search_cv = RandomizedSearchCV(keras_reg, param_distribs, n_iter=10, cv=3)
rnd_search_cv.fit(X_train, y_train, epochs=100, validation_data=(X_valid, y_valid), callbacks= 
                 [callbacks.EarlyStopping(patience=10)])

我在最后一行遇到错误:

I am getting the error on the last line:

回溯(最近通话最近):在第49行的文件"ml_hyperparams.py"中rnd_search_cv.fit(X_train,y_train,epochs = 100,validation_data =(X_valid,y_valid),callbacks = [callbacks.EarlyStopping(patience = 10)])

Traceback (most recent call last): File "ml_hyperparams.py", line 49, in rnd_search_cv.fit(X_train, y_train, epochs=100, validation_data=(X_valid, y_valid), callbacks=[callbacks.EarlyStopping(patience=10)])

文件C:\Users\Jesus\AppData\Roaming\Python\Python37\site-packages\sklearn\model_selection_search.py​​",第 736 行,合适**self.best_params_))克隆中的文件"C:\ Users \ AppData \ Roaming \ Python \ Python37 \ site-packages \ sklearn \ base.py",第82行(估算器,名称))

File "C:\Users\Jesus\AppData\Roaming\Python\Python37\site-packages\sklearn\model_selection_search.py", line 736, in fit **self.best_params_)) File "C:\Users\AppData\Roaming\Python\Python37\site-packages\sklearn\base.py", line 82, in clone (estimator, name))

RuntimeError:无法克隆对象< tensorflow.python.keras.wrappers.scikit_learn.KerasRegressor对象位于0x0000015C3CBCDB88> ;,因为构造函数没有设置或修改参数learning_rate

RuntimeError: Cannot clone object <tensorflow.python.keras.wrappers.scikit_learn.KerasRegressor object at 0x0000015C3CBCDB88>, as the constructor either does not set or modifies parameter learning_rate

我在某处读到,这通常在params dict具有嵌套列表时发生,并且可以使用元组和括号将其固定,但显然不起作用.最不寻常的是,我正在逐字复制 Aurélien Géron 的Hands-On Machine Learning"中的代码.教科书,所以我不知道为什么会引发此错误.感谢您的任何帮助,谢谢!

I read somewhere that this usually occurs when the params dict has nested list and that it can be fixed using tuples hence the parentheses, but obviously that doesn't work. What is most unusual about this is that I am copying this code verbatim from Aurélien Géron's - "Hands-On Machine Learning" textbook so I have no idea why this error is being thrown. Any help is appreciated thank you!

推荐答案

通常,人们说使用list和tuple而不是numpy array,但是不幸的是,它不能完全解决问题.这是一个问题(此处,和

Tipically folks says to work with list and tuple instead of numpy array, but unfortunately it doesn't fully solve the problem. This an issue (here, and here) in Kera's scikit learn wrapper, which can be easily solved in the following way:

  1. 找到您的tensorflow的文件夹安装和文件 tensorflow \ python \ keras \ wrappers \ scikit_learn.py

在第117行中编辑文件:

Edit the file in the line 117:

删除: res = copy.deepcopy(self.sk_params)

添加: res = self.sk_params.copy()

基本上,它是从深复制到浅复制.在这里工作正常.原始的解决方案在这里(

It basically switch from a deep copy to a shalow one. It worked fine here. The original solution is here (a pull merged in Tensorflow's project)

最诚挚的问候

这篇关于KerasRegressor无法克隆对象-不知道为什么会引发此错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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