TensorBoard
model visualization
training monitoring
deep learning
machine learning

Using Tensorboard to monitor training real time and visualize the model architecture

Master System Design with Codemia

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

Introduction

TensorBoard is the standard tool for monitoring TensorFlow training runs, tracking metrics as they change, and inspecting model graphs. It is most useful when you treat training as an observable system rather than a blind batch job. The basic pattern is simple: write event logs during training, launch TensorBoard against those logs, and inspect metrics, graphs, and optional performance profiles in the browser.

Create a Clean Log Directory

TensorBoard reads event files from a log directory. A timestamped run directory keeps experiments isolated and makes comparisons easier.

python
1from datetime import datetime
2from pathlib import Path
3
4base_log_dir = Path("logs")
5run_name = datetime.now().strftime("run-%Y%m%d-%H%M%S")
6log_dir = base_log_dir / run_name
7log_dir.mkdir(parents=True, exist_ok=True)
8
9print(log_dir)

Without per-run directories, unrelated training sessions can be merged into one confusing dashboard.

Log Training Metrics with the Keras Callback

For Keras workflows, the built-in TensorBoard callback is the fastest way to get live metric tracking.

python
1import tensorflow as tf
2import numpy as np
3
4x = np.random.rand(512, 10).astype("float32")
5y = np.random.randint(0, 2, size=(512,))
6
7model = tf.keras.Sequential([
8    tf.keras.layers.Input(shape=(10,)),
9    tf.keras.layers.Dense(32, activation="relu"),
10    tf.keras.layers.Dense(1, activation="sigmoid"),
11])
12
13model.compile(optimizer="adam", loss="binary_crossentropy", metrics=["accuracy"])
14
15tb_callback = tf.keras.callbacks.TensorBoard(
16    log_dir=str(log_dir),
17    histogram_freq=1,
18    write_graph=True,
19)
20
21model.fit(
22    x,
23    y,
24    epochs=5,
25    batch_size=32,
26    validation_split=0.2,
27    callbacks=[tb_callback],
28)

This writes training and validation metrics to event files while the model is running.

Launch TensorBoard

Once the logs exist, point TensorBoard at the parent log directory.

bash
tensorboard --logdir logs

In notebook environments, you can launch it inline:

python
%load_ext tensorboard
%tensorboard --logdir logs

After opening the URL, the Scalars tab shows how loss and accuracy evolve over time. This is the most common place to catch overfitting, stalled learning, or unstable training.

Visualize the Model Architecture

TensorBoard can display the model graph when graph logging is enabled. For many Keras models, write_graph=True is enough to populate the Graphs tab.

If you also want a static architecture image for documentation:

python
1tf.keras.utils.plot_model(
2    model,
3    to_file="model.png",
4    show_shapes=True,
5    show_layer_names=True,
6)

The TensorBoard graph is interactive and helpful for exploring the computational structure. The static diagram is more useful for docs and reviews.

Add Custom Scalars and Images

Built-in metrics are often not enough. TensorBoard also supports custom summaries for domain-specific values.

python
1writer = tf.summary.create_file_writer(str(log_dir / "custom"))
2
3for step, value in enumerate([0.9, 0.7, 0.55, 0.42]):
4    with writer.as_default():
5        tf.summary.scalar("custom_loss_estimate", value, step=step)
6    writer.flush()

You can also log images:

python
1image = tf.random.uniform((1, 64, 64, 3))
2with writer.as_default():
3    tf.summary.image("sample", image, step=0)
4writer.flush()

This is useful for generated outputs, segmentation previews, or intermediate model artifacts that are hard to understand from scalar metrics alone.

Compare Multiple Runs

TensorBoard becomes much more powerful when you compare several experiments in one place. Keep separate run directories for different hyperparameters, architectures, or datasets.

Example structure:

  • 'logs/lr-1e-3'
  • 'logs/lr-1e-4'
  • 'logs/wider-model'

When these live under the same parent directory, TensorBoard overlays them in the Scalars tab. That makes it much easier to compare convergence and stability.

Use Profiling Selectively

TensorBoard can also profile performance. That includes step timing, input pipeline behavior, and device utilization.

python
1tb_callback = tf.keras.callbacks.TensorBoard(
2    log_dir=str(log_dir),
3    profile_batch="2,4",
4)

Profile only selected batches. Profiling everything adds overhead and can distort the behavior you are trying to measure.

Common Pitfalls

  • Writing several unrelated experiments into one log directory.
  • Expecting TensorBoard to show data when no event files are being written.
  • Forgetting to flush custom summaries in long-running scripts.
  • Treating the graph view as proof that the model is correct without validating shapes and outputs.
  • Enabling expensive profiling on every batch.

Summary

  • TensorBoard reads event logs written during training.
  • The Keras TensorBoard callback is the easiest way to log live metrics.
  • Separate run directories make comparisons meaningful.
  • Graph and image views help with architecture inspection and debugging.
  • Profiling and custom summaries make TensorBoard useful beyond simple loss curves.

Course illustration
Course illustration

All Rights Reserved.