KerasTuner: Hyperparameter Optimization for Keras and TensorFlow

Jul 6, 2025

Introduction

Finding the optimal set of hyperparameters for a deep learning model is often a tedious process of trial and error that can lead to suboptimal performance. KerasTuner is an open-source hyperparameter optimization framework designed to automate this search, enabling developers to find the best model architecture and training configurations with minimal effort. With its seamless integration into the Keras and TensorFlow ecosystems, KerasTuner replaces manual tuning with scalable, algorithmic search strategies that significantly improve model accuracy and generalization.

What Is KerasTuner?

KerasTuner is a hyperparameter optimization framework that automates the process of tuning machine learning models built with Keras and TensorFlow. It allows users to define a search space for hyperparameters—such as the number of layers, units per layer, and learning rates—and then leverages specialized search algorithms to explore this space and identify the optimal combination of values.

Maintained by the Keras team, KerasTuner is distributed under the Apache License 2.0. It is designed for AI practitioners, model designers, and researchers who need a clean, human-readable API to move from a base model to a fully optimized version quickly.

Why KerasTuner Matters

Hyperparameters are the variables that govern the training process and the topology of a machine learning model. Unlike model parameters (weights), hyperparameters are set before training begins and directly impact the model’s ability to converge and generalize to new data. Manually exploring this space is computationally expensive and often fails to identify the best configuration.

KerasTuner fills this gap by providing a structured, automated approach. By using algorithms like Bayesian Optimization and Hyperband, it can find better hyperparameters faster than random or grid search. This reduces the time spent on manual experimentation and allows developers to focus on data quality and model architecture rather than guessing values.

The framework’s traction is evident in its wide adoption across the TensorFlow community, as it is the recommended tool for hypertuning in official Keras documentation and tutorials.

Key Features

  • Define-by-Run Syntax: KerasTuner uses a flexible syntax that allows you to define your search space dynamically within a model-building function. This makes it easy to tune not just values, but the actual structure of the network.
  • Built-in Search Algorithms: The library includes three primary algorithms: Random Search for baseline exploration, Bayesian Optimization for probabilistic, informed search, and Hyperband for efficient resource allocation through early stopping of poor trials.
  • Scalable Architecture: It supports distributed hyperparameter search, allowing you to scale from a single local machine to dozens of workers in parallel using a chief-worker model.
  • Extensible Design: The framework is designed to be easy for researchers to extend, enabling the implementation of custom search algorithms beyond the built-in options.
  • TensorBoard Integration: KerasTuner integrates with TensorBoard’s HParams plugin, providing interactive visualizations of the hyperparameter tuning process and the results of each trial.
  • Broad Model Support: While optimized for Keras, the framework can be used to tune other models, including those built with scikit-learn, provided they can be wrapped in a compatible function.

How KerasTuner Compares

Feature KerasTuner Optuna Ray Tune
Keras Integration Native/Deep Via Wrapper Via Wrapper
Ease of Setup High Medium Medium
Search Algorithms Bayesian, Hyperband, Random TPE, CMA-ES, Random Extensive/Distributed
Distributed Tuning Supported (Chief-Worker) Supported (DB-based) Supported (DB-based)

KerasTuner is the ideal choice for developers who are already deeply embedded in the Keras/TensorFlow ecosystem. Its native integration means that defining a search space is as simple as adding a few lines of code to an existing model-building function. In contrast, Optuna is more framework-agnostic and offers a wider variety of sampling algorithms (like TPE), which may be preferred by researchers who switch between PyTorch and TensorFlow.

Ray Tune is designed for massive scale. While KerasTuner supports distributed tuning, Ray Tune is a full-fledged distributed computing framework that can handle thousands of trials across a cluster. For most standard deep learning projects, however, KerasTuner provides the best balance of ease-of-use and performance without the overhead of managing a Ray cluster.

Getting Started: Installation

KerasTuner requires Python 3.8+ and TensorFlow 2.0+. You can install the latest release via pip:

Using pip

pip install keras-tuner

To ensure you have the most recent version, you can use the upgrade flag:

pip install -U keras-tuner

How to Use KerasTuner

The basic workflow of KerasTuner involves three main steps: defining a model-building function, initializing a tuner, and starting the search.

First, you create a function that returns a compiled Keras model. Inside this function, you use the hp (HyperParameters) object to define the search space. For example, you can use hp.Int for integer ranges, hp.Choice for a specific list of values, or hp.Float for floating-point numbers.

Next, you instantiate a tuner (such as RandomSearch, BayesianOptimization, Hyperband) by passing the model-building function and the objective you want to optimize (e.g., val_loss or val_accuracy). Finally, you call the search method, which iterates through different hyperparameter combinations and evaluates them on your validation data.

Code Examples

Below is a complete example of how to tune the number of units in a dense layer and the learning rate of an optimizer.

import keras_tuner as kt
from tensorflow import keras
from tensorflow.keras import layers

def build_model(hp):
    model = keras.Sequential()
    model.add(layers.Flatten())
    # Tune the number of units in the first Dense layer
    model.add(layers.Dense(
        units=hp.Int("units", min_value=32, max_value=512, step=32),
        activation="relu",
    ))
    model.add(layers.Dense(10, activation="softmax"))
    
    # Tune the learning rate for the optimizer
    optimizer_lr = hp.Choice("learning_rate", values=[1e-2, 1e-3, 1e-4])
    model.compile(
        optimizer=keras.optimizers.Adam(learning_rate=optimizer_lr),
        loss="sparse_categorical_crossentropy",
        metrics=["accuracy"],
    )
    return model

tuner = kt.RandomSearch(
    build_model, 
    objective="val_accuracy", 
    max_trials=10
)

tuner.search(x_train, y_train, epochs=5, validation_split=0.2)

best_model = tuner.get_best_models(num_models=1)[0]

In this example, the hp.Int method defines a range for the number of units, while hp.Choice allows the tuner to pick from a specific set of learning rates. The RandomSearch tuner explores 10 different configurations, and get_best_models retrieves the model with the highest validation accuracy.

Advanced Configuration

For large-scale projects, KerasTuner supports distributed tuning and result persistence. You can control whether to search from scratch or resume a previous search using the overwrite argument in the tuner’s constructor.

To enable distributed mode, you must set the following environment variables on your chief and worker nodes:

# On the chief node
export KERASTUNER_TUNER_ID="chief"
export KERASTUNER_ORACLE_IP="127.0.0.1"
export KERASTUNER_ORACLE_PORT="8000"

# On the worker nodes
export KERASTUNER_TUNER_ID="tuner0"
export KERASTUNER_ORACLE_IP="127.0.0.1"
export KERASTUNER_ORACLE_PORT="8000"

This configuration allows multiple workers to report results to a chief service, which manages the search process and coordinates the next set of hyperparameters to to try.

Real-World Use Cases

KerasTuner is particularly effective in scenarios where the model architecture is not yet fully known or where precision is critical.

  • Customer Churn Prediction: A telecom company can use KerasTuner to fine-tune a custom neural network that incorporates domain-specific features, optimizing the number of layers and dropout rates to prevent overfitting on structured tabular data.
  • Image Classification: In a medical imaging project, researchers can use Hyperband to quickly discard poorly performing architectures for a CNN, saving thousands of hours of GPU compute time by focusing only on the most promising candidates.
  • Image Classification: In a medical imaging project, researchers can use Hyperband to quickly discard poorly performing architectures for a CNN, saving thousands of hours of GPU compute time by focusing only on the most promising candidates.
  • Time Series Forecasting: For financial market prediction, developers can tune the window size of an LSTM network and the number of hidden units to capture complex temporal dependencies without manual trial and error.
  • Automated ML (AutoML) Pipelines: KerasTuner is KerasTuner can be integrated into a larger pipeline where it automatically optimizes the model for different datasets, ensuring that the best possible architecture is selected for each specific task.

Contributing to KerasTuner

KerasTuner is an open-source project maintained by the Keras team. Contributions are welcome and encouraged. To get started, refer to the CONTRIBUTING.md file in the GitHub repository for detailed guidelines on how to submit pull requests and report bugs.

The project follows standard GitHub flow: developers should open an issue to discuss a discuss a new feature or request a bug fix before submitting a PR. This ensures that the project maintains its clean API and remains compatible with Keras core.

Community and Support

The KerasTuner community is highly active, with extensive support available through official channels. The primary hub for technical questions and feature requests is GitHub Discussions, where maintainers and other users share solutions and troubleshooting tips.

Official documentation is hosted on the Keras website, providing detailed API references, developer guides, and comprehensive tutorials. For those looking for deeper integration, the TensorFlow blog often publishes updates and new features of the framework.

Conclusion

KerasTuner is the definitive tool for anyone working with Keras and TensorFlow who wants to move beyond manual hyperparameter tuning. By automating the search for the best model architecture and training settings, it allows developers to achieve higher accuracy and better generalization with significantly less effort.

While it is an excellent choice for most deep learning projects, users should be aware that hyperparameter tuning is computationally expensive. It is recommended to start with a small search space and a RandomSearch tuner to get a baseline, then move to BayesianOptimization or Hyperband for fine-tuning.

Star the repo, try the quickstart, and join the community to start optimizing your models today.

What is KerasTuner and what problem does it solve?

KerasTuner is a hyperparameter optimization framework that automates the search for the best hyperparameters for Keras and TensorFlow models. It solves the problem of manual, trial-and-error tuning, which is often inefficient and computationally expensive.

How do I install KerasTuner?

You can install KerasTuner using pip by install keras-tuner in your terminal. It requires Python 3.8+ and TensorFlow 2.0+.

How does KerasTuner compare to Optuna?

KerasTuner is natively integrated with Keras and TensorFlow, making it the easiest choice for Keras users. Optuna is framework-agnostic and offers a more diverse set of search algorithms, which is better for those using multiple different ML frameworks.

Can I use KerasTuner for non-Keras models?

Yes, you can use KerasTuner to tune any model that can be wrapped in a function that returns a compiled model or a score. As long as the tuner can call the model-building function, it can optimize any library, including scikit-learn.

What is the difference between Random Search and Bayesian Optimization in KerasTuner?

Random Search samples hyperparameters randomly from the search space. Bayesian Optimization uses a probabilistic model to choose the next set of hyperparameters based on the results of previous trials, making it more efficient for finding the optimal values.

Does KerasTuner support distributed tuning?

Yes, KerasTuner supports distributed tuning using a chief-worker model. This requires setting specific environment variables like KERASTUNER_TUNER_ID and KERASTUNER_ORACLE_IP to coordinate workers.

How do I retrieve the best model after a search?

After the search process is complete, you can use the tuner.get_best_models(num_models=1)[0] method to retrieve the best performing model based on the objective you specified during initialization.