Bird
Raised Fist0
PyTorchml~10 mins

Model parameters inspection in PyTorch - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to print the names of all parameters in a PyTorch model.

PyTorch
for name, param in model.[1]():
    print(name)
Drag options to blanks, or click blank then click option'
Anamed_parameters
Bparameters
Cstate_dict
Dchildren
Attempts:
3 left
💡 Hint
Common Mistakes
Using parameters() which returns only parameter values without names.
Using state_dict() which returns a dictionary, not an iterator.
2fill in blank
medium

Complete the code to count how many parameters in the model require gradients.

PyTorch
count = sum(p.requires_grad for p in model.[1]())
print(count)
Drag options to blanks, or click blank then click option'
Anamed_parameters
Bparameters
Cchildren
Dmodules
Attempts:
3 left
💡 Hint
Common Mistakes
Using named_parameters() when only parameters are needed.
Using children() which returns submodules, not parameters.
3fill in blank
hard

Fix the error in the code to print the shape of each parameter tensor.

PyTorch
for name, param in model.named_parameters():
    print(name, param.[1])
Drag options to blanks, or click blank then click option'
Ashape
Bdim
Csize
Dlength
Attempts:
3 left
💡 Hint
Common Mistakes
Using size without parentheses, which prints the method object instead of the shape.
Using dim which returns the number of dimensions, not the shape.
Using length which is not a tensor property.
4fill in blank
hard

Fill the blank to create a dictionary of parameter names and their flattened sizes.

PyTorch
param_sizes = {name: param.[1]() for name, param in model.named_parameters()}
Drag options to blanks, or click blank then click option'
Asize
Bdim
Cshape
Dnumel
Attempts:
3 left
💡 Hint
Common Mistakes
Using size() or shape which return the shape tuple, not the flattened count.
Using dim which returns number of dimensions, not size.
5fill in blank
hard

Fill all three blanks to filter parameters that require gradients and create a dictionary of their names and shapes.

PyTorch
grad_params = {name: param.[1]() for name, param in model.named_parameters() if param.[2] and param.[3]
Drag options to blanks, or click blank then click option'
Asize
Brequires_grad
Cis_leaf
Dgrad
Attempts:
3 left
💡 Hint
Common Mistakes
Using grad which is None before backward pass.
Not filtering by is_leaf which may include intermediate tensors.

Practice

(1/5)
1. What does the named_parameters() method in a PyTorch model return?
easy
A. The optimizer's learning rate
B. An iterator over tuples of parameter names and their tensors
C. The model's training loss values
D. A list of all model layers

Solution

  1. Step 1: Understand the purpose of named_parameters()

    This method is designed to give access to each parameter's name and its tensor, which includes weights and biases.
  2. Step 2: Identify what it returns

    It returns an iterator over tuples, where each tuple contains the parameter's name and the tensor itself.
  3. Final Answer:

    An iterator over tuples of parameter names and their tensors -> Option B
  4. Quick Check:

    named_parameters() = iterator of (name, tensor) [OK]
Hint: Remember: named_parameters() lists names and tensors [OK]
Common Mistakes:
  • Confusing parameters with layers
  • Thinking it returns loss or optimizer info
  • Assuming it returns just tensors without names
2. Which of the following code snippets correctly prints the names and shapes of all parameters in a PyTorch model model?
easy
A. for name, param in model.named_parameters(): print(name, param.shape)
B. for param in model.parameters(): print(param.name, param.shape)
C. print(model.parameters().names())
D. model.print_parameters()

Solution

  1. Step 1: Check correct method usage

    The method named_parameters() returns (name, parameter) pairs, so unpacking into two variables is correct.
  2. Step 2: Verify printing name and shape

    Accessing param.shape gives the tensor shape. for name, param in model.named_parameters(): print(name, param.shape) correctly prints both.
  3. Final Answer:

    for name, param in model.named_parameters(): print(name, param.shape) -> Option A
  4. Quick Check:

    Use named_parameters() with unpacking [OK]
Hint: Use named_parameters() to get names and tensors [OK]
Common Mistakes:
  • Trying to access .name on param tensor (no such attribute)
  • Using parameters() without names
  • Calling non-existent methods like print_parameters()
3. Given the following PyTorch model code, what will be the output of the print statement?
import torch.nn as nn

class SimpleNet(nn.Module):
    def __init__(self):
        super().__init__()
        self.fc1 = nn.Linear(4, 3)
        self.fc2 = nn.Linear(3, 2)

model = SimpleNet()
params = list(model.named_parameters())
print(len(params))
medium
A. 2
B. 6
C. 4
D. 3

Solution

  1. Step 1: Count parameters in each layer

    Each nn.Linear has weights and biases, so 2 parameters per layer.
  2. Step 2: Calculate total parameters

    Two layers x 2 parameters each = 4 parameters total.
  3. Final Answer:

    4 -> Option C
  4. Quick Check:

    2 layers x 2 params = 4 [OK]
Hint: Each Linear layer has weight and bias parameters [OK]
Common Mistakes:
  • Counting only layers, not parameters
  • Assuming one parameter per layer
  • Confusing number of parameters with tensor sizes
4. You wrote this code to print model parameter names and shapes but get an error:
for name, param in model.parameters():
    print(name, param.shape)
What is the error and how to fix it?
medium
A. The loop syntax is wrong; use for param, name in model.parameters()
B. param.shape is invalid; use param.size() instead
C. You must call model.parameters().items() to get name and param
D. model.parameters() returns tensors only; use model.named_parameters() to get names

Solution

  1. Step 1: Understand what model.parameters() returns

    This method returns only parameter tensors, no names.
  2. Step 2: Correct method to get names and parameters

    Use model.named_parameters() which returns (name, param) pairs.
  3. Final Answer:

    model.parameters() returns tensors only; use model.named_parameters() to get names -> Option D
  4. Quick Check:

    parameters() = tensors only, named_parameters() = (name, tensor) [OK]
Hint: Use named_parameters() to get names with tensors [OK]
Common Mistakes:
  • Trying to unpack parameters() into name and param
  • Using .items() on parameters() which is not a dict
  • Swapping variable order in for loop
5. You want to calculate the total number of trainable parameters in a PyTorch model model. Which code snippet correctly computes this?
hard
A. sum(p.numel() for p in model.parameters() if p.requires_grad)
B. len(model.named_parameters())
C. sum(p.shape for p in model.parameters())
D. model.count_parameters()

Solution

  1. Step 1: Understand what counts as trainable parameters

    Only parameters with requires_grad=True are trainable.
  2. Step 2: Use numel() to count elements in each tensor

    Summing p.numel() for all trainable parameters gives total count.
  3. Final Answer:

    sum(p.numel() for p in model.parameters() if p.requires_grad) -> Option A
  4. Quick Check:

    Sum numel() of trainable params = total count [OK]
Hint: Sum numel() of parameters with requires_grad True [OK]
Common Mistakes:
  • Using len() which counts parameters, not elements
  • Trying to sum shapes directly (shapes are tuples)
  • Assuming model has count_parameters() method
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard