What does the ReduceLROnPlateau scheduler do in PyTorch?
Think about what happens when the model's performance metric does not improve.
The ReduceLROnPlateau scheduler lowers the learning rate if the monitored metric (like validation loss) does not improve for a set number of epochs. This helps the model converge better.
Given the following PyTorch code snippet, what will be the learning rate after 3 epochs if the validation loss does not improve?
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.ReduceLROnPlateau(optimizer, mode='min', factor=0.1, patience=2) val_losses = [0.5, 0.5, 0.5] for epoch, loss in enumerate(val_losses): scheduler.step(loss) print(f"Epoch {epoch+1} LR: {optimizer.param_groups[0]['lr']}")
Remember the patience is 2, so learning rate reduces after 2 epochs without improvement.
The learning rate stays at 0.1 for the first two epochs because patience=2 means it waits 2 epochs without improvement. On the third epoch, since loss did not improve, the learning rate reduces by factor 0.1 to 0.01.
You want to reduce the learning rate when validation accuracy stops improving. Which mode should you use in ReduceLROnPlateau?
Think about whether accuracy is better when higher or lower.
Accuracy improves when it increases, so the scheduler should watch for when the metric stops increasing. Thus, mode='max' is correct.
What is the effect of setting a high patience value in ReduceLROnPlateau?
Patience controls how long the scheduler waits before reducing LR.
A higher patience means the scheduler waits longer before reducing the learning rate, allowing more epochs without improvement.
Consider this code snippet:
import torch
import torch.optim as optim
model_params = [torch.nn.Parameter(torch.randn(2, 2, requires_grad=True))]
optimizer = optim.Adam(model_params, lr=0.01)
scheduler = optim.lr_scheduler.ReduceLROnPlateau(optimizer, mode='min', factor=0.5, patience=1)
val_losses = [0.3, 0.3, 0.3, 0.3]
for epoch, loss in enumerate(val_losses):
scheduler.step(loss)
print(f"Epoch {epoch+1} LR: {optimizer.param_groups[0]['lr']}")Why does the learning rate never reduce?
Check how the scheduler detects improvement and what happens if the metric stays the same.
The scheduler reduces LR only if the metric does not improve (decrease) for patience epochs. Here, the loss stays constant (0.3), which is not considered an improvement, but also not worse. By default, the scheduler treats equal values as no improvement, so it waits. Since patience=1, after 1 epoch without improvement, LR should reduce. If LR never reduces, it may be because the metric is not passed correctly or patience is too high. Here, the constant loss means no improvement, so LR should reduce after patience epochs.