QuiddityML

How to save and resume training in PyTorch (checkpoints)

A checkpoint is a file that stores a training run so it can be continued later. This post explains what to put in one, how to save and load it in PyTorch, why saving only the model weights is not enough to resume, and why most runs keep a latest and a best checkpoint.

ML FoundationsBy · 1 October 2026 · 9 min read

A checkpoint is a file written during training that holds what is needed to continue the run later from the same point. Training can take hours or days, and a crash, a closed laptop or a job time limit can end it early. Without a checkpoint the run starts again from the first step. With one, it picks up at the last save.

What training state is

Training changes more than the model's weights. A typical PyTorch run has four pieces of state that move as training goes on.

  • The model's weights. model.state_dict() returns a dictionary that maps each layer's name to its tensor of weights or biases.
  • The optimizer's state. The optimizer is the object that updates the weights from the gradients. Plain SGD stores nothing between steps, but most optimizers do. Adam keeps two running averages for each weight: one of its recent gradients, usually written $m$, and one of its recent squared gradients, usually written $v$. It uses them to set the size of each update. optimizer.state_dict() returns them.
  • The scheduler's state. A learning rate schedule changes the learning rate, the number that scales each update, as training goes on. scheduler.state_dict() records how far along the schedule the run is.
  • Bookkeeping. The epoch number (an epoch is one pass over the training data) and the lowest validation loss so far, where the validation loss is the loss measured on held-out examples the model does not train on.

A complete checkpoint stores all four in one dictionary and writes it with torch.save.

A diagram titled "What a complete checkpoint holds". Four boxes, model.state_dict() with weights and biases, optimizer.state_dict() with Adam moments m and v, scheduler.state_dict() with the position in the schedule, and epoch plus best_val_loss, all point into one file called checkpoint_latest.pt. Below, a check mark says all four saved and the resume continues smoothly, and a cross says weights only and the optimizer restarts cold.

Saving and resuming, in one script

The script below trains a small network on made-up data three ways. Run A trains for 20 epochs with no interruption. Run B trains for 10 epochs, saves a checkpoint, throws its objects away, builds new ones, loads the checkpoint, and trains 10 more. Run C loads the same file but restores only the model weights. The learning rate starts at 0.01 and a StepLR schedule halves it every 10 epochs.

import os
import tempfile
import torch
import torch.nn as nn

torch.manual_seed(0)
X = torch.randn(600, 10)
y = torch.sin(X[:, :1]) + 0.5 * X[:, 1:2] * X[:, 2:3] + 0.3 * torch.randn(600, 1)
X_train, y_train, X_val, y_val = X[:500], y[:500], X[500:], y[500:]
loss_fn = nn.MSELoss()

def build(seed):
    torch.manual_seed(seed)
    model = nn.Sequential(nn.Linear(10, 64), nn.ReLU(), nn.Linear(64, 1))
    optimizer = torch.optim.Adam(model.parameters(), lr=0.01)
    scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=10, gamma=0.5)
    return model, optimizer, scheduler

def run_epoch(model, optimizer, scheduler):
    model.train()
    for i in range(0, 500, 50):
        loss = loss_fn(model(X_train[i:i + 50]), y_train[i:i + 50])
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
    scheduler.step()
    model.eval()
    with torch.no_grad():
        return loss_fn(model(X_val), y_val).item()

path = os.path.join(tempfile.mkdtemp(), "checkpoint_latest.pt")

# Run A: 20 epochs with no interruption
model, optimizer, scheduler = build(seed=1)
for epoch in range(20):
    val_a = run_epoch(model, optimizer, scheduler)
    if epoch == 10:
        val_a_11 = val_a

# Run B: 10 epochs, save, stop
model, optimizer, scheduler = build(seed=1)
for epoch in range(10):
    val_loss = run_epoch(model, optimizer, scheduler)
torch.save({
    "epoch": epoch,
    "model_state_dict": model.state_dict(),
    "optimizer_state_dict": optimizer.state_dict(),
    "scheduler_state_dict": scheduler.state_dict(),
    "val_loss": val_loss,
}, path)
print(f"saved after epoch {epoch + 1}, val loss {val_loss:.4f}, lr {scheduler.get_last_lr()[0]}")

# Run B, resumed: new objects with a different seed, then load everything
model, optimizer, scheduler = build(seed=999)
checkpoint = torch.load(path, weights_only=True)
model.load_state_dict(checkpoint["model_state_dict"])
optimizer.load_state_dict(checkpoint["optimizer_state_dict"])
scheduler.load_state_dict(checkpoint["scheduler_state_dict"])
start_epoch = checkpoint["epoch"] + 1
print(f"resuming at epoch {start_epoch + 1}, lr {scheduler.get_last_lr()[0]}")
for epoch in range(start_epoch, 20):
    val_b = run_epoch(model, optimizer, scheduler)
    if epoch == 10:
        val_b_11 = val_b

# Run C, resumed with the weights only: optimizer and scheduler start fresh
model, optimizer, scheduler = build(seed=999)
model.load_state_dict(checkpoint["model_state_dict"])
print(f"weights-only resume, lr {scheduler.get_last_lr()[0]}")
for epoch in range(start_epoch, 20):
    val_c = run_epoch(model, optimizer, scheduler)
    if epoch == 10:
        val_c_11 = val_c

print(f"epoch 11 val loss  uninterrupted {val_a_11:.4f} | full resume {val_b_11:.4f} | weights only {val_c_11:.4f}")
print(f"epoch 20 val loss  uninterrupted {val_a:.4f} | full resume {val_b:.4f} | weights only {val_c:.4f}")
print("full resume matches uninterrupted run:", val_a == val_b)
saved after epoch 10, val loss 0.1553, lr 0.005
resuming at epoch 11, lr 0.005
weights-only resume, lr 0.01
epoch 11 val loss  uninterrupted 0.1529 | full resume 0.1529 | weights only 0.1799
epoch 20 val loss  uninterrupted 0.1466 | full resume 0.1466 | weights only 0.1538
full resume matches uninterrupted run: True

The fully restored run ends on the same validation loss as the run that was not interrupted, 0.1466, even though its model, optimizer and scheduler were rebuilt from a different random seed before loading. The file carried everything the next step depended on.

Three details in the loading code are worth copying. The model, optimizer and scheduler are built first and then filled in with load_state_dict, because a state dict holds values and has no code in it. start_epoch is the saved epoch plus one, since the saved epoch already finished. weights_only=True tells torch.load to read tensors and plain Python values and refuse to run other code stored in the file, which is the safer setting for a file from another machine or person.

What goes wrong with weights only

Run C starts from the same weights as run B, and its first epoch back is worse: 0.1799 against 0.1529. Two things were lost.

The first is Adam's running averages. A fresh Adam starts both averages at zero, so for its first steps the update sizes are not scaled by the gradient history the run had built up, and the weights move further than they would have.

The second is the position in the schedule. The saved run had already halved its learning rate to 0.005. The fresh scheduler starts again at 0.01, which the output shows on the "weights-only resume" line.

Run C recovers part of the way and still ends at 0.1538 instead of 0.1466. The gap is small on this toy problem. It tends to be larger on long runs that use warmup or a decayed learning rate, where restarting the schedule sends a nearly trained model back to the largest learning rate.

A sketch of training loss against training step, titled "Resuming without the optimizer state". The loss falls from 0.9 to 0.5 by step 2000, where a dashed line marks the resume. A red curve labeled "weights only" jumps up before coming back down, and a teal curve labeled "full restore" keeps falling smoothly. A box at the bottom says load model, optimizer, scheduler.

The picture is a sketch of the shape, and the size of the jump depends on the optimizer, the learning rate and the model.

Keeping a latest and a best checkpoint

A run usually keeps two checkpoint files, because there are two different reasons to save.

  • Latest checkpoint: written at the end of each epoch, or every fixed number of steps, over the previous one. It exists for crash recovery, so a crash costs at most the work since the last save.
  • Best checkpoint: written only when the validation loss reaches a new low. Later epochs can make the model worse on held-out data, so the weights at the end of training are often not the ones to keep. This file is the model you evaluate and ship.
import os
import tempfile
import torch
import torch.nn as nn

torch.manual_seed(0)
X = torch.randn(260, 10)
y = torch.sin(X[:, :1]) + 0.5 * X[:, 1:2] * X[:, 2:3] + 0.3 * torch.randn(260, 1)
X_train, y_train, X_val, y_val = X[:200], y[:200], X[200:], y[200:]

model = nn.Sequential(nn.Linear(10, 64), nn.ReLU(), nn.Linear(64, 1))
optimizer = torch.optim.Adam(model.parameters(), lr=0.01)
loss_fn = nn.MSELoss()
folder = tempfile.mkdtemp()

def save_checkpoint(state, name):
    tmp_path = os.path.join(folder, name + ".tmp")
    torch.save(state, tmp_path)
    os.replace(tmp_path, os.path.join(folder, name))  # swap in only after the write finished

best_val_loss = float("inf")
for epoch in range(12):
    model.train()
    for i in range(0, 200, 20):
        loss = loss_fn(model(X_train[i:i + 20]), y_train[i:i + 20])
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

    model.eval()
    with torch.no_grad():
        val_loss = loss_fn(model(X_val), y_val).item()

    improved = val_loss < best_val_loss
    best_val_loss = min(best_val_loss, val_loss)
    state = {
        "epoch": epoch,
        "model_state_dict": model.state_dict(),
        "optimizer_state_dict": optimizer.state_dict(),
        "best_val_loss": best_val_loss,
        "val_loss": val_loss,
    }
    save_checkpoint(state, "checkpoint_latest.pt")      # every epoch
    if improved:
        save_checkpoint(state, "checkpoint_best.pt")    # only on a new best
    print(f"epoch {epoch + 1:2d}  val loss {val_loss:.4f}  latest saved" + ("  best saved" if improved else ""))

best = torch.load(os.path.join(folder, "checkpoint_best.pt"), weights_only=True)
latest = torch.load(os.path.join(folder, "checkpoint_latest.pt"), weights_only=True)
print(f"best checkpoint:   epoch {best['epoch'] + 1}, val loss {best['val_loss']:.4f}")
print(f"latest checkpoint: epoch {latest['epoch'] + 1}, val loss {latest['val_loss']:.4f}")
epoch  1  val loss 0.4969  latest saved  best saved
epoch  2  val loss 0.2874  latest saved  best saved
epoch  3  val loss 0.2204  latest saved  best saved
epoch  4  val loss 0.2019  latest saved  best saved
epoch  5  val loss 0.1839  latest saved  best saved
epoch  6  val loss 0.1543  latest saved  best saved
epoch  7  val loss 0.1437  latest saved  best saved
epoch  8  val loss 0.1454  latest saved
epoch  9  val loss 0.1451  latest saved
epoch 10  val loss 0.1479  latest saved
epoch 11  val loss 0.1470  latest saved
epoch 12  val loss 0.1462  latest saved
best checkpoint:   epoch 7, val loss 0.1437
latest checkpoint: epoch 12, val loss 0.1462

The validation loss stops improving after epoch 7, so the best file stays at epoch 7 while the latest file moves on to epoch 12. best_val_loss is stored in the checkpoint too. A resumed run that forgets it starts from infinity and overwrites the best file on its first epoch back, even when that epoch is worse.

A diagram titled "Keep two checkpoints". A validation loss line over eight epochs reads 0.90, 0.74, 0.62, 0.55, 0.61, 0.48, 0.41, 0.47. A row labeled latest checkpoint has a save icon at all eight epochs, with the note saved every epoch, for crash recovery, lose at most one epoch. A row labeled best checkpoint has a save icon at epochs 1, 2, 3, 4, 6 and 7 and none at epochs 5 and 8, where the loss went up, with the note saved only when validation improves, this is the model you deploy. A code box shows if val_loss < best_val_loss: torch.save(checkpoint, checkpoint_best.pt).

The picture shows the same rule on a run where the validation loss goes back up twice: epochs 5 and 8 write the latest file and leave the best file alone.

save_checkpoint writes to a temporary file and then renames it with os.replace. A crash in the middle of torch.save would otherwise leave a half-written latest checkpoint, which is the one file crash recovery depends on. When a resume fails with an error about missing or unexpected keys, compare the model you built against the one that was saved, since the layer names and shapes have to match.

Loading a checkpoint for evaluation

Using the best checkpoint to make predictions needs the model weights only. Build the model, call load_state_dict with checkpoint["model_state_dict"], then call model.eval() so layers such as dropout switch to their evaluation behaviour. If the file was saved on a GPU and is being loaded on a machine without one, pass map_location="cpu" to torch.load.

Common mistakes

  • Saving only model.state_dict() and then resuming training. The weights come back, and the optimizer's running averages and the learning rate schedule start over.
  • Saving the whole model object with torch.save(model, path). That stores a reference to the class and the file it was defined in, so the file can stop loading once the code is renamed or moved. A state dict holds tensors and loads into any model with matching layers.
  • Resuming at the saved epoch instead of the next one. The saved epoch is repeated, and a schedule driven by the epoch number drifts by one.
  • Overwriting the best checkpoint with a later, worse epoch. Guard the save with the val_loss < best_val_loss comparison and restore best_val_loss when resuming.
  • Saving once at the end. A crash at 90% of the run then loses 90% of the work.
  • Expecting a resumed run to match bit for bit when the data is shuffled. The script above matched because it reads batches in a fixed order. With a shuffling data loader, the random number generator has its own state, and a resumed run draws a different shuffle unless that state is saved as well.

Adam explains the two running averages that make the optimizer state worth saving. Learning rate schedules covers warmup, cosine and step decay, which are what a restarted scheduler repeats from the beginning. Early stopping, covered under regularization, ends training when the validation loss stops improving and returns the best checkpoint. The train, validation and test split explains why the best checkpoint is picked on validation data and scored once on test data.

QuiddityML teaches checkpointing as a concept in Unit 3 of the ML Foundation track, where the exercises include ordering the lines of save_checkpoint, spotting the bug in a resume that restarts at the saved epoch instead of the next one, and writing the save, load and training loop from scratch (quiddityml.com).