Cannot clone object <keras.wrappers.scikit_learn.KerasRegressor object at 0x7fdc9c3ba550>

Trying to hypertune ANN but getting an error while using fit..(grid1.fit(X_train, y_train))

Below is the code

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)

Topic hyperparameter-tuning randomized-algorithms keras error-handling

Category Data Science


I had the same problem. It seems to be a bug in keras that occurs with nested arrays as parameters for the grid search. I was able to solve it by nesting tuples instead of arrays.

So try to change

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

to

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

(add a ,(comma) when only 1 value is available in tuple coz python treats 1d tuple as an integer)


This is sklearn bug. You should reduce the version of sklearn:

conda install scikit-learn==0.21.2


I get the same error also if I use any random variable distribution for any parameter. For instance:

from scipy.stats import uniform, loguniform, branding

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

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.