Python / PyTorch Fundamentals Interview Questions
What is the difference between model.parameters() and model.state_dict() in PyTorch?
Both expose a model's learnable values, but they serve different purposes. parameters() returns an iterator of nn.Parameter tensor objects (used by the optimizer); state_dict() returns an OrderedDict mapping layer names to tensors (used for saving/loading and inspection).
import torch
import torch.nn as nn
model = nn.Sequential(
nn.Linear(10, 20),
nn.ReLU(),
nn.Linear(20, 1),
)
# ── parameters(): iterator of Parameter tensors (no names)
for p in model.parameters():
print(p.shape, p.requires_grad)
# torch.Size([20, 10]) True
# torch.Size([20]) True
# torch.Size([1, 20]) True
# torch.Size([1]) True
# Used to construct optimizers
optimizer = torch.optim.Adam(model.parameters(), lr=1e-3)
# ── named_parameters(): iterator of (name, Parameter) tuples
for name, p in model.named_parameters():
print(name, p.shape)
# 0.weight torch.Size([20, 10])
# 0.bias torch.Size([20])
# 2.weight torch.Size([1, 20])
# 2.bias torch.Size([1])
# ── state_dict(): OrderedDict for save/load
sd = model.state_dict()
print(type(sd)) # <class 'collections.OrderedDict'>
print(sd.keys()) # dict_keys(['0.weight', '0.bias', '2.weight', '2.bias'])
# Saving and loading via state_dict (the recommended pattern)
torch.save(model.state_dict(), "model_weights.pt")
new_model = nn.Sequential(nn.Linear(10, 20), nn.ReLU(), nn.Linear(20, 1))
new_model.load_state_dict(torch.load("model_weights.pt"))
new_model.eval() # ALWAYS call after loading for inference
# Total parameter count
total_params = sum(p.numel() for p in model.parameters())
trainable = sum(p.numel() for p in model.parameters() if p.requires_grad)
print(f"Total: {total_params}, Trainable: {trainable}")
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
