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.
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:
- Training Loss: This metric measures how well the model is fitting the training data. A decreasing training loss indicates that the model is learning.
- 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.
Step 2: Define the Model
Define a simple model, like a neural network for a classification task.
Step 3: Train the Model with TensorBoard
Pass the defined TensorBoard callback to the model's fit method.
Visualizing Losses with TensorBoard
Once the model is trained and the logs are saved, run TensorBoard in a command line to visualize the logs.
In the TensorBoard UI:
- Navigate to the 'Scalars' tab.
- Select the
lossandval_lossmetrics 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
| Situation | Training Loss | Validation Loss | Potential Solutions |
| Overfitting | Low | High | Regularization, Reduce Model Complexity, More Data Early Stopping |
| Underfitting | High | High | More Complex Model, Train Longer, Improve Data Quality |
| Ideal Learning | Decreasing | Decreasing | Continue 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
- Tensorboard - visualize weights of LSTM
- Tensorboard AttributeError 'Model' object has no attribute '_get_distribution_strategy
- Tensorboard AttributeError 'ModelCheckpoint' object has no attribute 'on_train_batch_begin
- tensorboard command not found
- TensorBoard could not bind to port 6006, it was already in use
- TensorBoard doesn't show all data points
- Tensorboard graph recall
- TensorFlow - Implementation of MCTS

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 courseTrack 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.