How to show training and predicted values on Tensorboard using python
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
TensorBoard is TensorFlow's built-in visualization toolkit that displays training metrics, model graphs, and custom data in a web dashboard. To show training loss, accuracy, and predicted values, you log data using tf.summary writers during training. TensorBoard reads these log files and renders interactive charts. You can log scalars (loss, accuracy), images, histograms, and custom text. For comparing predicted vs actual values, log them as scalars or create custom matplotlib figures and log them as images.
Basic Setup
Logging Training Metrics
The TensorBoard callback is the simplest way to log training metrics. It automatically records loss and all metrics specified in model.compile().
Logging Custom Scalars
Use the tag/subtag naming convention to group related metrics. TensorBoard displays loss/train and loss/validation on the same chart under the "loss" group.
Logging Predicted vs Actual Values
Classification Predictions with Confusion Matrix
Logging Weight Histograms
Histograms show how weight distributions change over training — useful for detecting vanishing/exploding gradients.
Comparing Multiple Runs
Using separate subdirectories under the same parent logs/ directory enables side-by-side comparison of runs.
Launching TensorBoard
Common Pitfalls
- Not calling
writer.flush()orwriter.close(): TensorBoard writers buffer data before writing to disk. If you do not flush, recent data may not appear in TensorBoard until the writer is closed or the buffer fills. Callwriter.flush()after each epoch for real-time updates. - Overwriting log directories between runs: Writing to the same log directory across different training runs mixes old and new data, creating confusing charts with discontinuities. Use unique subdirectories per run (e.g., timestamped names).
- Logging too frequently: Logging every training step generates massive log files and slows TensorBoard. Log scalars every N steps and images every N epochs. Use
update_freq="epoch"orupdate_freq=100in the callback. - Forgetting to close matplotlib figures: Each
plt.subplots()call creates a new figure object. Withoutplt.close(fig), figures accumulate in memory, causing memory leaks during long training runs. - Not adding the batch dimension to images:
tf.summary.image()expects a 4D tensor[batch, height, width, channels]. Forgettingtf.expand_dims(image, 0)raises a shape error.
Summary
- Use
keras.callbacks.TensorBoard(log_dir=...)for automatic logging of loss and metrics - Use
tf.summary.scalar()for custom metric logging andtf.summary.image()for plots - Log predicted vs actual values as scatter plots converted to TensorBoard images
- Use separate log subdirectories per run to enable side-by-side comparison
- Call
writer.flush()to ensure data appears in real-time - Launch with
tensorboard --logdir=logsand openhttp://localhost:6006
Related reading
- How to shuffle two numpy datasets using TensorFlow 2.0?
- How to simplify Tensorboard graph with shared variables?
- How to simulate reduced precision floats in TensorFlow?
- How to slice Tensorflow network into two maintaining gradient back-propagation?
- How to solve Cholesky decomposition error in Tensorflow caused by low precision datatype tf.float32?
- How to solve nan loss?
- How to skip the headers when processing a csv file using Python?
- How to smooth a curve for a dataset
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free 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.