Using random search with scikit-learn

Grid search and random search can be easily implemented with scikit-learn. In this example, we will use the KerasClassifier class from Keras to wrap our model and make it compatible with the scikit-learn API. Then, we will use scikit-learn's RandomSearchCV class to do the hyperparameter search.

To do this, we will start by changing our now familiar model build function slightly. We will parameterize it with the hyperparameters we would like to search, as shown in the following code:

def build_network(keep_prob=0.5, optimizer='adam'):    inputs = Input(shape=(784,), name="input")    x = Dense(512, activation='relu', name="hidden1")(inputs)    x = Dropout(keep_prob)(x)    x = Dense(256, activation='relu', name="hidden2" ...

Get Deep Learning Quick Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.