PyTorch - How to get learning rate during training?
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
In PyTorch, the current learning rate is stored on the optimizer, not on the model. The usual way to inspect it during training is to read optimizer.param_groups, and if you are using a scheduler you may also want to query the scheduler after each step.
Read the learning rate from the optimizer
Every optimizer has one or more parameter groups, and each group has its own lr value.
That is the simplest answer when you have only one parameter group.
Print the learning rate during training
A common pattern is to log it at the start of each epoch.
This works well when the learning rate changes infrequently or only once per epoch.
If you have multiple parameter groups
Sometimes different layers use different learning rates. In that case, there is no single global learning rate.
If your optimizer has multiple groups, read all of them rather than assuming param_groups[0] tells the whole story.
Using a scheduler changes when you should inspect it
If you use a learning rate scheduler, the logged value depends on when you call scheduler.step().
That is why learning rate logs can look "off" if you print them before the scheduler update but mentally interpret them as post-update values.
get_last_lr() can be clearer with schedulers
For scheduler-aware logging, PyTorch schedulers also expose get_last_lr().
This returns a list because, again, there may be multiple parameter groups. It is often the cleanest way to log learning rates when a scheduler is in control.
Where to log it
The right frequency depends on the schedule:
- per epoch for epoch-based schedulers
- per batch for batch-based schedules
- whenever you need diagnostics for debugging training instability
If the learning rate is constant, logging it every batch is just noise.
Logging to experiment trackers
If you use TensorBoard, Weights and Biases, or a custom logger, log the learning rate from the optimizer at the same moment you log loss. That keeps the training curve and the learning-rate history aligned, which makes scheduler debugging much easier.
Per-batch versus per-epoch schedules
Always match your logging cadence to the scheduler cadence. If the scheduler updates every batch, epoch-level logging can hide important learning-rate changes.
Common Pitfalls
- Looking for the learning rate on the model instead of the optimizer.
- Assuming there is only one learning rate when the optimizer has multiple parameter groups.
- Logging the value before
scheduler.step()and interpreting it as the updated rate. - Using
get_last_lr()without realizing it returns a list, not a scalar. - Forgetting that some schedulers change the rate every batch rather than every epoch.
Summary
- Read the learning rate from
optimizer.param_groups. - For a single parameter group,
optimizer.param_groups[0]["lr"]is usually enough. - If you use multiple groups, inspect each group's
lr. - With schedulers,
scheduler.get_last_lr()is often the clearest logging interface. - Always be explicit about whether you are reading the rate before or after the scheduler step.
Related reading
- Pytorch - Using more GPUs and increasing batch size makes training slower in DistributedDataParallel
- Pytorch DataLoader multiple data source
- Pytorch doesn't support one-hot vector?
- PyTorch equivalence for softmax_cross_entropy_with_logits
- Pytorch Autograd what does runtime error grad can be implicitly created only for scalar outputs mean
- PyTorch Binary Classification - same network structure, ''simpler'' data, but worse performance?
- Pytorch equivalent features in tensorflow?
- pytorch error multi-target not supported in CrossEntropyLoss
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.