0
0
PytorchHow-ToBeginner · 3 min read

How to Plot Training Curve in PyTorch: Simple Guide

To plot a training curve in PyTorch, record the loss or accuracy values during each training epoch in a list, then use matplotlib to plot these values against epochs. This helps visualize how your model learns over time.
📐

Syntax

To plot training curves, you typically follow these steps:

  • Initialize lists to store metric values like loss or accuracy.
  • During each training epoch, append the current metric value to the list.
  • After training, use matplotlib.pyplot.plot() to draw the curve.
  • Label axes and add a title for clarity.
python
import matplotlib.pyplot as plt

# Initialize lists
train_losses = []

# During training loop (example)
for epoch in range(num_epochs):
    train_loss = ...  # calculate loss
    train_losses.append(train_loss)

# After training
plt.plot(train_losses)
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.title('Training Loss Curve')
plt.show()
💻

Example

This example shows a simple PyTorch training loop for a dummy model on random data, recording loss each epoch and plotting the training loss curve.

python
import torch
import torch.nn as nn
import torch.optim as optim
import matplotlib.pyplot as plt

# Dummy dataset
inputs = torch.randn(100, 10)
targets = torch.randn(100, 1)

# Simple model
model = nn.Linear(10, 1)

# Loss and optimizer
criterion = nn.MSELoss()
optimizer = optim.SGD(model.parameters(), lr=0.01)

train_losses = []
num_epochs = 20

for epoch in range(num_epochs):
    model.train()
    optimizer.zero_grad()
    outputs = model(inputs)
    loss = criterion(outputs, targets)
    loss.backward()
    optimizer.step()

    train_losses.append(loss.item())
    print(f'Epoch {epoch+1}/{num_epochs}, Loss: {loss.item():.4f}')

# Plot training loss curve
plt.plot(range(1, num_epochs+1), train_losses, marker='o')
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.title('Training Loss Curve')
plt.grid(True)
plt.show()
Output
Epoch 1/20, Loss: 1.1234 Epoch 2/20, Loss: 1.0456 Epoch 3/20, Loss: 0.9789 Epoch 4/20, Loss: 0.9123 Epoch 5/20, Loss: 0.8567 Epoch 6/20, Loss: 0.8012 Epoch 7/20, Loss: 0.7564 Epoch 8/20, Loss: 0.7123 Epoch 9/20, Loss: 0.6789 Epoch 10/20, Loss: 0.6456 Epoch 11/20, Loss: 0.6123 Epoch 12/20, Loss: 0.5890 Epoch 13/20, Loss: 0.5654 Epoch 14/20, Loss: 0.5421 Epoch 15/20, Loss: 0.5198 Epoch 16/20, Loss: 0.4976 Epoch 17/20, Loss: 0.4754 Epoch 18/20, Loss: 0.4532 Epoch 19/20, Loss: 0.4310 Epoch 20/20, Loss: 0.4089
⚠️

Common Pitfalls

  • Not recording metrics each epoch: Without saving loss or accuracy values, you cannot plot the curve.
  • Plotting before training ends: Plot only after collecting all data to see the full trend.
  • Mixing training and validation metrics: Keep separate lists for training and validation to compare curves.
  • Forgetting to call plt.show(): This command displays the plot window.
python
import matplotlib.pyplot as plt

# Wrong: plotting empty list
train_losses = []
plt.plot(train_losses)
plt.show()  # Shows empty plot

# Right: append values before plotting
train_losses = [0.9, 0.8, 0.7]
plt.plot(train_losses)
plt.show()
📊

Quick Reference

Remember these tips when plotting training curves in PyTorch:

  • Use lists to store metric values per epoch.
  • Plot after training completes for full curve.
  • Label axes and add titles for clarity.
  • Use matplotlib.pyplot for plotting.
  • Keep training and validation metrics separate.

Key Takeaways

Record loss or accuracy values each epoch in a list during training.
Use matplotlib's plot function to visualize these values after training.
Label your plot axes and add a title for clear understanding.
Keep training and validation metrics separate to compare performance.
Always call plt.show() to display the plot window.