Easy Hyperparameter Tuning in Neural Networks using Keras Tuner
When building neural networks, choosing the right set of hyperparameters is critical to getting good performance. Hyperparameters are the tunable "knobs" of a neural net that control its capacity, training speed, and generalization ability. Unlike model parameters like weights that are learned during training, hyperparameters must be set before training begins.
Some of the most important hyperparameters in neural nets include:
- Number and width of hidden layers
- Type of activation functions
- Learning rate and optimizer settings
- Regularization methods like dropout, L1/L2, early stopping
- Batch size and number of training epochs
Traditionally, finding a good set of hyperparameters has required a tedious manual search process based on trial-and-error and expert intuition. Data scientists would train many models with different combinations of settings and compare their performance to gradually hone in on an optimal configuration.
In recent years, automated hyperparameter optimization has emerged as a way to make this process much more efficient and less of an art. Libraries like Hyperopt, Optuna, and Keras Tuner allow you to define a search space of hyperparameters and automate the tuning process using proven optimization algorithms.
Keras Tuner is a popular open-source library developed by Google for hyperparameter tuning with Keras, one of the most widely used deep learning frameworks. It provides a simple, powerful API for defining a hyperparameter search and integrating it into your model-building workflow.
Architecture of Keras Tuner
Under the hood, Keras Tuner provides an abstraction called HyperModel for defining a model-building function along with a hyperparameter search space. The HyperModel encapsulates everything needed to construct and train a Keras model for different sets of hyperparameters.
Keras Tuner then takes this HyperModel and uses one of several built-in search algorithms to find the optimal hyperparameter configuration. During the search process, it iteratively samples hyperparameter values, builds and trains models, evaluates their performance, and uses this information to intelligently decide what to try next.
The main search algorithms available in Keras Tuner are:
- Random Search – Randomly samples hyperparameter combinations
- Hyperband – Uses adaptive resources allocation and early-stopping to quickly converge on promising configurations
- Bayesian Optimization – Builds a probabilistic model of the objective function and uses it to select the most promising hyperparameters to evaluate
In most cases, Hyperband and Bayesian optimization will outperform random search by arriving at better solutions in fewer total model evaluations.
Defining a Hyperparameter Search Space
To use Keras Tuner, you start by defining a HyperModel that specifies the space of architectures and hyperparameters to search over. This is done by creating a subclass of HyperModel and overriding its build() method.
Inside build(), you define your model architecture using the standard Keras functional or Sequential API, but replace hyperparameter values with special HyperParameters. These act as placeholders that will be filled in with concrete values by the tuner during the search.
For example, to define a tunable number of hidden layers between 1 and 5:
class MyHyperModel(HyperModel):
def build(self, hp):
model = Sequential()
for i in range(hp.Int(‘num_layers‘, 1, 5)):
model.add(Dense(units=64, activation=‘relu‘))
model.add(Dense(1, activation=‘sigmoid‘))
model.compile(loss=‘binary_crossentropy‘, optimizer=‘adam‘, metrics=[‘accuracy‘])
return model
Here hp.Int() is used to define an integer-valued hyperparameter that the tuner will replace with values between 1 and 5 inclusive.
Other HyperParameter types include:
- hp.Choice(options) – Categorical values chosen from a list
- hp.Float(min, max, step) – Float values between min and max
- hp.Boolean() – Boolean values
- hp.Fixed(value) – Constant values passed through
You can also use conditional logic to define more complex spaces, like different layer types or activation functions depending on a categorical HyperParameter:
class MyHyperModel(HyperModel):
def build(self, hp):
model = Sequential()
for i in range(hp.Int(‘num_layers‘, 1, 5)):
units = hp.Int(f‘units_{i}‘, 32, 512, 32)
activation = hp.Choice(f‘activation_{i}‘, [‘relu‘, ‘tanh‘])
model.add(Dense(units=units, activation=activation))
model.add(Dense(1, activation=‘sigmoid‘))
model.compile(loss=‘binary_crossentropy‘,
optimizer=hp.Choice(‘optimizer‘, [‘adam‘, ‘sgd‘]),
metrics=[‘accuracy‘])
return model
This search space would include between 1 and 5 hidden layers, each with a number of units from 32 to 512 in increments of 32, relu or tanh activation, and an Adam or SGD optimizer.
Running a Hyperparameter Search
With a HyperModel defined, actually running the search is straightforward. You instantiate a Tuner by passing in the HyperModel, specifying the objective to optimize (like validation accuracy), and set a few other configs:
tuner = BayesianOptimization(MyHyperModel(),
objective=‘val_accuracy‘,
max_trials=50,
executions_per_trial=3,
directory=‘my_dir‘,
project_name=‘helloworld‘)
Then kick off the search by calling search() with your training data:
tuner.search(x_train, y_train, epochs=5, validation_data=(x_val, y_val))
The tuner will proceed to test out different model architectures and hyperparameter settings, evaluating each one on the held-out validation set. You can track progress in the terminal output or via TensorBoard.
After the search is finished, you can retrieve a summary of results:
tuner.results_summary()
As well as the best performing model(s):
best_models = tuner.get_best_models(num_models=2)
Keras Tuner makes it very simple to integrate powerful hyperparameter tuning into the model development process. With just a few lines of code, you can easily search over wide spaces of architectures and settings to arrive at an optimized model.
Tips and Best Practices
While Keras Tuner automates much of the search process, there are still some important considerations to keep in mind:
-
Start simple and gradually expand your search space. Begin with a relatively small, focused search over the most critical hyperparameters before broadening it to include more settings.
-
Use appropriate HyperParameters for each value. Favor Float and Int for numerical hyperparameters and Choice for categorical ones. Avoid searching over too many values.
-
Set a reasonable number of max_trials based on your computational budget. More trials will cover the search space more thoroughly but also take longer. You may need to experiment some to find the right tradeoff.
-
Take advantage of Tuner callback hooks to add logic for early-stopping, model checkpointing, learning rate scheduling, etc. This can help reduce wasted computation.
-
Evaluate the best hyperparameters on a separate test set. The scores returned by the tuner are biased since they were used to select the best model. Always confirm performance on unseen data before deploying.
Keras Tuner vs Other Libraries
While Keras Tuner is a solid choice for hyperparameter optimization with Keras models, there are many other good options. Scikit-Optimize, Optuna, and Determined AI are popular standalone libraries that support Keras as well as other frameworks like PyTorch and XGBoost.
Keras Tuner‘s strengths are its simplicity, tight integration with the Keras API, and excellent documentation from Google. However, it currently lacks some of the more advanced features of other libraries like visualization, pruning, and distributed training. It also doesn‘t support architecture search quite as flexibly as a library like Optuna.
Limitations and Future Directions
Keras Tuner and similar hyperparameter optimization tools have made the model tuning process dramatically more efficient in recent years. However, they still face some key challenges:
Efficiently searching over very large, conditional search spaces is difficult and an active area of research. Most existing libraries are limited in how complex of spaces they can optimize.
Evaluating each trial can be extremely expensive for large neural networks. Model-based optimization helps but more work is needed to reduce the number of trials needed.
As models grow more complex, the search space expands to include things like custom neural architectures and data augmentation pipelines. Supporting these in a clean, unified way is an open challenge.
Despite these limitations, automated hyperparameter tuning is already an invaluable tool for building state-of-the-art deep learning models. Libraries like Keras Tuner have made this technology accessible to a wide audience beyond just expert practitioners. Continued development of even more efficient and flexible AutoML approaches promises to further accelerate progress in the years ahead.