84. Keras Model Hyper-parameter Tuning

One way to tune your keras model is using random search. Here is one example with sklearn.

from scipy.stats import reciprocal
from sklearn.model_selection import RandomizedSearchCV

#Specify range for random initialization
param_distribs = {
    "n_hidden": [0, 1, 2, 3],
    "n_neurons": np.arange(1, 100),
    "learning_rate": reciprocal(3e-4, 3e-2).rvs(1000),
}

rnd_search_cv = RandomizedSearchCV(KERAS_MODEL, param_distribs, n_iter=10, cv=3, verbose=2)
rnd_search_cv.fit(X_train, y_train, epochs=50,
                  validation_data=(X_valid, y_valid),
                  callbacks=[keras.callbacks.EarlyStopping(patience=10)])

After fitting, information about the best parameter will be stored to the rnd_search_cv instance.
You can check the parameters with the following code.

rnd_search_cv.best_params_
#OUTPUT EXAMPLE:{'n_neurons': 74, 'n_hidden': 3, 'learning_rate': 0.005803602934201024}

You can also load the best model as well.

model = rnd_search_cv.best_estimator_.model