How to write to TensorBoard in TensorFlow 2
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
TensorBoard is TensorFlow's built-in visualization toolkit that lets you monitor and debug your machine learning experiments. It provides dashboards for tracking metrics like loss and accuracy, visualizing model architectures, inspecting weight distributions, and displaying images or text during training. In TensorFlow 2, writing data to TensorBoard is straightforward thanks to the Keras callback API and the tf.summary module. This guide shows you how to use both approaches effectively.
Setting Up TensorBoard
TensorBoard is included when you install TensorFlow. To launch it, you point it at a log directory where your training summaries are stored:
This starts a local web server, typically at http://localhost:6006, where you can view your dashboards in a browser.
Using the Keras TensorBoard Callback
The simplest way to write training metrics to TensorBoard is through the built-in Keras callback. This requires no manual summary writing -- just pass the callback to model.fit:
Setting histogram_freq=1 tells TensorBoard to record weight and bias histograms every epoch, which is useful for spotting issues like vanishing or exploding gradients. The timestamp in the log directory lets you compare multiple training runs side by side.
Writing Custom Scalars with tf.summary
When you need to log custom metrics that are not part of the standard Keras training loop, use the tf.summary API directly. Create a SummaryWriter and use it as a context manager:
Every call to tf.summary.scalar records a single data point at the given step. TensorBoard then plots these points as line charts, letting you see how your metrics evolve over time.
Logging Images and Text
TensorBoard can display more than just numbers. You can log images to monitor what your model sees during training, and log text for debugging:
Image logging is particularly valuable for tasks like image generation, segmentation, or data augmentation verification, where you want to visually confirm what your model is producing at each stage.
Logging Histograms and Distributions
Histograms help you understand the distribution of weights, biases, and activations across your network. You can log them manually:
If you see weight distributions collapsing to near-zero or exploding to large values, that signals a problem with your learning rate or initialization strategy.
Comparing Multiple Runs
A key benefit of TensorBoard is comparing experiments. By writing logs to separate subdirectories, each run appears as a separate line on the dashboard:
Then launch TensorBoard with --logdir=logs/lr_experiment to see all three learning rates plotted together.
Common Pitfalls
- Writing all runs to the same directory: If multiple training runs write to the same log directory without unique subdirectories, their metrics overlap on the same chart and produce confusing, jumbled plots. Always include a timestamp or experiment name in the log path.
- Forgetting to set the step parameter: Every
tf.summarycall requires astepargument. Omitting it defaults to a global step counter that can produce unexpected x-axis values in your charts. Always pass an explicit step value for clarity. - Logging too frequently: Writing summaries every training step (rather than every N steps or every epoch) generates massive log directories and slows down both training and the TensorBoard UI. Log scalars every 100 steps and histograms or images every epoch as a reasonable starting point.
- Not flushing the writer: Summary data is buffered in memory and may not be written to disk immediately. If your training script crashes or you check TensorBoard mid-training, recent data might be missing. Call
writer.flush()periodically or use the writer as a context manager to ensure data is persisted. - Mixing TensorFlow 1.x summary API with TensorFlow 2: The old
tf.compat.v1.summaryAPI and session-basedFileWriterstill work in TF2 but should not be mixed with the newtf.summaryAPI. Mixing them leads to duplicate or missing logs. Use thetf.summaryAPI exclusively in TensorFlow 2 projects.
Summary
- Use the
tf.keras.callbacks.TensorBoardcallback for automatic logging of loss, metrics, weight histograms, and model graphs duringmodel.fit. - Use
tf.summary.create_file_writerandtf.summary.scalar,tf.summary.image,tf.summary.histogram, andtf.summary.textfor custom logging outside the Keras training loop. - Organize logs into separate subdirectories per experiment to enable side-by-side comparison of training runs in the TensorBoard UI.
- Launch TensorBoard with
tensorboard --logdir=./logsand openhttp://localhost:6006to view your dashboards. - Flush summary writers regularly and avoid logging at every single step to keep log sizes manageable and the TensorBoard interface responsive.

