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

cbeh67ev  于 2023-05-18  发布在  其他
关注(0)|答案(1)|浏览(246)

我得到了这个:

runtimeError: Cannot clone object <keras.wrappers.scikit_learn.KerasClassifier object at 0x7f9d95dd50f0>,

因为在Grid Search Deep Learning Model Parameters by Jason Bownlee中执行 gridsearch 时,构造函数没有设置或修改参数batch_size

# 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
kulphzqa

kulphzqa1#

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

batches = [5, 10, 20]

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

相关问题