How to visualize a tensor summary in tensorboard
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
To visualize tensor summaries in TensorBoard, use tf.summary writers to log scalar, histogram, image, and text data during training, then launch TensorBoard to view the results. In TensorFlow 2, the workflow is: create a tf.summary.create_file_writer(), call tf.summary.scalar(), tf.summary.histogram(), or tf.summary.image() inside the training loop, and run tensorboard --logdir=logs to open the dashboard. TensorBoard reads the logged event files and renders interactive visualizations of loss curves, weight distributions, activation maps, and more.
Basic Setup
Scalar Summaries (Loss, Accuracy)
The most common visualization — tracking metrics over training steps:
Manual scalar logging in a custom training loop:
Histogram Summaries (Weights, Gradients)
Visualize the distribution of tensors over time:
In TensorBoard, histograms appear in the "Histograms" tab and show how tensor value distributions change across training steps.
Image Summaries
Log images to visualize inputs, predictions, or feature maps:
Text Summaries
Log text data like hyperparameters or sample predictions:
Custom Summaries with tf.summary.experimental
Launching TensorBoard
Common Pitfalls
- Forgetting
summary_writer.as_default()context: Summary operations only write to the active writer. Withoutwith summary_writer.as_default():,tf.summary.scalar()silently does nothing. Always wrap summary calls in the writer context manager. - Not flushing the writer: Summary data is buffered. If your script crashes or you check TensorBoard before training finishes, recent data may not appear. Call
summary_writer.flush()periodically or at the end of each epoch to force writes. - Stale logs from previous runs: TensorBoard reads all event files in the log directory. Old runs show up as overlapping curves. Use timestamped subdirectories (
logs/run_20240101_120000/) or delete old logs before starting a new experiment. - Wrong tensor shape for
tf.summary.image: Images must have shape(batch, height, width, channels)with values in[0, 1](float) or[0, 255](uint8). Forgetting to normalize or reshape causes blank images or errors. - Using
histogram_freqwithout validation data: TheTensorBoardcallback'shistogram_freqparameter logs weight histograms every N epochs, but only whenvalidation_datais provided tomodel.fit(). Without it, histograms are silently skipped.
Summary
- Use
tf.summary.scalar()for loss and accuracy curves,tf.summary.histogram()for weight distributions - Use
tf.summary.image()to visualize inputs, predictions, and feature maps - The
TensorBoardKeras callback handles most logging automatically withhistogram_freq=1 - For custom training loops, create a
tf.summary.create_file_writer()and use theas_default()context manager - Launch with
tensorboard --logdir=logsand use timestamped subdirectories to separate experiments
Related reading
- How to visualize output of intermediate layers of convolutional neural network in keras?
- How to visualize RNN/LSTM gradients in Keras/TensorFlow?
- How to wrap a custom TensorFlow loss function in Keras?
- How to write a custom loss function in Tensorflow?
- How to visualize a TFRecord?
- How to visualize learned filters on tensorflow
- How to work with Tensorflow on Android platform?
- How to work with TF Lite library in a c project
.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.