0
0
PyTorchml~10 mins

Why pre-trained models accelerate development in PyTorch - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to load a pre-trained model in PyTorch.

PyTorch
import torch
import torchvision.models as models

model = models.resnet18(pretrained=[1])
Drag options to blanks, or click blank then click option'
ATrue
BFalse
CNone
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using False will create a model with random weights.
2fill in blank
medium

Complete the code to freeze all layers of the pre-trained model.

PyTorch
for param in model.[1]():
    param.requires_grad = False
Drag options to blanks, or click blank then click option'
Alayers
Bchildren
Cmodules
Dparameters
Attempts:
3 left
💡 Hint
Common Mistakes
Using children() returns submodules, not parameters.
3fill in blank
hard

Fix the error in replacing the final layer for transfer learning.

PyTorch
import torch.nn as nn
num_classes = 10
model.fc = nn.[1](num_classes)
Drag options to blanks, or click blank then click option'
AConv2d
BLinear
CReLU
DDropout
Attempts:
3 left
💡 Hint
Common Mistakes
Using Conv2d or activation layers instead of Linear.
4fill in blank
hard

Fill both blanks to set the model to evaluation mode and disable gradient calculation.

PyTorch
model.[1]()
with torch.[2]():
    outputs = model(inputs)
Drag options to blanks, or click blank then click option'
Aeval
Bno_grad
Ctrain
Denable_grad
Attempts:
3 left
💡 Hint
Common Mistakes
Using train() instead of eval(), or enable_grad instead of no_grad.
5fill in blank
hard

Fill all three blanks to create a dictionary of layer names and their requires_grad status.

PyTorch
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'
Arequires_grad
Bnamed_parameters
Cendswith
Dstartswith
Attempts:
3 left
💡 Hint
Common Mistakes
Using parameters() instead of named_parameters(), or endswith instead of startswith.