Skip to course content
Free PyTorch course

Advanced Deep Learning with PyTorch

Unit 05.05: Learning curves and instability signals

A learning curve tells you what to do next more reliably than any single metric. Three shapes cover most situations.

Reading the two curves together

Track training and validation loss every epoch and the diagnosis becomes visual. Both falling and close: keep going. Training falling while validation rises: overfitting, stop or regularise. Both flat and high: underfitting, or the learning rate is wrong.

Here are two rates, one stable and one not:

import torch
from torch import nn

torch.manual_seed(0)
X = torch.randn(300, 4)
y = (X[:, 0] * 1.5 - X[:, 1]).unsqueeze(1)
Xtr, ytr, Xva, yva = X[:220], y[:220], X[220:], y[220:]


def curve(lr, steps=120):
    torch.manual_seed(0)
    model = nn.Sequential(nn.Linear(4, 32), nn.ReLU(), nn.Linear(32, 1))
    opt = torch.optim.SGD(model.parameters(), lr=lr)
    train_hist, val_hist = [], []
    for _ in range(steps):
        opt.zero_grad()
        loss = nn.MSELoss()(model(Xtr), ytr)
        loss.backward()
        opt.step()
        with torch.no_grad():
            train_hist.append(loss.item())
            val_hist.append(nn.MSELoss()(model(Xva), yva).item())
    return train_hist, val_hist


for lr in (0.01, 0.9):
    train_hist, val_hist = curve(lr)
    diverged = any(v != v or v > 1e6 for v in val_hist)
    print(f"lr={lr}: start {train_hist[0]:.3f} -> end {train_hist[-1]:.3f}"
          f"   diverged: {diverged}")

train_hist, val_hist = curve(0.01)
print("\nfinal train/val gap:", round(val_hist[-1] - train_hist[-1], 4))
print("signals to read: loss going up, NaN, or a val curve that turns upward")

At 0.9 the loss diverges — the diverged flag catches values that have become NaN or absurdly large. At 0.01 it converges cleanly and the final train/val gap is small.

That gap is the number worth recording each run. It is a more useful summary than either loss alone.

The mistake this prevents

Plotting only training loss. It falls smoothly in nearly every scenario, including the ones where the model is getting worse at the job you actually care about.

Takeaway

Plot both curves from the first run. The shape of the pair tells you what to change.