Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to load a pretrained ResNet model for feature extraction.
PyTorch
import torch import torchvision.models as models model = models.resnet18(pretrained=[1])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Setting pretrained to False loads a model without learned features.
Using None or 0 causes errors or no pretrained weights.
✗ Incorrect
Setting pretrained=True loads the model with weights trained on ImageNet, which is essential for feature extraction.
2fill in blank
mediumComplete the code to freeze all parameters in the model to prevent training updates.
PyTorch
for param in model.parameters(): param.[1] = False
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'grad' or 'train' attributes which do not exist for parameters.
Calling detach() on parameters instead of setting requires_grad.
✗ Incorrect
Setting requires_grad to False disables gradient computation, freezing the parameters during training.
3fill in blank
hardFix the error in the code to replace the final fully connected layer with an identity layer for feature extraction.
PyTorch
import torch.nn as nn model.fc = [1]()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using nn.Linear requires parameters and changes output size.
Using nn.ReLU or nn.Conv2d changes the output and is not identity.
✗ Incorrect
Replacing the final layer with nn.Identity removes it, so the model outputs features before classification.
4fill in blank
hardFill both blanks to create a feature extractor that outputs features without gradients and sets the model to evaluation mode.
PyTorch
with torch.no_grad(): model.[1]() features = model(inputs)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using model.train() enables training mode, which is incorrect here.
Using requires_grad or zero_grad are not model methods.
✗ Incorrect
Calling model.eval() sets the model to evaluation mode, disabling dropout and batch norm updates during feature extraction.
5fill in blank
hardFill all three blanks to extract features from a batch of images using a pretrained model with frozen parameters and identity final layer.
PyTorch
import torch import torchvision.models as models import torch.nn as nn model = models.resnet50(pretrained=[1]) for param in model.parameters(): param.requires_grad = [2] model.fc = nn.[3]() model.eval() with torch.no_grad(): features = model(images)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Not freezing parameters causes training updates.
Not replacing final layer outputs class scores, not features.
Setting pretrained to False loads random weights.
✗ Incorrect
Load pretrained weights (True), freeze parameters (requires_grad=False), and replace final layer with nn.Identity for feature extraction.