TensorBoard
training losses
validation losses
graph plotting
machine learning visualization

TensorBoard - Plot training and validation losses on the same graph?

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

TensorBoard is an invaluable tool for machine learning practitioners, offering a suite of visualizations that make it easier to understand, debug, and optimize models. Among its capabilities, one of the most critical is plotting training and validation losses on the same graph. Given the significance of monitoring these metrics, let's delve into how TensorBoard can be employed to its full extent for this purpose.

Why Plot Training and Validation Losses?

Visualizing training and validation losses together provides an immediate understanding of how well a model is learning and generalizing:

  1. Training Loss: This metric measures how well the model is fitting the training data. A decreasing training loss indicates that the model is learning.
  2. Validation Loss: By assessing the model's performance on unseen data, this metric helps in understanding its ability to generalize.

Plotting these together helps detect overfitting—when a model learns the training data too well and performs poorly on the validation data. A rising validation loss compared to a decreasing training loss is a classic indicator of this issue.

TensorBoard Setup

Step 1: Import TensorBoard and Initialize

First, ensure you have TensorBoard installed. It typically comes bundled with TensorFlow. Begin by importing TensorBoard, and initializing it in your script.

python
1import tensorflow as tf
2from tensorboard.plugins.hparams import api as hp
3
4# Enabling TensorBoard logging to save logs
5log_dir = "logs/"
6tensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir=log_dir, histogram_freq=1)

Step 2: Define the Model

Define a simple model, like a neural network for a classification task.

python
1model = tf.keras.models.Sequential([
2    tf.keras.layers.Dense(128, activation='relu', input_shape=(input_shape,)),
3    tf.keras.layers.Dense(64, activation='relu'),
4    tf.keras.layers.Dense(10, activation='softmax')
5])
6
7model.compile(optimizer='adam', 
8              loss='sparse_categorical_crossentropy', 
9              metrics=['accuracy'])

Step 3: Train the Model with TensorBoard

Pass the defined TensorBoard callback to the model's fit method.

python
1history = model.fit(train_data, 
2                    train_labels, 
3                    validation_data=(val_data, val_labels),
4                    epochs=30, 
5                    callbacks=[tensorboard_callback])

Visualizing Losses with TensorBoard

Once the model is trained and the logs are saved, run TensorBoard in a command line to visualize the logs.

bash
tensorboard --logdir=logs/

In the TensorBoard UI:

  • Navigate to the 'Scalars' tab.
  • Select the loss and val_loss metrics to visualize them together.
  • Analyze: A synchronized drop or convergence of both losses suggests good performance. Divergence may indicate overfitting or underfitting.

Practical Use Case: Diagnose Model Behavior

Overfitting

A clear separation where the training loss decreases while the validation loss starts increasing is a sign of overfitting. Address it by:

  • Regularization: Apply techniques like L1/L2 regularization or dropout.
  • Simplifying the Model: Reduce the model's complexity.
  • More Data: Expand the dataset size to expose the model to varied examples.

Underfitting

If both losses are high and don't decrease, the model might be underfitting:

  • Complexify the Model: Introduce more layers or units.
  • Train Longer: Sometimes, it simply needs more time to learn from the data.

Table: Strategies for Model Improvement

SituationTraining LossValidation LossPotential Solutions
OverfittingLowHighRegularization, Reduce Model Complexity, More Data Early Stopping
UnderfittingHighHighMore Complex Model, Train Longer, Improve Data Quality
Ideal LearningDecreasingDecreasingContinue Monitoring Might adjust learning rate for fine-tuning

Additional TensorBoard Features

  • Histograms: Analyze the distribution of weights or activations.
  • Projector: Visualize embeddings (e.g., word embeddings).
  • Graphs: View the computational graph to ensure that the model's architecture is correct.

Conclusion

By plotting training and validation losses with TensorBoard, practitioners can quickly assess model performance, diagnose issues, and implement changes efficiently. This powerful visualization technique is integral to modern machine learning workflows, offering clear insights that are pivotal for optimizing model architectures and achieving better results.


Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the 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.