9951 explained code solutions for 126 technologies


python-kerasHow can I use Python Keras Tuner to optimize my model's hyperparameters?


Keras Tuner is a library that allows you to easily search for the best hyperparameters for your deep learning model. It works by running a series of trials with different hyperparameter combinations, and then selecting the combination that yields the best performance.

To use Keras Tuner, you first need to define the hyperparameters that you wish to optimize. For example, if you were building a convolutional neural network, you might define hyperparameters such as the number of filters, kernel size, and learning rate.

Once you have defined the hyperparameters, you can use Keras Tuner to find the optimal combination. The following example code shows how to use the Hyperband tuner to find the best hyperparameters for a convolutional neural network:

from kerastuner.tuners import Hyperband

# Define the model
def build_model(hp):
  model = Sequential()
  model.add(Conv2D(
      filters=hp.Int('filters', min_value=32, max_value=128, step=16),
      kernel_size=hp.Choice('kernel_size', values = (3,5)),
      activation='relu',
      input_shape=input_shape))
  model.add(Flatten())
  model.add(Dense(10, activation='softmax'))

  # Compile the model
  model.compile(
      optimizer=keras.optimizers.Adam(
          hp.Choice('learning_rate',
                    values=[1e-2, 1e-3, 1e-4])),
      loss='categorical_crossentropy',
      metrics=['accuracy'])

  return model

# Initialize the Hyperband tuner
tuner = Hyperband(
    build_model,
    objective='val_accuracy',
    max_epochs=10,
    factor=3,
    directory='my_dir',
    project_name='intro_to_kt')

# Search for the best hyperparameters
tuner.search(x_train, y_train, epochs=10, validation_split=0.1)

# Get the optimal hyperparameters
best_hps = tuner.get_best_hyperparameters(num_trials = 1)[0]

print(f"""
The hyperparameter search is complete. The optimal number of filters is {best_hps.get('filters')}, the optimal kernel size is {best_hps.get('kernel_size')} and the optimal learning rate is {best_hps.get('learning_rate')}.
""")

The output of the above code will be something like this:

The hyperparameter search is complete. The optimal number of filters is 96, the optimal kernel size is 5 and the optimal learning rate is 0.001.

Keras Tuner provides a number of different tuners for hyperparameter optimization, including RandomSearch, Hyperband, and BayesianOptimization. You can find more information about these tuners in the Keras Tuner documentation.

Edit this code on GitHub