Monitor training progress
You need loss and metric charts while training a Keras model.
TensorFlow · Class
Logs Keras training information for visualization in TensorBoard. It can record metrics, the graph, weight histograms, throughput, profiling data, and embeddings.
TensorBoard is a Keras callback that writes training information to event files for visualization. Configure its log directory and optional logging frequencies, then pass the callback to a model-training method such as fit.
tf.keras.callbacks.TensorBoard(log_dir='logs', histogram_freq=0, write_graph=True, write_images=False, write_steps_per_second=False, update_freq='epoch', profile_batch=0, embeddings_freq=0, embeddings_metadata=None)| Name | Type | Description |
|---|---|---|
log_dir | str | Directory in which TensorBoard log files are written. |
histogram_freq | int | Epoch interval for computing layer weight histograms; zero disables them. |
write_graph | bool | Whether to write the model graph. |
write_images | bool | Whether to write model weights for image visualization. |
write_steps_per_second | bool | Whether to log training steps per second. |
update_freq | str or int | Logging cadence: "batch", "epoch", or an integer batch interval. |
profile_batch | int or str | Batches to profile; the documented default is 0. |
embeddings_freq | int | Frequency in epochs for embedding visualization. |
embeddings_metadata | str or dict | Metadata associated with embeddings. |
A TensorBoard callback instance that can be passed to Keras model methods through their callbacks argument.
write_steps_per_second=True adds training throughput information to the TensorBoard logs.histogram_freq=0 disables weight-histogram computation, so set a positive epoch frequency when those histograms are needed.import tensorflow as tf
(x_train, y_train), _ = tf.keras.datasets.mnist.load_data()
x_train = x_train[..., None].astype("float32") / 255.0
model = tf.keras.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28, 1)),
tf.keras.layers.Dense(10, activation="softmax"),
])
model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"])
callback = tf.keras.callbacks.TensorBoard(log_dir="./logs")
model.fit(x_train, y_train, epochs=2, callbacks=[callback])Creates TensorBoard event logs while fitting a Keras model; launch TensorBoard with tensorboard --logdir=./logs to inspect them.
You need loss and metric charts while training a Keras model.
You want to inspect model structure and layer-weight distributions.
You need to investigate training throughput or execution performance.
You want to examine learned representations.
tf.keras.callbacks.TensorBoard do?TensorBoard is a visualization tool, and this callback logs metrics summary plots, the training graph, weight histograms, and sampled profiling information. The callback is supplied to Keras training through the callbacks argument.
Follow-up: How would you inspect performance? Set profile_batch to select batches for profiling, then open the resulting logs in TensorBoard's profiling view.
log_dir identifies the directory where TensorBoard-readable log files are saved. A typical setup creates the callback with tf.keras.callbacks.TensorBoard(log_dir="./logs") and passes it to model.fit.
Follow-up: How can logging frequency be changed? Use update_freq="batch", "epoch", or an integer such as 1000 for periodic batch-based logging.
histogram_freq is measured in epochs and controls how often layer weight histograms are computed. A value of zero turns histogram computation off.
Follow-up: What additional image logging is available? Set write_images=True to write model weights for visualization as images in TensorBoard.
tf.keras.callbacks.Callbacktf.keras.Model.fitTensorBoard