0
0
PyTorchml~20 mins

Learning rate schedulers in PyTorch - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Learning Rate Scheduler Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
What is the main purpose of a learning rate scheduler?

In training neural networks, why do we use learning rate schedulers?

ATo randomly reset model weights at certain epochs to prevent overfitting.
BTo adjust the learning rate during training to improve convergence and avoid overshooting minima.
CTo increase the batch size dynamically during training for faster computation.
DTo change the activation function of the model layers during training.
Attempts:
2 left
💡 Hint

Think about how changing the step size affects learning progress.

Predict Output
intermediate
2:00remaining
Output of learning rate after 3 epochs with StepLR

Given the PyTorch code below, what is the learning rate printed after 3 epochs?

PyTorch
import torch
import torch.optim as optim

model_params = [torch.nn.Parameter(torch.randn(2, 2, requires_grad=True))]
optimizer = optim.SGD(model_params, lr=0.1)
scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=2, gamma=0.5)

for epoch in range(3):
    optimizer.step()
    scheduler.step()
    lr = optimizer.param_groups[0]['lr']
print(f"Learning rate after 3 epochs: {lr}")
ALearning rate after 3 epochs: 0.05
BLearning rate after 3 epochs: 0.1
CLearning rate after 3 epochs: 0.025
DLearning rate after 3 epochs: 0.2
Attempts:
2 left
💡 Hint

StepLR reduces the learning rate by gamma every step_size epochs.

Model Choice
advanced
2:00remaining
Choosing a scheduler for cyclic learning rate

You want the learning rate to cyclically increase and decrease during training to help escape local minima. Which PyTorch scheduler should you choose?

Atorch.optim.lr_scheduler.CyclicLR
Btorch.optim.lr_scheduler.StepLR
Ctorch.optim.lr_scheduler.ExponentialLR
Dtorch.optim.lr_scheduler.ReduceLROnPlateau
Attempts:
2 left
💡 Hint

Look for a scheduler that explicitly cycles the learning rate.

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

During training, you apply a learning rate scheduler that reduces the learning rate when validation loss plateaus. What effect do you expect on the training loss curve?

ATraining loss will remain constant regardless of scheduler.
BTraining loss will increase sharply after scheduler reduces learning rate.
CTraining loss will decrease more smoothly and possibly reach a lower minimum.
DTraining loss will oscillate wildly without any pattern.
Attempts:
2 left
💡 Hint

Reducing learning rate on plateau helps fine-tune the model.

🔧 Debug
expert
3:00remaining
Why does this CosineAnnealingLR scheduler not reduce learning rate as expected?

Consider this PyTorch code snippet:

import torch
import torch.optim as optim

params = [torch.nn.Parameter(torch.randn(1, requires_grad=True))]
optimizer = optim.SGD(params, lr=0.1)
scheduler = optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=5)

for epoch in range(10):
    optimizer.step()
    scheduler.step()
    print(f"Epoch {epoch+1}: lr = {optimizer.param_groups[0]['lr']}")

The learning rate resets after 5 epochs but does not decrease smoothly over 10 epochs as intended. What is the cause?

ACosineAnnealingLR requires gamma parameter to reduce learning rate, which is missing.
BThe learning rate is fixed and cannot be changed by CosineAnnealingLR.
CThe optimizer.step() must be called after scheduler.step() for correct learning rate update.
DThe scheduler restarts after T_max epochs; to have smooth decay over 10 epochs, T_max should be set to 10.
Attempts:
2 left
💡 Hint

Check the meaning of T_max parameter in CosineAnnealingLR.