Can't get GridSearchCV working with Keras

Question:

I’m trying to use GridSearchCV to optimise the hyperparameters in a custom model built with Keras. My code so far:

https://pastebin.com/ujYJf67c#9suyZ8vM

The model definition:

def build_nn_model(n, hyperparameters, loss, metrics, opt):
    model = keras.Sequential([
    keras.layers.Dense(hyperparameters[0], activation=hyperparameters[1], # number of outputs to next layer
                           input_shape=[n]),  # number of features
    keras.layers.Dense(hyperparameters[2], activation=hyperparameters[3]),
    keras.layers.Dense(hyperparameters[4], activation=hyperparameters[5]),

    keras.layers.Dense(1) # 1 output (redshift)
    ])

    model.compile(loss=loss,
                  optimizer = opt,
            metrics = metrics)
    return model

and the grid search:

optimizer = ['SGD', 'RMSprop', 'Adagrad', 'Adadelta', 'Adam', 'Adamax', 'Nadam']
epochs = [10, 50, 100]

param_grid = dict(epochs=epochs, optimizer=optimizer)
grid = GridSearchCV(estimator=model, param_grid=param_grid, scoring='accuracy', n_jobs=-1, refit='boolean')
grid_result = grid.fit(X_train, y_train)

throws an error:

TypeError: Cannot clone object '<keras.engine.sequential.Sequential object at 0x0000028B8C50C0D0>' (type <class 'keras.engine.sequential.Sequential'>): it does not seem to be a scikit-learn estimator as it does not implement a 'get_params' method.

How can I get GridSearchCV to play nicely with the model as it’s defined?

Asked By: Jim421616

||

Answers:

I’m assuming you are training a classifier, so you have to wrap it in KerasClassifier:

from scikeras.wrappers import KerasClassifier

...

model = KerasClassifier(build_nn_model)

# Do grid search

Remember to provide for each of build_nn_model‘s parameters either a default value or a grid in GridSearchCV.

For a regression model use KerasRegressor instead.

Answered By: Alex Bochkarev