Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

PyTorch · Module

model.state_dict

Returns a dictionary-like mapping of a module’s learnable parameters and registered buffers. Use it to save, inspect, modify, or restore model state.

How it works

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.

Syntax

Module.state_dict(*args, destination=None, prefix='', keep_vars=False)

Returns

A Python dictionary-like state mapping containing the module’s parameters and registered buffers.

Key points

  • The mapping uses module or layer names as keys and stores the corresponding parameter tensors as values.
  • Its dictionary representation makes checkpoints straightforward to serialize and manipulate.
  • The documented save pattern serializes the result of model.state_dict() with torch.save().

Common mistakes

  • The result includes learnable layers and registered buffers, but not arbitrary attributes that were assigned to the module.

Example: Save and restore module weights

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.

When to use it

Checkpoint a model

Persist trained weights for later inference or continued training.

Inspect module state

Inspect parameter names and tensor shapes during debugging.

Restore model parameters

Transfer learned weights into a newly constructed compatible model.

Interview questions

QWhat does Module.state_dict() return?

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.

QWhich values appear in a module’s state_dict()?

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.

QWhy is state_dict() commonly used for saving models?

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_dict
  • torch.save
  • torch.load
  • torch.nn.Module.parameters

Source: What is a state_dict in PyTorch. Checked 2026-10-16.