Checkpoint a model
Persist trained weights for later inference or continued training.
PyTorch · Module
Returns a dictionary-like mapping of a module’s learnable parameters and registered buffers. Use it to save, inspect, modify, or restore model state.
Calling state_dict() produces a dictionary-like snapshot of a module’s learnable parameters and registered buffers, using names such as weight and bias as keys. The result can be serialized for checkpointing and later supplied to load_state_dict() on a compatible model instance.
Module.state_dict(*args, destination=None, prefix='', keep_vars=False)A Python dictionary-like state mapping containing the module’s parameters and registered buffers.
import torch
from torch import nn
model = nn.Linear(2, 1)
state = model.state_dict()
print(list(state.keys()))
path = "model_weights.pth"
torch.save(state, path)
restored = nn.Linear(2, 1)
restored.load_state_dict(torch.load(path, weights_only=True))Creates a linear model, obtains its state dictionary, saves it, and loads it into a matching model.
Persist trained weights for later inference or continued training.
Inspect parameter names and tensor shapes during debugging.
Transfer learned weights into a newly constructed compatible model.
state_dict() returns a Python dictionary-like mapping from parameter or buffer names to tensors. It is the standard object used to persist a module’s learned state separately from its code.
Follow-up: How would you restore it? Instantiate a compatible module, then call load_state_dict() with the deserialized mapping.
It contains entries for learnable parameters and registered buffers, such as BatchNorm running statistics. Ordinary attributes that are not registered as parameters or buffers are not included.
Follow-up: Why are buffers useful here? They preserve non-learnable module state needed for correct inference, such as running statistics.
A state dictionary is a Python dictionary object, so it can be saved, changed, and restored. The usual checkpoint pattern is torch.save(model.state_dict(), path), followed by loading it into a matching model.
Follow-up: Why save the state dictionary rather than the entire model? It separates weights from the model’s class definition and is the documented modular checkpointing pattern.
torch.nn.Module.load_state_dicttorch.savetorch.loadtorch.nn.Module.parameters