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}")
More Related questions...