Bird
Raised Fist0
PyTorchml~20 mins

Learning rate differential in PyTorch - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Learning Rate Differential Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why use different learning rates for different layers?

In training deep neural networks, why might we assign different learning rates to different layers?

ABecause different learning rates help the optimizer skip some layers during training.
BBecause early layers often learn general features and require smaller learning rates, while later layers learn task-specific features and can use larger learning rates.
CBecause using the same learning rate for all layers always causes the model to diverge.
DBecause some layers have more parameters and need slower updates to avoid overfitting.
Attempts:
2 left
💡 Hint

Think about how early layers and later layers in a neural network behave differently during training.

Predict Output
intermediate
2:00remaining
Output of learning rate differential setup in PyTorch

What will be the learning rate of the parameters in model.layer1 and model.layer2 after this code runs?

PyTorch
import torch
import torch.nn as nn

class SimpleModel(nn.Module):
    def __init__(self):
        super().__init__()
        self.layer1 = nn.Linear(10, 5)
        self.layer2 = nn.Linear(5, 2)

model = SimpleModel()
optimizer = torch.optim.SGD([
    {'params': model.layer1.parameters(), 'lr': 0.001},
    {'params': model.layer2.parameters(), 'lr': 0.01}
], momentum=0.9)

lrs = [group['lr'] for group in optimizer.param_groups]
print(lrs)
A[0.01, 0.001]
B[0.001]
C[0.01]
D[0.001, 0.01]
Attempts:
2 left
💡 Hint

Look at how the optimizer parameter groups are defined with different learning rates.

Hyperparameter
advanced
2:00remaining
Choosing learning rates for differential training

You want to fine-tune a pretrained model by freezing early layers and training only the last few layers. Which learning rate setup is best?

ASet zero learning rate for frozen layers and a small learning rate for trainable layers.
BSet a high learning rate for all layers to speed up training.
CSet a small learning rate for frozen layers and a high learning rate for trainable layers.
DSet the same moderate learning rate for all layers regardless of freezing.
Attempts:
2 left
💡 Hint

Frozen layers should not update during training.

Metrics
advanced
2:00remaining
Effect of learning rate differential on training loss

During training with differential learning rates, you notice the loss decreases quickly at first but then plateaus. What is a likely cause?

AThe learning rate for later layers is too low, slowing learning.
BThe learning rates are perfectly balanced; plateau is normal.
CThe learning rate for early layers is too high, causing instability.
DThe optimizer momentum is set to zero, causing slow convergence.
Attempts:
2 left
💡 Hint

Consider which layers learn task-specific features and how their learning rate affects training speed.

🔧 Debug
expert
2:00remaining
Why does this differential learning rate code cause an error?

What error does this PyTorch code raise when trying to set different learning rates?

PyTorch
import torch
import torch.nn as nn

model = nn.Sequential(
    nn.Linear(10, 5),
    nn.ReLU(),
    nn.Linear(5, 2)
)

optimizer = torch.optim.Adam([
    {'params': model[0].parameters(), 'lr': 0.001},
    {'params': model[1].parameters(), 'lr': 0.01}
])
ANo error, code runs successfully
BRuntimeError: optimizer parameter groups must have 'params' key
CAttributeError: 'ReLU' object has no attribute 'parameters'
DTypeError: optimizer got an unexpected keyword argument 'lr'
Attempts:
2 left
💡 Hint

Check which layers have parameters and which do not.

Practice

(1/5)
1. What does learning rate differential mean in PyTorch training?
easy
A. Changing the learning rate randomly during training
B. Setting different learning rates for different parts of a model
C. Using the same learning rate for the entire model
D. Freezing all model layers during training

Solution

  1. Step 1: Understand learning rate concept

    The learning rate controls how fast a model updates its knowledge during training.
  2. Step 2: Define learning rate differential

    Learning rate differential means assigning different learning rates to different parts of the model to control their update speed.
  3. Final Answer:

    Setting different learning rates for different parts of a model -> Option B
  4. Quick Check:

    Learning rate differential = Different rates per model part [OK]
Hint: Different parts can learn at different speeds [OK]
Common Mistakes:
  • Thinking learning rate is always the same for all layers
  • Confusing learning rate differential with random rate changes
  • Believing freezing layers means changing learning rate
2. Which PyTorch code snippet correctly sets different learning rates for two parameter groups?
easy
A. optimizer = torch.optim.SGD(model.parameters(), lr=0.01, lr2=0.001)
B. optimizer = torch.optim.SGD(model.parameters(), lr=[0.01, 0.001])
C. optimizer = torch.optim.SGD([{'params': model.layer1.parameters(), 'lr': 0.01}, {'params': model.layer2.parameters(), 'lr': 0.001}], momentum=0.9)
D. optimizer = torch.optim.SGD([model.layer1, model.layer2], lr=0.01)

Solution

  1. Step 1: Check PyTorch optimizer syntax for param groups

    PyTorch allows passing a list of dicts with 'params' and 'lr' keys to set different learning rates.
  2. Step 2: Identify correct syntax

    optimizer = torch.optim.SGD([{'params': model.layer1.parameters(), 'lr': 0.01}, {'params': model.layer2.parameters(), 'lr': 0.001}], momentum=0.9) correctly uses a list of dicts with separate learning rates for layer1 and layer2 parameters.
  3. Final Answer:

    optimizer = torch.optim.SGD([{'params': model.layer1.parameters(), 'lr': 0.01}, {'params': model.layer2.parameters(), 'lr': 0.001}], momentum=0.9) -> Option C
  4. Quick Check:

    Param groups with separate 'lr' keys = Correct syntax [OK]
Hint: Use list of dicts with 'params' and 'lr' keys [OK]
Common Mistakes:
  • Passing lr as a list directly to optimizer
  • Using unknown keyword like lr2
  • Passing layers instead of parameters
3. Given this code, what is the learning rate for model.layer2 during training?
optimizer = torch.optim.Adam([
  {'params': model.layer1.parameters(), 'lr': 0.005},
  {'params': model.layer2.parameters(), 'lr': 0.0005}
])
medium
A. 0.0005
B. 0.05
C. 0.0055
D. 0.005

Solution

  1. Step 1: Identify learning rates assigned to each layer

    Layer1 has lr=0.005, Layer2 has lr=0.0005 as per the optimizer param groups.
  2. Step 2: Find learning rate for model.layer2

    From the second dict, model.layer2.parameters() uses lr=0.0005.
  3. Final Answer:

    0.0005 -> Option A
  4. Quick Check:

    Layer2 lr = 0.0005 from param groups [OK]
Hint: Check param group with layer2 parameters [OK]
Common Mistakes:
  • Adding learning rates instead of selecting correct one
  • Confusing layer1 lr with layer2 lr
  • Assuming default lr overrides param groups
4. Identify the error in this PyTorch optimizer setup for learning rate differential:
optimizer = torch.optim.SGD([
  {'params': model.layer1.parameters(), 'lr': 0.01},
  {'params': model.layer2.parameters()}
], lr=0.001)
medium
A. Missing learning rate for second param group causes error
B. Using lr=0.001 outside param groups is invalid
C. Parameters should be passed as model.layer1, not model.layer1.parameters()
D. SGD optimizer does not support param groups

Solution

  1. Step 1: Review param groups and learning rates

    First param group has lr=0.01, second param group has no lr specified.
  2. Step 2: Understand default lr behavior

    When param groups are used, each group should have lr or optimizer's lr applies. Here, lr=0.001 is passed but second group lacks explicit lr, causing confusion.
  3. Final Answer:

    Missing learning rate for second param group causes error -> Option A
  4. Quick Check:

    All param groups need lr or default applies [OK]
Hint: Each param group must have lr or rely on optimizer lr [OK]
Common Mistakes:
  • Assuming optimizer lr applies to all param groups automatically
  • Passing parameters instead of parameter iterators
  • Believing SGD can't use param groups
5. You want to fine-tune a pretrained model by training only the last layer fast and freezing the rest. Which learning rate setup is best?
hard
A. Set same lr=0.01 for all layers
B. Freeze last layer and train others with lr=0.01
C. Set lr=0.01 for all layers except last layer with lr=0
D. Set lr=0 for all layers except last layer with lr=0.01

Solution

  1. Step 1: Understand freezing and learning rate

    Freezing means no updates, which can be done by setting lr=0 or disabling gradients.
  2. Step 2: Apply learning rate differential for fine-tuning

    Set lr=0 for frozen layers to prevent updates, and higher lr for last layer to train it fast.
  3. Final Answer:

    Set lr=0 for all layers except last layer with lr=0.01 -> Option D
  4. Quick Check:

    Freeze layers = lr 0, train last layer fast [OK]
Hint: Freeze layers by lr=0, train last layer with higher lr [OK]
Common Mistakes:
  • Using same learning rate for all layers when freezing
  • Freezing last layer instead of others
  • Not setting lr=0 for frozen layers