Complete the code to print the names of all parameters in a PyTorch model.
for name, param in model.[1](): print(name)
parameters() which returns only parameter values without names.state_dict() which returns a dictionary, not an iterator.The named_parameters() method returns both the parameter names and their values, which allows us to print the names.
Complete the code to count how many parameters in the model require gradients.
count = sum(p.requires_grad for p in model.[1]()) print(count)
named_parameters() when only parameters are needed.children() which returns submodules, not parameters.The parameters() method returns all parameters, and we check their requires_grad attribute to count those that require gradients.
Fix the error in the code to print the shape of each parameter tensor.
for name, param in model.named_parameters(): print(name, param.[1])
size without parentheses, which prints the method object instead of the shape.dim which returns the number of dimensions, not the shape.length which is not a tensor property.In PyTorch, shape is a property/attribute that returns the shape of a tensor as a torch.Size tuple. size is a method that requires parentheses (size()); without them, it refers to the method object itself.
Fill the blank to create a dictionary of parameter names and their flattened sizes.
param_sizes = {name: param.[1]() for name, param in model.named_parameters()}size() or shape which return the shape tuple, not the flattened count.dim which returns number of dimensions, not size.numel() returns the total number of elements (flattened size) in the tensor.
Fill all three blanks to filter parameters that require gradients and create a dictionary of their names and shapes.
grad_params = {name: param.[1]() for name, param in model.named_parameters() if param.[2] and param.[3]grad which is None before backward pass.is_leaf which may include intermediate tensors.We get the shape with size(). We filter parameters that require gradients (requires_grad) and are leaf tensors (is_leaf) to ensure they are trainable parameters.