The TensorBoard callback automatically writes training and validation loss/metrics to log files after every epoch, which can then be visualized as interactive, live-updating charts by running the separate tensorboard command-line tool pointed at log_dir. It's especially useful for comparing multiple training runs side by side, visually spotting overfitting from diverging train/validation curves, and inspecting the model's computational graph and weight/gradient histograms, all without needing to manually collect and plot metrics yourself.
1Understanding callbacks.TensorBoard()
The TensorBoard callback automatically writes training and validation loss/metrics to log files after every epoch, which can then be visualized as interactive, live-updating charts by running the separate tensorboard command-line tool pointed at log_dir. It's especially useful for comparing multiple training runs side by side, visually spotting overfitting from diverging train/validation curves, and inspecting the model's computational graph and weight/gradient histograms, all without needing to manually collect and plot metrics yourself.
Use a distinct, timestamped subdirectory under log_dir for each separate training run, so TensorBoard can display and compare multiple runs' curves side by side instead of overwriting or mixing together the logs from different experiments.
import tensorflow as tf
callback = tf.keras.callbacks.TensorBoard(log_dir='logs/run1')
print(callback.log_dir)2Practical Example
Here is a real-world application of callbacks.TensorBoard() showing how it is used in production TensorFlow code.
import tensorflow as tf
from tensorflow.keras import layers, Sequential
import numpy as np
import os
model = Sequential([layers.Dense(1, input_shape=(1,))])
model.compile(optimizer='sgd', loss='mse')
callback = tf.keras.callbacks.TensorBoard(log_dir='logs/run2')
x, y = np.array([1, 2, 3, 4]), np.array([2, 4, 6, 8])
model.fit(x, y, epochs=3, callbacks=[callback], verbose=0)
print(os.path.isdir('logs/run2'))3Best Practices
Follow these guidelines when working with callbacks.TensorBoard():
1. Use a separate, uniquely named subdirectory per training run under log_dir, so different experiments can be compared side by side in TensorBoard rather than overwritten
2. Pass the TensorBoard callback to fit()'s callbacks list alongside EarlyStopping/ModelCheckpoint, rather than as a replacement for them, since they serve different purposes
3. Launch the TensorBoard tool pointed at the parent log directory to visually compare loss/metric curves across all logged runs at once
Tip: Use a distinct, timestamped subdirectory under log_dir for each separate training run, so TensorBoard can display and compare multiple runs' curves side by side instead of overwriting or mixing together the logs from different experiments.
import tensorflow as tf
callback = tf.keras.callbacks.TensorBoard(log_dir='logs/run1')
print(callback.log_dir)