C.W.K.
Stream
Lesson 02 of 04 · published

tf.summary API — Custom Logging

~11 min · tf-summary, scalars, images, histograms

Level 0Level 0
0 XP0/78 lessons0/17 achievements
0/100 XP to next level100 XP to go0% complete

Beyond what model.fit logs automatically

The Keras TensorBoard callback handles standard metrics. For custom metrics, sample images, attention maps, embedding projections, or anything not covered by model.fit, use the tf.summary API directly.

Pattern: create a writer, open it as a context, call tf.summary.scalar / image / histogram / text with a name, value, and step. Use separate writers for train and validation logs so they appear as separate series in the dashboard.

Code

Custom logging — scalar / image / histogram·python
import tensorflow as tf
import datetime

log_dir = "logs/" + datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
train_writer = tf.summary.create_file_writer(log_dir + "/train")
val_writer   = tf.summary.create_file_writer(log_dir + "/val")

# Scalars (run inside the training loop, per epoch)
with train_writer.as_default():
    tf.summary.scalar("loss",           train_loss,    step=epoch)
    tf.summary.scalar("accuracy",       train_accuracy, step=epoch)
    tf.summary.scalar("learning_rate",  lr_schedule(epoch), step=epoch)

with val_writer.as_default():
    tf.summary.scalar("loss",     val_loss,     step=epoch)
    tf.summary.scalar("accuracy", val_accuracy, step=epoch)

# Images (shape: batch, h, w, channels; values [0,1] or [0,255])
with train_writer.as_default():
    tf.summary.image("training_samples", images, step=epoch, max_outputs=5)

# Histograms — watch weights evolve
with train_writer.as_default():
    for layer in model.layers:
        for weight in layer.weights:
            tf.summary.histogram(weight.name, weight, step=epoch)

# Text — log sample predictions, hparams, anything string-shaped
with train_writer.as_default():
    tf.summary.text("sample", "Predicted: cat, Actual: cat", step=epoch)

train_writer.flush()
val_writer.flush()
Custom training loop with summaries·python
import tensorflow as tf

train_loss_metric = tf.keras.metrics.Mean(name='train_loss')
train_acc_metric  = tf.keras.metrics.SparseCategoricalAccuracy(name='train_accuracy')
train_writer = tf.summary.create_file_writer("logs/custom_training")

@tf.function
def train_step(x, y):
    with tf.GradientTape() as tape:
        predictions = model(x, training=True)
        loss = loss_fn(y, predictions)
    grads = tape.gradient(loss, model.trainable_weights)
    optimizer.apply_gradients(zip(grads, model.trainable_weights))
    train_loss_metric.update_state(loss)
    train_acc_metric.update_state(y, predictions)

for epoch in range(NUM_EPOCHS):
    train_loss_metric.reset_state()
    train_acc_metric.reset_state()

    for x_batch, y_batch in train_dataset:
        train_step(x_batch, y_batch)

    with train_writer.as_default():
        tf.summary.scalar("loss",     train_loss_metric.result(), step=epoch)
        tf.summary.scalar("accuracy", train_acc_metric.result(),  step=epoch)

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.