Hint: Output shape matches batch size and new class count [OK]
Common Mistakes:
Expecting original 1000 classes output
Confusing feature size with output size
Misreading input tensor shape as output
4. You tried replacing the classifier head of a pretrained model with model.fc = nn.Linear(1024, 10) but got a runtime error during training. What is the most likely cause?
medium
A. The model.fc attribute does not exist in pretrained models
B. The output size 10 is too large for the model
C. You forgot to call model.eval() before training
D. The input feature size 1024 does not match the model's actual output features
Solution
Step 1: Check input feature size for classifier
The input size to the new Linear layer must match the output features of the previous layer.
Step 2: Identify mismatch causing runtime error
If 1024 is incorrect, the model will raise size mismatch errors during forward pass.
Final Answer:
The input feature size 1024 does not match the model's actual output features -> Option D
Quick Check:
Input size mismatch causes runtime error [OK]
Hint: Match Linear input size to previous layer output features [OK]
Common Mistakes:
Assuming output size causes error
Confusing eval mode with training errors
Thinking model.fc is missing in pretrained models
5. You want to fine-tune a pretrained ResNet50 on a dataset with 15 classes. Which code snippet correctly replaces the classifier head and freezes all layers except the new head?
hard
A. model = models.resnet50(pretrained=True)
model.fc = nn.Linear(2048, 15)
for param in model.parameters():
param.requires_grad = False
B. model = models.resnet50(pretrained=True)
for param in model.parameters():
param.requires_grad = False
model.fc = nn.Linear(2048, 15)
C. model = models.resnet50(pretrained=True)
for param in model.fc.parameters():
param.requires_grad = False
model.fc = nn.Linear(2048, 15)
D. model = models.resnet50(pretrained=True)
model.fc = nn.Linear(512, 15)
for param in model.parameters():
param.requires_grad = True
Solution
Step 1: Freeze all existing model parameters
Set param.requires_grad = False for all parameters to prevent updates during training.
Step 2: Replace classifier head with correct input/output sizes
ResNet50's classifier input size is 2048; output size is 15 for new classes.
Step 3: Ensure new head parameters are trainable
By replacing model.fc after freezing, new layer parameters default to requires_grad=True.
Final Answer:
Freeze all params, then replace head with nn.Linear(2048, 15) -> Option B
Quick Check:
Freeze old layers, replace head with correct sizes [OK]
Hint: Freeze before replacing head to keep new layer trainable [OK]
Common Mistakes:
Freezing after replacing head disables new layer training