Course contentsShow
← Machine Learning and Deep Learning
Lesson 809 of 3,53819. PyTorch FundamentalsFree lesson

Accessing and Iterating Over Parameters

Use .parameters() and .named_parameters() to access model weights for optimization and inspection.

Accessing and Iterating Over Parameters

What You'll Learn

You'll learn how to inspect and access the weights and biases inside your PyTorch models using .parameters() and .named_parameters().

Why Access Parameters?

When you build neural networks with nn.Module, PyTorch automatically registers all learnable weights and biases as parameters. But sometimes you need to directly access these parameters—to pass them to an optimizer, inspect their values for debugging, or apply custom operations.

Think of your model as a filing cabinet full of numbered drawers (parameters). You need a way to open each drawer and see what's inside.

Two Key Methods

.parameters()

This method returns an iterator over all learnable parameters in your model (and any nested modules). It's perfect when you just need the parameter tensors themselves:

import torch.nn as nn

model = nn.Sequential(
    nn.Linear(10, 5),
    nn.Linear(5, 2)
)

# Count total parameters
total_params = sum(p.numel() for p in model.parameters())
print(f"Total parameters: {total_params}")

# Check which parameters require gradients
for param in model.parameters():
    print(param.shape, param.requires_grad)

.named_parameters()

This returns tuples of (name, parameter), giving you both the parameter tensor and a descriptive name. Essential for debugging or selective operations:

for name, param in model.named_parameters():
    print(f"{name}: shape {param.shape}")
    # Example output: "0.weight: shape torch.Size([5, 10])"

Common Use Cases

  • Optimizer setup: optimizer = torch.optim.SGD(model.parameters(), lr=0.01)
  • Gradient inspection: Check if gradients are flowing properly
  • Custom weight initialization: Apply specific initialization schemes
  • Debugging: Find layers with unexpected shapes or frozen weights

Key Takeaway: Use .parameters() to iterate over model weights for optimization, and .named_parameters() when you need descriptive names for inspection or selective operations.