Learning Rate Monitoring During PyTorch Training: Exploring `get_last_lr()`


Context: Learning Rate Scheduling in PyTorch

In deep learning optimization, the learning rate plays a crucial role in how quickly the model's weights are adjusted during training. A fixed learning rate might not be ideal throughout the entire training process, as it can lead to slow convergence or oscillations. Learning rate schedulers address this by dynamically adjusting the learning rate based on certain criteria.

ConstantLR Scheduler

The ConstantLR scheduler, as the name suggests, maintains a constant learning rate for a specified number of iterations (total_iters) before potentially applying a factor-based reduction. This can be useful for providing a stable learning rate during the initial phase of training.

get_last_lr() Method

The get_last_lr() method is an integral part of the ConstantLR scheduler. It allows you to retrieve the most recently applied learning rates for each parameter group in the optimizer. This is important for monitoring and debugging your training process, as it provides insights into how the learning rate is evolving.

How it Works

  1. Creation
    You create a ConstantLR scheduler instance by passing your optimizer object, along with optional arguments like the factor for learning rate reduction (factor), the total number of iterations for the constant rate (total_iters), and the initial epoch (last_epoch).
  2. Learning Rate Updates
    During training, the ConstantLR scheduler might not explicitly update the learning rate in each iteration. However, it keeps track of the internal state and applies the factor-based reduction when the specified number of iterations is reached.
  3. get_last_lr() Call
    When you call get_last_lr(), the scheduler returns a list containing the current learning rates for all parameter groups in the optimizer. These represent the most recently applied learning rates, which might have been the initial value or a factor-reduced version.

Key Points

  • Use get_last_lr() to track the learning rate progression and adjust your training hyperparameters if needed.
  • ConstantLR doesn't necessarily update the learning rate in every iteration, but get_last_lr() reflects the internal state.
  • get_last_lr() doesn't modify the learning rate itself; it simply provides access to the current values.


import torch
from torch import nn
from torch.optim import SGD
from torch.optim.lr_scheduler import ConstantLR

# Define a simple model
class MyModel(nn.Module):
    def __init__(self):
        super(MyModel, self).__init__()
        self.fc = nn.Linear(10, 1)

    def forward(self, x):
        return self.fc(x)

# Create model, optimizer, and scheduler
model = MyModel()
optimizer = SGD(model.parameters(), lr=0.1)  # Initial learning rate

# Constant learning rate for 5 epochs (iterations adjusted based on your setup)
scheduler = ConstantLR(optimizer, factor=0.5, total_iters=5*len(your_dataloader))

# Training loop (simplified)
for epoch in range(10):
    for data in your_dataloader:
        # Forward pass, backward pass, and optimizer update
        # ...

        # Access and print the current learning rates after each update step
        current_lrs = scheduler.get_last_lr()
        print(f"Epoch: {epoch+1}, Current Learning Rates: {current_lrs}")

    # Step the scheduler after each epoch (optional)
    scheduler.step()
  1. We define a simple model and create an optimizer with an initial learning rate of 0.1.
  2. We create a ConstantLR scheduler with a factor of 0.5, meaning it will halve the learning rate after 5 epochs (assuming total_iters is adjusted for your dataloader size).
  3. Inside the training loop, we access the current learning rates using scheduler.get_last_lr() after each update step and print them for monitoring.
  4. Optionally, we can call scheduler.step() after each epoch to explicitly step the scheduler (it might not be necessary with ConstantLR).


  1. Using a Different Learning Rate Scheduler

    PyTorch offers various learning rate schedulers with built-in functionality to access the current learning rates:

    • ReduceLROnPlateau: Tracks validation loss and reduces learning rate if the loss plateaus. It provides a get_lr() method for getting the current learning rates.
    • CosineAnnealingLR: Decreases the learning rate from an initial value to a minimum value following a cosine annealing schedule. It offers a get_lr() method as well.
    • StepLR: Decreases the learning rate by a factor at specified intervals (epochs or iterations). You can track the learning rate based on the factor and the initial value (similar to ConstantLR).
  • If you only care about general changes in the learning rate (e.g., decrease by a factor), manual tracking with the initial value can suffice.
  • If you need the most accurate current learning rates for all parameter groups, using a scheduler with a get_lr() method like ReduceLROnPlateau or CosineAnnealingLR is best.