How to display custom metrics in TensorBoard?

Published on Aug. 22, 2023, 12:19 p.m.

To display custom metrics in TensorBoard, you need to log the metrics during training using a TensorFlow SummaryWriter object. You can then use the TensorBoard web interface to visualize the metrics.

Here is an example of how to log a custom metric during training using a SummaryWriter object:

from torch.utils.tensorboard import SummaryWriter

# create a SummaryWriter
writer = SummaryWriter()

# during training, log the custom metric value
for i, (inputs, labels) in enumerate(train_loader):
    # train the model on this batch
    # calculate the custom metric value
    custom_metric_value = ...
    # log the metric value
    writer.add_scalar('custom_metric', custom_metric_value, i)

# close the SummaryWriter
writer.close()

Then, you can launch TensorBoard using the command tensorboard --logdir=path/to/logdir and navigate to the Scalars tab to view the custom metric value.

You can also customize the visualization of the custom metric using the add_scalar() method, such as specifying a tag and adding metadata to the metric, for example:

writer.add_scalar('custom_metrics/accuracy', accuracy, global_step=epoch, description='Accuracy metric')

By default, the custom scalar is plotted on the Y-axis of the scalar dashboard.