无法克隆对象 <keras.wrappers.scikit_learn.KerasRegressor 对象位于 0x7fdc9c3ba550>

数据挖掘 喀拉斯 超参数调整 错误处理 随机算法
2021-10-09 05:53:15

尝试超调 ANN 但在使用时出错 fit..(grid1.fit(X_train, y_train))

下面是代码

def create_model(dropout_rate,weight_constraint,optimizer,init,layers,activation):
    model = Sequential()
    model.add(Dense(nodes, input_dim=171, kernel_initializer=init, activation='relu', kernel_constraint=maxnorm(weight_constraint)))
    model.add(Dropout(dropout_rate))
    model.add(Dense(1, kernel_initializer=init, activation='relu'))

    model.compile(loss='mse', optimizer=optimizers, metrics=['mean_absolute_error'])
    return model

model = KerasRegressor(build_fn=create_model, verbose=0)

#hyperparameters
layers = [[50],[50, 20], [50, 30, 15], [70,45,15,5]]
optimizers = ['rmsprop', 'adam']
dropout_rate = [0.1, 0.2, 0.3, 0.4]
init = ['glorot_uniform', 'normal', 'uniform']
epochs = [150, 500]
batches = [5, 10, 20]
weight_constraint = [1, 2, 3]
param_dist = dict(optimizer=optimizers,
layers=layers,
dropout_rate=dropout_rate,
epochs=epochs,
batch_size=batches,
weight_constraint=weight_constraint,
init=init
)

grid1 = RandomizedSearchCV(estimator=model,param_distributions=param_dist,n_jobs=-1, cv=6)

grid1.fit(X_train, y_train)

3个回答

我有同样的问题。这似乎是keras中的一个错误,它使用嵌套数组作为网格搜索的参数。我能够通过嵌套元组而不是数组来解决它。

所以试着改变

层 = [[50],[50, 20], [50, 30, 15], [70,45,15,5]]

层 = [(50,),(50, 20), (50, 30, 15), (70,45,15,5)]

(当元组中只有 1 个值可用时添加一个,(逗号),因为 python 将 1d 元组视为整数)

如果我对任何参数使用任何随机变量分布,我也会得到同样的错误。例如:

from scipy.stats import uniform, loguniform, branding

dropout_rate = uniform(0.1, 0.4)
...
...

这是 sklearn 错误。您应该降低 sklearn 的版本:

conda install scikit-learn==0.21.2