from torch.utils.tensorboard import SummaryWriter writer = SummaryWriter('logs') for i, batch in enumerate(train_loader): loss = train(batch) writer.add_scalar('training_loss', loss, i)
from torch.utils.tensorboard import SummaryWriter writer = SummaryWriter('logs') for i, batch in enumerate(train_loader): loss = train(batch) accuracy = evaluate(model, test_loader) writer.add_scalars('training_metrics', {'loss': loss, 'accuracy': accuracy}, i)In this example, we use `add_scalars` to log both the loss and accuracy values during training. The `training_metrics` name is used to group all of the metrics together, and the `{ 'loss': loss, 'accuracy': accuracy }` dictionary specifies the name-value pairs for each scalar. This results in separate plots for the loss and accuracy metrics over time in TensorBoard. Overall, `torch.utils.tensorboard` is a powerful and flexible library for visualizing and analyzing training metrics and network graphs.