TensorBoard
TensorBoard is a powerful visualization toolkit for machine learning experimentation, enabling tracking of metrics like loss and accuracy, visualization of model graphs, projection of embeddings, and much more. It is closely integrated with TensorFlow and PyTorch ecosystems, and its releases generally track TensorFlow versions. The current stable version is 2.20.0, and it is actively maintained with regular updates.
Warnings
- breaking TensorBoard.dev, the hosted sharing service, has been shut down. The `tensorboard dev upload` command will fail and the website is no longer accessible.
- gotcha TensorBoard plugin compatibility with Keras 3. While TensorFlow 2.16+ uses Keras 3 by default, some TensorBoard plugins' implementations may still primarily support Keras 2. This can lead to unexpected behavior or missing visualizations for Keras 3 models.
- gotcha Protobuf dependency conflicts can occur. TensorBoard's `protobuf` requirements have varied across versions (e.g., tight restrictions, then relaxations). This can cause installation errors or runtime issues if other installed libraries have conflicting `protobuf` version requirements.
- gotcha Python 3.13 compatibility requires TensorBoard version 2.20.0 or higher. Earlier versions will fail on Python 3.13 due to the removal of the `imghdr` module from the standard library, which TensorBoard previously used.
- gotcha When using `SummaryWriter` in notebook environments (e.g., Colab, Jupyter), it's highly recommended to call `writer.flush()` and `writer.close()` after logging data. This ensures all event files are properly written to disk and available for TensorBoard to render, preventing data loss or incomplete visualizations.
Install
-
pip install tensorboard
Imports
- SummaryWriter
from torch.utils.tensorboard import SummaryWriter
- TensorBoard callback for Keras
from tensorflow.keras.callbacks import TensorBoard
- tf.summary
import tensorflow as tf writer = tf.summary.create_file_writer('logs/my_experiment')
Quickstart
import datetime
from torch.utils.tensorboard import SummaryWriter
log_dir = "runs/" + datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
writer = SummaryWriter(log_dir)
# Log a scalar value
for i in range(100):
writer.add_scalar('Loss/train', 100 / (i + 1), i)
writer.add_scalar('Accuracy/train', i / 100, i)
writer.close()
print(f"TensorBoard logs saved to: {log_dir}")
print("To view, run in your terminal: tensorboard --logdir runs")