TensorBoard
machine learning
deep learning
model training
data visualization

Logging training and validation loss in tensorboard

Master System Design with Codemia

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

Introduction

TensorBoard becomes useful the moment you can compare training loss and validation loss on the same run. Those two curves tell you whether the model is learning, overfitting, plateauing, or simply misconfigured.

In Keras, the easiest approach is to use the built-in TensorBoard callback and provide validation data to model.fit. If you are using a custom training loop, then you write the scalar summaries yourself with tf.summary.

The Simple Keras Approach

When you train through model.fit, Keras can log both training and validation metrics automatically.

python
1import tensorflow as tf
2
3model = tf.keras.Sequential([
4    tf.keras.layers.Input(shape=(10,)),
5    tf.keras.layers.Dense(32, activation="relu"),
6    tf.keras.layers.Dense(1),
7])
8
9model.compile(optimizer="adam", loss="mse")
10
11x = tf.random.normal((512, 10))
12y = tf.reduce_sum(x, axis=1, keepdims=True)
13
14callback = tf.keras.callbacks.TensorBoard(log_dir="logs/run1")
15
16model.fit(
17    x,
18    y,
19    validation_split=0.2,
20    epochs=5,
21    callbacks=[callback],
22    verbose=1,
23)

Then start TensorBoard:

bash
tensorboard --logdir logs

If validation data is provided, Keras logs both loss and val_loss. If you do not provide validation data or validation_split, you will only see training loss.

Keep Run Directories Organized

One of the easiest ways to make TensorBoard useless is to dump unrelated experiments into the same log directory. Give each run its own path.

python
run_name = "lr_1e-3_batch32"
log_dir = f"logs/exp1/{run_name}"
callback = tf.keras.callbacks.TensorBoard(log_dir=log_dir)

That makes side-by-side experiment comparison much easier later. A clean directory structure is often more valuable than any single extra metric.

Manual Logging in a Custom Training Loop

If you are not using model.fit, log the losses yourself with summary writers.

python
1import tensorflow as tf
2
3train_writer = tf.summary.create_file_writer("logs/custom/train")
4val_writer = tf.summary.create_file_writer("logs/custom/val")
5
6for epoch in range(5):
7    train_loss = tf.constant(0.5 / (epoch + 1), dtype=tf.float32)
8    val_loss = tf.constant(0.6 / (epoch + 1), dtype=tf.float32)
9
10    with train_writer.as_default():
11        tf.summary.scalar("loss", train_loss, step=epoch)
12
13    with val_writer.as_default():
14        tf.summary.scalar("loss", val_loss, step=epoch)

This gives you full control over what is logged and when. It is especially useful when training logic is too custom for the stock callback.

What the Curves Tell You

A few common patterns are worth knowing:

  • Training loss down, validation loss down: healthy learning.
  • Training loss down, validation loss flat or up: likely overfitting.
  • Both losses high and flat: underfitting, bad learning rate, or data issues.
  • Both losses noisy: unstable optimization or too-small batches.

TensorBoard is not just for pretty charts; it is a debugging tool. The point of logging loss is to guide decisions about model capacity, regularization, learning rate, and data quality.

Logging Frequency

Per-epoch logging is enough for many projects. Per-batch logging gives more detail, but it also creates larger event files and noisier charts.

Use finer-grained logging only when you actually need to inspect within-epoch behavior, such as exploding loss, unstable batches, or curriculum-style training changes.

Common Pitfalls

A common mistake is expecting val_loss to appear without supplying validation data. TensorBoard cannot log a validation curve if the model never computes validation metrics.

Another issue is reusing the same log directory across unrelated runs. That blends curves together and makes the dashboard hard to interpret.

Developers also sometimes mix step semantics between training and validation summaries, such as logging training per batch and validation per epoch with the same step meanings. Keep the x-axis meaning consistent.

Finally, do not overinterpret tiny short-run fluctuations. TensorBoard helps you see trends, not just single-point noise.

Summary

  • With model.fit, use the TensorBoard callback and provide validation data to get both loss and val_loss.
  • For custom loops, log scalar summaries manually with tf.summary.
  • Keep each experiment run in its own log directory.
  • Use the loss curves to diagnose learning progress, overfitting, and instability.
  • Make sure your logging frequency and step semantics stay consistent.

Course illustration
Course illustration

All Rights Reserved.