Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to load a pre-trained model from torchvision.
Computer Vision
import torchvision.models as models model = models.[1](pretrained=True)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'train' or 'fit' instead of a model name.
Trying to call 'load' which is not a model constructor.
✗ Incorrect
The function 'resnet18' loads the ResNet-18 pre-trained model from torchvision. 'train', 'load', and 'fit' are not model names.
2fill in blank
mediumComplete the code to freeze all layers of the pre-trained model to save training time.
Computer Vision
for param in model.[1](): param.requires_grad = False
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'children()' which returns submodules, not parameters.
Using 'layers()' which is not a valid method.
✗ Incorrect
The 'parameters()' method returns all parameters of the model. Setting requires_grad to False freezes them.
3fill in blank
hardFix the error in the code to replace the last layer for transfer learning.
Computer Vision
import torch.nn as nn model.fc = nn.[1](512, 10)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Conv2d which is for convolutional layers.
Using activation layers like ReLU or Dropout instead of Linear.
✗ Incorrect
The last layer of ResNet is a fully connected (Linear) layer. We replace it with a Linear layer with output size 10.
4fill in blank
hardFill both blanks to set the model to evaluation mode and disable gradient calculation.
Computer Vision
model.[1]() with torch.[2](): outputs = model(inputs)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using model.train() instead of model.eval().
Using torch.enable_grad() which enables gradients.
✗ Incorrect
Calling model.eval() sets the model to evaluation mode. Using torch.no_grad() disables gradient calculation to save time.
5fill in blank
hardFill all three blanks to create a dictionary of layer names and their requires_grad status.
Computer Vision
grad_status = {name: param.[1] for name, param in model.[2]() if name.[3]('fc') == False} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'parameters()' instead of 'named_parameters()' which lacks names.
Using 'endswith' instead of 'startswith' to filter layer names.
Checking 'requires_grad' incorrectly.
✗ Incorrect
We check requires_grad for each parameter from named_parameters(). We exclude layers starting with 'fc' to keep them trainable.