In this tutorial, we will introduce how to tune neural network hyperparameters using grid search method in keras.
1. Import libraries
import numpy as np from keras import models from keras import layers from keras.wrappers.scikit_learn import KerasClassifier from sklearn.model_selection import GridSearchCV from sklearn.datasets import make_classification # Set random seed np.random.seed(0)
2. Create data to train
# Number of features number_of_features = 100 # Generate features matrix and target vector features, target = make_classification(n_samples = 10000, n_features = number_of_features, n_informative = 3, n_redundant = 0, n_classes = 2, weights = [.5, .5], random_state = 0)
3. Build a neural network using keras
def create_network(optimizer='rmsprop'): # Start neural network network = models.Sequential() # Add fully connected layer with a ReLU activation function network.add(layers.Dense(units=16, activation='relu', input_shape=(number_of_features,))) # Add fully connected layer with a ReLU activation function network.add(layers.Dense(units=16, activation='relu')) # Add fully connected layer with a sigmoid activation function network.add(layers.Dense(units=1, activation='sigmoid')) # Compile neural network network.compile(loss='binary_crossentropy', # Cross-entropy optimizer=optimizer, # Optimizer metrics=['accuracy']) # Accuracy performance metric # Return compiled network return network
4. Wrap function In keras
# Wrap Keras model so it can be used by scikit-learn neural_network = KerasClassifier(build_fn=create_network, verbose=0)
5. Create a search space
# Create hyperparameter space epochs = [5, 10] batches = [5, 10, 100] optimizers = ['rmsprop', 'adam'] # Create hyperparameter options hyperparameters = dict(optimizer=optimizers, epochs=epochs, batch_size=batches)
6. Start grid search to find best hyperparameters
# Create grid search grid = GridSearchCV(estimator=neural_network, cv=3, param_grid=hyperparameters) # Fit grid search grid_result = grid.fit(features, target)
7. Find best hyperparameters
# View hyperparameters of best neural network grid_result.best_params_
Run this code, you may get the best hyperparameters:
{'batch_size': 10, 'epochs': 5, 'optimizer': 'adam'}