Challenge - 5 Problems
CosineAnnealingLR Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate2:00remaining
What does CosineAnnealingLR scheduler do during training?
Imagine you are training a model and using the CosineAnnealingLR scheduler. What best describes how the learning rate changes over time?
Attempts:
2 left
💡 Hint
Think about how cosine functions behave between 0 and pi.
✗ Incorrect
CosineAnnealingLR adjusts the learning rate by following a cosine curve, smoothly decreasing it from the initial value to a minimum and then optionally restarting. This helps the model avoid getting stuck and can improve training.
❓ Predict Output
intermediate2:00remaining
What is the learning rate after 5 epochs?
Given this PyTorch code snippet using CosineAnnealingLR, what is the learning rate at epoch 5?
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.CosineAnnealingLR(optimizer, T_max=10, eta_min=0.01) lrs = [] for epoch in range(6): scheduler.step() lrs.append(optimizer.param_groups[0]['lr']) print(lrs[-1])
Attempts:
2 left
💡 Hint
CosineAnnealingLR formula: lr = eta_min + (initial_lr - eta_min) * (1 + cos(pi * epoch / T_max)) / 2
✗ Incorrect
At epoch 5, the learning rate is calculated as 0.01 + (0.1 - 0.01) * (1 + cos(pi * 5 / 10)) / 2 = 0.01 + 0.09 * (1 + cos(pi/2)) / 2 = 0.01 + 0.09 * (1 + 0) / 2 = 0.01 + 0.045 = 0.055.
❓ Model Choice
advanced2:00remaining
Which scenario benefits most from using CosineAnnealingLR?
You want to train a deep neural network that tends to get stuck in local minima. Which training setup is best suited for CosineAnnealingLR?
Attempts:
2 left
💡 Hint
CosineAnnealingLR can be combined with restarts to help escape local minima.
✗ Incorrect
CosineAnnealingLR is especially useful for deep networks where the learning rate is gradually reduced and can be restarted to help the model jump out of local minima and explore better solutions.
❓ Hyperparameter
advanced2:00remaining
What effect does increasing T_max have in CosineAnnealingLR?
In the CosineAnnealingLR scheduler, what happens if you increase the T_max parameter while keeping other settings constant?
Attempts:
2 left
💡 Hint
T_max controls the period of the cosine cycle.
✗ Incorrect
T_max sets the number of epochs for one full cosine cycle. Increasing it makes the learning rate decrease more slowly, extending the time before it reaches the minimum value.
🔧 Debug
expert2:00remaining
Why does the learning rate not change as expected?
You wrote this code to use CosineAnnealingLR but the learning rate stays constant at 0.1 for all epochs. What is the most likely cause?
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.CosineAnnealingLR(optimizer, T_max=10, eta_min=0.01) for epoch in range(5): # training code here print(f"Epoch {epoch} lr: {optimizer.param_groups[0]['lr']}") # forgot to call scheduler.step()
Attempts:
2 left
💡 Hint
Schedulers need to be updated each epoch to change the learning rate.
✗ Incorrect
If scheduler.step() is not called, the learning rate will not update and will stay at the initial value throughout training.