Using Keras Tuner to Find the Best Hyperparameters for Your Neural Network Model.

Vinicius Queiroz
4 min readMar 10, 2023

--

Deep learning models have become a go-to solution for many complex tasks, such as image recognition, natural language processing, and speech recognition. However, developing an optimal deep learning model can be a daunting task, as it requires a deep understanding of the model architecture, hyperparameters, and training parameters.

Thankfully, there are many tools available to assist in this task, and one of the most popular tools is Keras Tuner. Keras Tuner is a powerful library that allows you to automate the hyperparameter tuning process and search for the best model configuration. In this article, we will cover how to use Keras Tuner to tune a deep learning model and improve its performance.

Setting up Keras Tuner

Before we dive into tuning our deep learning model, we need to set up Keras Tuner. Keras Tuner is an open-source Python library, which means you can install it using pip. Run the following command in yout notebook to install Keras Tuner:

!pip install keras-tuner

Once you have installed Keras Tuner, we can begin tuning our deep learning model.

Importing the Required Libraries

To begin, let’s import the required libraries that we will use in this tutorial:

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from kerastuner.tuners import RandomSearch

Defining the Model

In this tutorial, we will use the classic MNIST dataset to classify handwritten digits. We will define a simple deep learning model that consists of three hidden layers and an output layer with a softmax activation function. We will use the Adam optimizer and categorical cross-entropy loss function.

def build_model(hp):
model = keras.Sequential()
model.add(layers.Flatten(input_shape=(28, 28)))

# Tune the number of units in the first dense layer
# Choose an optimal value between 32-512
hp_units1 = hp.Int('units1', min_value=32, max_value=512, step=32)
model.add(layers.Dense(units=hp_units1, activation='relu'))

# Tune the number of units in the second dense layer
# Choose an optimal value between 32-512
hp_units2 = hp.Int('units2', min_value=32, max_value=512, step=32)
model.add(layers.Dense(units=hp_units2, activation='relu'))

# Tune the learning rate for the optimizer
# Choose an optimal value from 0.01, 0.001, or 0.0001
hp_learning_rate = hp.Choice('learning_rate', values=[0.01, 0.001, 0.0001])

model.add(layers.Dense(10, activation='softmax'))

model.compile(optimizer=keras.optimizers.Adam(learning_rate=hp_learning_rate),
loss='categorical_crossentropy',
metrics=['accuracy'])

return model

Creating the Tuner

Now that we have defined our model, we can create a tuner to search for the best hyperparameters. In this tutorial, we will use the RandomSearch tuner, which randomly samples hyperparameters from the defined search space.

tuner = RandomSearch(
build_model,
objective='val_accuracy',
max_trials=5,
executions_per_trial=3,
directory='my_dir',
project_name='helloworld')

In the code above, we create a RandomSearch tuner and set the following parameters:

  • build model: A function that builds and compiles the Keras model
  • objective: The metric to optimize. In this case, we want to maximize the validation accuracy.
  • max_trials: The maximum number of hyperparameter combinations to try.
  • executions_per_trial: The number of times to train the model for each combination of hyperparameters.
  • directory: The directory where to save the results of the search.
  • project_name: The name of the project. This will be used to name the directory where to save the results.

Running the Tuner

Once we have created the tuner, we can start the search process by calling the search method and passing in the training and validation data.

tuner.search(x_train, y_train, epochs=5, validation_data=(x_val, y_val))

In the code above, we search for the best hyperparameters for five epochs using the training and validation data. The search() method trains and evaluates the model for each combination of hyperparameters defined by the tuner.

Retrieving the Best Model

After the tuner has completed the search process, we can retrieve the best model configuration using the get_best_models() method. This method returns a list of models sorted by their validation accuracy, with the best model at index 0.

best_models = tuner.get_best_models(num_models=1)

In the code above, we retrieve the best model configuration, which is the model with the highest validation accuracy.

Evaluating the Best Model

Now that we have the best model configuration, we can evaluate its performance on the test set using the evaluate() method.

test_loss, test_acc = best_models[0].evaluate(x_test, y_test)
print('Test accuracy:', test_acc)

In the code above, we evaluate the best model on the test set and print the test accuracy.

Conclusion

In this article, we have covered how to use Keras Tuner to tune a deep learning model and improve its performance.

Keras Tuner is a powerful library that can help you automate the hyperparameter tuning process and find the best model configuration.

By following the steps outlined in this article, you can improve the performance of your deep learning models and achieve better result and, of course, save a lot of time!

--

--