0
0
PyTorchml~10 mins

Freezing layers in PyTorch - Interactive Code Practice

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

Complete the code to freeze all parameters in the model.

PyTorch
for param in model.parameters():
    param.[1] = False
Drag options to blanks, or click blank then click option'
Arequires_grad
Bgrad
Cfreeze
Ddetach
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'grad' instead of 'requires_grad'.
Trying to call a method like 'freeze()' which doesn't exist.
2fill in blank
medium

Complete the code to freeze only the first layer of the model.

PyTorch
for param in model.[1].parameters():
    param.requires_grad = False
Drag options to blanks, or click blank then click option'
Alayers
Bmodules
Cchildren
Dlayer1
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'children' or 'modules' which return iterables, not a single layer.
Using 'layers' which is not a standard attribute.
3fill in blank
hard

Fix the error in the code to freeze the model's convolutional layers only.

PyTorch
for name, param in model.named_parameters():
    if 'conv' in name:
        param.[1] = False
Drag options to blanks, or click blank then click option'
Agrad
Brequires_grad
Cdetach
Dfreeze
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'detach' which returns a new tensor but does not freeze parameters.
Trying to set 'grad' or 'freeze' which are invalid.
4fill in blank
hard

Fill both blanks to freeze all layers except the last fully connected layer.

PyTorch
for name, param in model.named_parameters():
    if not [1] in name:
        param.[2] = False
Drag options to blanks, or click blank then click option'
A'fc'
Brequires_grad
C'conv'
Dgrad
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'conv' instead of 'fc' to exclude the last layer.
Trying to set 'grad' instead of 'requires_grad'.
5fill in blank
hard

Fill all three blanks to freeze all layers except those containing 'bn' or 'fc' in their names.

PyTorch
for name, param in model.named_parameters():
    if not ([1] in name or [2] in name):
        param.[3] = False
Drag options to blanks, or click blank then click option'
A'bn'
B'fc'
Crequires_grad
D'conv'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'conv' instead of 'bn' or 'fc' to exclude layers.
Trying to set 'grad' or other invalid attributes.