TensorFlow
hyperparameter tuning
machine learning
deep learning
neural networks

Hyperparameter tune for Tensorflow

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Hyperparameter tuning in TensorFlow means searching for training settings that improve validation performance, not just changing numbers at random until something works. The important part is to define a realistic search space, run a repeatable evaluation loop, and keep the test set separate until tuning is finished.

What Counts as a Hyperparameter

Hyperparameters are choices around training rather than values learned by gradient descent. Common examples include:

  • learning rate
  • optimizer type
  • number of layers
  • units per layer
  • dropout rate
  • batch size
  • number of epochs

These settings affect both model quality and training cost, which is why tuning is usually a balance between accuracy and compute budget.

Start with a Baseline Model

Before using a tuning library, establish one sensible baseline model so you know what "better" means.

python
1import tensorflow as tf
2
3model = tf.keras.Sequential([
4    tf.keras.layers.Input(shape=(784,)),
5    tf.keras.layers.Dense(64, activation="relu"),
6    tf.keras.layers.Dropout(0.2),
7    tf.keras.layers.Dense(10, activation="softmax"),
8])
9
10model.compile(
11    optimizer=tf.keras.optimizers.Adam(learning_rate=1e-3),
12    loss="sparse_categorical_crossentropy",
13    metrics=["accuracy"],
14)

That baseline gives you a reference point before the search space becomes more complex.

KerasTuner is a practical way to tune TensorFlow models because it lets you describe hyperparameters inside a model-building function.

python
1import tensorflow as tf
2import keras_tuner as kt
3
4
5def build_model(hp):
6    model = tf.keras.Sequential()
7    model.add(tf.keras.layers.Input(shape=(784,)))
8
9    units = hp.Int("units", min_value=32, max_value=256, step=32)
10    dropout = hp.Float("dropout", min_value=0.0, max_value=0.5, step=0.1)
11    learning_rate = hp.Choice("learning_rate", values=[1e-2, 1e-3, 1e-4])
12
13    model.add(tf.keras.layers.Dense(units, activation="relu"))
14    model.add(tf.keras.layers.Dropout(dropout))
15    model.add(tf.keras.layers.Dense(10, activation="softmax"))
16
17    model.compile(
18        optimizer=tf.keras.optimizers.Adam(learning_rate=learning_rate),
19        loss="sparse_categorical_crossentropy",
20        metrics=["accuracy"],
21    )
22    return model

Then create a tuner and run the search:

python
1tuner = kt.RandomSearch(
2    build_model,
3    objective="val_accuracy",
4    max_trials=10,
5    directory="tuning",
6    project_name="mnist_demo",
7)
8
9tuner.search(
10    x_train,
11    y_train,
12    validation_data=(x_val, y_val),
13    epochs=10,
14    callbacks=[tf.keras.callbacks.EarlyStopping(patience=2)],
15)

This gives you a repeatable tuning loop instead of manual guesswork.

Choose the Search Strategy Deliberately

A bigger search space is not automatically better. In practice:

  • grid search is useful only for very small spaces
  • random search is often a strong default
  • Bayesian or adaptive search is useful when trials are expensive

Random search works surprisingly well because not every hyperparameter matters equally. A broad search over a few meaningful values often beats a rigid grid over too many combinations.

Evaluate the Best Result Properly

After the search, inspect the best settings, rebuild the model, and train it cleanly before final evaluation.

python
1best_hp = tuner.get_best_hyperparameters(num_trials=1)[0]
2
3print(best_hp.get("units"))
4print(best_hp.get("dropout"))
5print(best_hp.get("learning_rate"))
6
7best_model = tuner.hypermodel.build(best_hp)

Then evaluate that final model on a held-out test set. The validation data drives tuning; the test data should stay untouched until the end.

Keep the Search Space Realistic

The most common tuning mistake is searching too many things at once with too few trials. For example, trying to vary optimizer type, layer count, width, dropout, regularization, batch size, and learning rate all at once can make the search noisy rather than informative.

A better approach is to search over values that are plausible for the model family and dataset size. That makes each trial more meaningful and reduces wasted compute.

Common Pitfalls

Tuning against the test set leaks evaluation information and makes the final result overly optimistic.

Searching too many hyperparameters at once without enough trials usually produces noise instead of insight.

Ignoring early stopping wastes compute on clearly bad configurations.

Using a search space with unrealistic values can make the tuner spend most of its budget on obviously poor models.

Trusting one lucky run without checking reproducibility can lead to fragile conclusions.

Summary

  • Hyperparameter tuning should begin with a stable baseline model.
  • Define a realistic search space for learning rate, width, dropout, and related settings.
  • KerasTuner is a practical way to automate structured search in TensorFlow.
  • Use validation data for tuning and keep the test set separate for final evaluation.
  • Early stopping and disciplined search spaces save time and improve signal quality.

Course illustration
Course illustration

All Rights Reserved.