0
0
PyTorchml~10 mins

Model parameters inspection in PyTorch - Interactive Code Practice

Choose your learning style9 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.