TensorFlow
TensorBoard
machine learning
code separation
data visualization

How can I separate runs of my TensorFlow code in TensorBoard?

Master System Design with Codemia

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

Introduction

Separating experiments in TensorBoard is mostly a logging-directory design problem. If each training run writes to its own unique log directory, TensorBoard treats those directories as separate runs and comparisons stay clean.

Give Every Run Its Own Log Directory

The basic rule is simple: never point different experiments at the same event-file directory unless you intentionally want them merged.

python
1import os
2from datetime import datetime
3import tensorflow as tf
4
5run_name = datetime.now().strftime("run_%Y%m%d_%H%M%S")
6log_dir = os.path.join("logs", run_name)
7
8model = tf.keras.Sequential([
9    tf.keras.layers.Input(shape=(4,)),
10    tf.keras.layers.Dense(8, activation="relu"),
11    tf.keras.layers.Dense(3, activation="softmax"),
12])
13
14model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"])
15
16tb = tf.keras.callbacks.TensorBoard(log_dir=log_dir, histogram_freq=1)

Now each run writes event files to its own folder, which TensorBoard can display separately.

Use Meaningful Run Names

Timestamps are unique, but they are not very descriptive. It is often better to include key hyperparameters in the run name.

python
1lr = 1e-3
2batch_size = 64
3run_name = f"lr_{lr}_bs_{batch_size}_{datetime.now().strftime('%Y%m%d_%H%M%S')}"
4log_dir = os.path.join("logs", run_name)

This makes it much easier to compare runs visually without having to remember which timestamp belonged to which experiment.

A Minimal Training Example

python
1import numpy as np
2import tensorflow as tf
3
4x = np.random.randn(512, 4).astype("float32")
5y = np.random.randint(0, 3, size=(512,), dtype="int32")
6
7model = tf.keras.Sequential([
8    tf.keras.layers.Input(shape=(4,)),
9    tf.keras.layers.Dense(32, activation="relu"),
10    tf.keras.layers.Dense(3, activation="softmax"),
11])
12
13model.compile(
14    optimizer=tf.keras.optimizers.Adam(1e-3),
15    loss="sparse_categorical_crossentropy",
16    metrics=["accuracy"],
17)
18
19model.fit(x, y, epochs=5, batch_size=64, callbacks=[tb], verbose=0)
20print("logged to", log_dir)

Then launch TensorBoard against the parent directory:

bash
tensorboard --logdir logs

TensorBoard will treat each run subdirectory as a distinct experiment.

Keep Train and Validation Streams Organized

If you write custom summaries, use separate subdirectories or clear tag conventions for training and validation so the charts remain understandable.

python
1train_writer = tf.summary.create_file_writer(os.path.join(log_dir, "train"))
2val_writer = tf.summary.create_file_writer(os.path.join(log_dir, "val"))
3
4for step in range(3):
5    with train_writer.as_default():
6        tf.summary.scalar("loss", 1.0 / (step + 1), step=step)
7    with val_writer.as_default():
8        tf.summary.scalar("loss", 1.2 / (step + 1), step=step)

This keeps the metrics organized instead of mixing unrelated curves under the same tag.

Log Metadata Alongside Metrics

Run separation becomes much more useful when the logs record not just metrics, but also what changed between runs.

python
1from tensorboard.plugins.hparams import api as hp
2
3HP_LR = hp.HParam("learning_rate", hp.RealInterval(1e-4, 1e-2))
4METRIC = "accuracy"
5
6with tf.summary.create_file_writer(log_dir).as_default():
7    hp.hparams({HP_LR: 1e-3})
8    tf.summary.scalar(METRIC, 0.82, step=1)

Without metadata, separated runs are still harder to interpret than they should be.

Avoid Accidental Run Merging

One subtle source of confusion is accidentally reusing the same log_dir after restarting a script. That can be useful when intentionally resuming training, but it is the wrong behavior when you want clean experiment boundaries.

A good habit is to generate the run directory once per experiment and print it clearly to the console so you always know where the logs are going.

Common Pitfalls

A common mistake is reusing the same log directory across experiments and then being surprised when curves look merged or overwritten.

Another issue is using run names that are unique but meaningless, which makes cross-run comparison harder than it should be.

Developers also often log metrics without recording the hyperparameters or code version that produced them, which weakens the value of experiment tracking.

Summary

  • Separate runs by writing each experiment to its own unique log directory.
  • Put meaningful information such as hyperparameters in the run name.
  • Launch TensorBoard on the parent logs directory, not on only one run folder.
  • Keep training and validation summaries organized with separate writers or tags.
  • Log metadata alongside metrics so experiment comparisons stay useful.

Course illustration
Course illustration

All Rights Reserved.