TensorFlow
hyperparameters
random search
machine learning
neural networks

Tensorflow - How to implement hyper parameters random search?

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

Random search is a practical and efficient way to tune TensorFlow model hyperparameters without exploring every possible combination. Compared with grid search, it often finds strong configurations faster when only a few parameters matter most. This article shows an end-to-end implementation with Keras Tuner.

Why Random Search Works Well

Hyperparameter spaces can grow quickly with learning rates, layer sizes, dropout values, and optimizer choices. Random search samples combinations broadly, which is often better than dense grids in high-dimensional spaces.

Install dependencies:

bash
pip install tensorflow keras-tuner

Define a Search Space

Create a model-building function with tunable parameters.

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=(28, 28)))
8    model.add(tf.keras.layers.Flatten())
9
10    units = hp.Int("units", min_value=64, max_value=256, step=64)
11    dropout = hp.Float("dropout", min_value=0.1, max_value=0.5, step=0.1)
12    lr = hp.Choice("learning_rate", values=[1e-2, 1e-3, 5e-4])
13
14    model.add(tf.keras.layers.Dense(units, activation="relu"))
15    model.add(tf.keras.layers.Dropout(dropout))
16    model.add(tf.keras.layers.Dense(10, activation="softmax"))
17
18    model.compile(
19        optimizer=tf.keras.optimizers.Adam(learning_rate=lr),
20        loss="sparse_categorical_crossentropy",
21        metrics=["accuracy"],
22    )
23    return model

Use validation accuracy as the objective.

python
1(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
2x_train = x_train.astype("float32") / 255.0
3x_test = x_test.astype("float32") / 255.0
4
5tuner = kt.RandomSearch(
6    hypermodel=build_model,
7    objective="val_accuracy",
8    max_trials=10,
9    executions_per_trial=1,
10    directory="tuning_runs",
11    project_name="mnist_random_search",
12)
13
14tuner.search(
15    x_train,
16    y_train,
17    epochs=5,
18    validation_split=0.2,
19    callbacks=[tf.keras.callbacks.EarlyStopping(monitor="val_loss", patience=2)],
20)

This explores ten random combinations and keeps the top performers.

Retrieve Best Hyperparameters and Re-Train

After search, train a final model on the best configuration.

python
1best_hp = tuner.get_best_hyperparameters(num_trials=1)[0]
2print(best_hp.values)
3
4best_model = tuner.hypermodel.build(best_hp)
5best_model.fit(x_train, y_train, epochs=8, validation_split=0.2)
6
7test_loss, test_acc = best_model.evaluate(x_test, y_test)
8print("test accuracy:", test_acc)

You can store selected values in config files to keep experiments reproducible.

Practical Search Design Tips

  • start with broad ranges, then narrow in later runs
  • cap training epochs and use early stopping for search speed
  • track random seeds for comparability
  • reserve separate test data for final evaluation only

Random search is iterative. A second pass with refined ranges often yields better models at lower cost.

Scaling Random Search in Practice

As projects grow, you may want more trials and better experiment tracking. Two practical improvements are callback logging and deterministic seeds.

python
1import numpy as np
2import tensorflow as tf
3
4np.random.seed(42)
5tf.random.set_seed(42)

You can also inspect full trial history:

python
for trial in tuner.oracle.get_best_trials(num_trials=5):
    print(trial.hyperparameters.values, trial.score)

If compute allows, run several short random searches with different seeds, then merge promising ranges into a narrower second stage. This staged approach often beats one large unfocused run and keeps total training cost under control.

Track both validation score and training time per trial. A slightly lower metric can be preferable if it is much cheaper to train and deploy at scale.

When tuning on shared infrastructure, set trial-level resource limits to avoid one experiment starving other workloads.

Document the final best hyperparameters in experiment notes so model retraining remains repeatable across team members.

Common Pitfalls

  • Searching too many parameters at once with too few trials.
  • Evaluating on test data during tuning, which inflates reported performance.
  • Using long training schedules during search and wasting compute.
  • Ignoring trial logs and repeating already poor parameter ranges.
  • Not saving tuner artifacts, making results hard to reproduce.

Summary

  • Random search is an efficient baseline for TensorFlow hyperparameter tuning.
  • Keras Tuner makes search spaces and trials easy to manage.
  • Use early stopping and bounded trial counts to control cost.
  • Re-train best settings and evaluate once on held-out test data.
  • Iterate search ranges based on first-run results for better outcomes.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.