How to use Keras TensorBoard callback for grid 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.
Introduction
TensorBoard is useful during hyperparameter tuning because it lets you compare loss curves, learning rates, and metrics across multiple runs. The main trick when combining it with a grid search is to give every training run its own log directory, otherwise different parameter combinations overwrite each other.
Why TensorBoard Needs Special Handling in Grid Search
A normal Keras training run can use a single TensorBoard callback and a fixed log_dir. That stops working once you loop through many parameter combinations, because each fit call writes event files to the same location.
For grid search you want:
- A unique log directory per parameter combination.
- Usually another unique suffix per validation fold.
- A reproducible mapping from run name to hyperparameters.
If you do not enforce that, TensorBoard will show mixed curves that are hard to interpret.
A Manual Grid Search Pattern That Works Well
The simplest reliable approach is to run the parameter grid yourself and create the callback inside the loop. This example uses ParameterGrid from scikit-learn and logs each run separately.
This pattern is explicit, easy to debug, and TensorBoard-friendly.
Launch TensorBoard and Compare Runs
After training, point TensorBoard at the base directory:
TensorBoard groups the event files by subdirectory, so each parameter combination becomes a separate run. If your naming convention includes the hyperparameters, comparison becomes much easier.
Practical naming advice:
- Keep run names short.
- Include the parameters that actually vary.
- Avoid timestamps unless you really need them, because deterministic names are easier to compare.
What About GridSearchCV With Keras Wrappers
You can wrap a Keras model for use with scikit-learn style search, but callback management becomes more awkward. The search object clones estimators and refits across folds, so you still need a way to create unique log directories per fit.
That is why many teams prefer one of these approaches:
- Manual
ParameterGridloops for full callback control. - KerasTuner for dedicated neural-network tuning workflows.
- Custom wrapper code that builds callbacks dynamically inside each fit call.
If TensorBoard analysis is important, manual loops are usually the least surprising option.
Logging More Than Scalars
TensorBoard can record more than training loss and accuracy. When useful, enable:
- Histograms for weights and activations.
- Profiling for selected batches.
- Embeddings for learned representations.
Those are powerful, but start with scalar metrics first. During a grid search, the biggest win usually comes from clear run separation, not from logging every possible artifact.
Common Pitfalls
- Reusing one
log_diracross every grid-search run. - Naming runs so vaguely that you cannot tell which hyperparameters produced them.
- Logging too much information and creating huge event files for a small tuning exercise.
- Using a wrapper-based search without thinking about how callbacks are recreated for each fit.
- Comparing runs that used different preprocessing pipelines and then blaming the network settings.
Summary
- The key to TensorBoard plus grid search is one log directory per run.
- A manual
ParameterGridloop is often the simplest way to keep TensorBoard output clean. - Build the
TensorBoardcallback inside the loop so each fit call gets its own path. - Launch TensorBoard on the parent directory to compare runs side by side.
- Keep preprocessing and run naming consistent, or the comparison will be misleading.
Related reading
- How to use Keras with GPU?
- How to use K.get_session in Tensorflow 2.0 or how to migrate it?
- How to use Merge layer concat function on Keras 2.0.0?
- How to use multilayered bidirectional LSTM in Tensorflow?
- How to use Keras Variational Autoencoder example with text data
- how to use model after trained in tensorflow save/load graph
- How to use log_loss scorer in gridsearchcv?
- How to use Model.fit which supports generators after fit_generator deprecation
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.