无法克隆对象 <keras.wrappers.scikit_learn.KerasClassifier object at 0x7f9d95dd50f0>,因为构造函数要么未设置

Posted

技术标签:

【中文标题】无法克隆对象 <keras.wrappers.scikit_learn.KerasClassifier object at 0x7f9d95dd50f0>,因为构造函数要么未设置【英文标题】:Cannot clone object <keras.wrappers.scikit_learn.KerasClassifier object at 0x7f9d95dd50f0>, as the constructor either does not set 【发布时间】:2020-11-02 14:09:08 【问题描述】:

我得到这个 runtimeError: Cannot clone object ,因为构造函数没有设置或修改参数 batch_size 在深度学习 Jason Bownlee 中执行网格搜索时。

# MLP for Pima Indians Dataset with grid search via sklearnfrom keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasClassifier
from sklearn.model_selection import StratifiedKFold
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import GridSearchCV
import numpy
# MLP for Pima Indians Dataset with 10-fold cross validation via sklearn
from keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasClassifier
from sklearn.model_selection import StratifiedKFold
from sklearn.model_selection import cross_val_score
import numpy
# MLP for Pima Indians Dataset with 10-fold cross validation via sklearn
from keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasClassifier
from sklearn.model_selection import StratifiedKFold
from sklearn.model_selection import cross_val_score
import numpy
# Function to create model, required for KerasClassifier
def create_model(optimizer= 'rmsprop' , init= 'glorot_uniform'):
    # create model
    model = Sequential()
    model.add(Dense(12, input_dim=8, kernel_initializer= 'uniform' , activation= 'relu' )) 
    model.add(Dense(8, kernel_initializer = 'uniform' , activation= 'relu' )) 
    model.add(Dense(1, kernel_initializer = 'uniform' , activation= 'sigmoid' )) 
    # Compile model
    model.compile(loss= 'binary_crossentropy' , optimizer= 'adam' , metrics=['accuracy'])
    return model
# fix random seed for reproducibility
seed = 7
numpy.random.seed(seed)
# load pima indians dataset
from google.colab import drive
drive.mount('/content/drive')
dataset = numpy.loadtxt("/content/drive/My Drive/Colab Notebooks/pima-indians-diabetes.csv", delimiter=",", skiprows=1)
# split into input (X) and output (Y) variables
X = dataset[:,0:8]
Y = dataset[:,8]
# create model
model = KerasClassifier(build_fn=create_model, verbose=0)
# grid search epochs, batch size and optimizer
optimizers = [ 'rmsprop' , 'adam']
init = [ 'glorot_uniform' , 'normal' , 'uniform']
epochs = numpy.array([50, 100, 150])
batches = numpy.array([5, 10, 20])
param_grid = dict(optimizer=optimizers, nb_epoch=epochs, batch_size=batches, init=init)
grid = GridSearchCV(estimator=model, param_grid=param_grid)
grid_result = grid.fit(X, Y)
# summarize results
print("Best: %f using %s" % (grid_result.best_score_, grid_result.best_params_))
for params, mean_score, scores in grid_result.grid_scores_:
    print("%f (%f) with: %r" % (scores.mean(), scores.std(), params))
RuntimeError                              Traceback (most recent call last)
....
---> 56 grid_result = grid.fit(X, Y)
RuntimeError: Cannot clone object <keras.wrappers.scikit_learn.KerasClassifier object at 0x7f375a51b2b0>, as the constructor either does not set or modifies parameter nb_epoch

【问题讨论】:

【参考方案1】:

我遇到了这个确切的问题,并通过将 numpy.arrays 更改为列表来解决它。例如

batches = [5, 10, 20]

总而言之,使用列表(就像您对优化器、init 等所做的那样)而不是 numpy.array

【讨论】:

以上是关于无法克隆对象 <keras.wrappers.scikit_learn.KerasClassifier object at 0x7f9d95dd50f0>,因为构造函数要么未设置的主要内容,如果未能解决你的问题,请参考以下文章

无法克隆对象 <keras.wrappers.scikit_learn.KerasClassifier object at 0x7f9d95dd50f0>,因为构造函数要么未设置

无法使用 Sklearn 和 Keras Wrappers 使 pipeline.fit() 工作

由于不可克隆性,将 KerasRegressor 与 cross_validate 一起使用失败

keras开发成sklearn接口

用于 Scikit Learn 的 Keras Wrappers - AUC 记分器不工作

KerasClassifier 对象没有属性模型