Keras
TensorBoard
Training Metrics
Validation Scalars
Data Visualization

keras tensorboard plot train and validation scalars in a same figure

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

If you want TensorBoard to show training and validation metrics together, the main requirement is consistent logging structure. Keras already logs both kinds of metrics when you use validation_data or validation_split. Most problems come from log-directory layout or custom tag naming, not from TensorBoard lacking the capability.

Start With the Built-In TensorBoard Callback

For many projects, the default Keras callback is enough.

python
1import tensorflow as tf
2from tensorflow import keras
3
4x = tf.random.normal((1000, 20))
5y = tf.cast(tf.reduce_sum(x[:, :3], axis=1) > 0, tf.float32)
6
7x_train, x_val = x[:800], x[800:]
8y_train, y_val = y[:800], y[800:]
9
10model = keras.Sequential([
11    keras.layers.Input(shape=(20,)),
12    keras.layers.Dense(32, activation="relu"),
13    keras.layers.Dense(1, activation="sigmoid"),
14])
15
16model.compile(optimizer="adam", loss="binary_crossentropy", metrics=["accuracy"])
17
18tb = keras.callbacks.TensorBoard(log_dir="logs/run1", update_freq="epoch")
19
20model.fit(
21    x_train,
22    y_train,
23    validation_data=(x_val, y_val),
24    epochs=5,
25    callbacks=[tb],
26)

With validation data supplied, Keras logs both training and validation metrics during fit.

Keep One Run Directory Per Experiment

The simplest structure is one directory per experiment, such as logs/run1, logs/run2, and so on. Let both training and validation metrics for that run live under the same run directory.

bash
tensorboard --logdir logs --port 6006

If training metrics go into one tree and validation metrics go into another unrelated tree, the comparison becomes harder and the dashboard may look like separate experiments instead of one run with two curves.

Use Custom Tags When You Need Tighter Grouping

If the default tags are not organized the way you want, log them manually in a callback.

python
1class GroupedScalarCallback(keras.callbacks.Callback):
2    def __init__(self, log_dir):
3        super().__init__()
4        self.writer = tf.summary.create_file_writer(log_dir)
5
6    def on_epoch_end(self, epoch, logs=None):
7        logs = logs or {}
8        with self.writer.as_default():
9            if "loss" in logs:
10                tf.summary.scalar("loss/train", logs["loss"], step=epoch)
11            if "val_loss" in logs:
12                tf.summary.scalar("loss/validation", logs["val_loss"], step=epoch)
13            if "accuracy" in logs:
14                tf.summary.scalar("accuracy/train", logs["accuracy"], step=epoch)
15            if "val_accuracy" in logs:
16                tf.summary.scalar("accuracy/validation", logs["val_accuracy"], step=epoch)
17            self.writer.flush()

This gives you more predictable grouping in the Scalars dashboard.

Read the Curves, Not Only the Final Numbers

Putting train and validation metrics together matters because curve shape is often more informative than the last value. For example:

  • training loss falls but validation loss rises: overfitting
  • both curves stall early: optimization or capacity issue
  • validation jumps wildly: unstable training or small validation set

That is why side-by-side plotting is useful. It speeds up diagnosis.

Keep Experiment Metadata Close to the Logs

If several runs share the same log root, include enough naming structure to remember what changed between experiments, such as optimizer, learning rate, or dataset version. Otherwise train-versus-validation plots may be visible in TensorBoard but still hard to interpret because you no longer remember what each run actually represents.

Clean run naming sounds mundane, but it is often the difference between useful experiment review and a dashboard full of unlabeled curves. Once several team members are logging runs into the same root, that discipline stops TensorBoard from becoming noise.

Common Pitfalls

  • Forgetting to pass validation data, so no validation metrics are logged.
  • Writing training and validation metrics into unrelated log trees.
  • Changing metric names between runs and making comparisons harder than necessary.
  • Using custom logging without checking that tags are consistent across experiments.

Summary

  • Keras can already log training and validation metrics through the built-in TensorBoard callback.
  • Keep both metric streams under one run directory for each experiment.
  • Use custom tags only when you need tighter grouping or naming control.
  • Compare curve behavior, not just final scalar values.
  • Clean log structure is the simplest way to get useful train-versus-validation plots.

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.