Files
Hamza 5245f3d18c Fix incorrect LR curves in Chained/Sequential scheduler doc images (#186468)
## Summary
---
The example learning-rate curve plots for the [`ChainedScheduler`](https://docs.pytorch.org/docs/2.12/generated/torch.optim.lr_scheduler.ChainedScheduler.html) and [`SequentialLR`](https://docs.pytorch.org/docs/2.12/generated/torch.optim.lr_scheduler.SequentialLR.html) schedulers in the documentation are inconsistent with the examples. As a result, the lr values listed in the `ChainedScheduler` docstring example were also wrong (this is not the case for `SequentialLR`).

The cause is in `docs/source/scripts/build_lr_scheduler_images.py`, which shares a single optimizer instance across all schedulers. Two issues result:

1. The optimizer's `lr` is reset to `0.05` *after* the `ConstantLR` and `ExponentialLR` (used by `SequentialLR`/`ChainedScheduler`) are constructed. Since `ConstantLR` applies its factor to the optimizer at construction time, this reset discards the correct starting lr.
2. `OneCycleLR`, which precedes `SequentialLR` in the loop, writes an `"initial_lr"` to the shared optimizer's param groups. `SequentialLR` then picks up those values during instantiation, corrupting the starting lr.

### Change

Use a scheduler factory for the `ConstantLR` and `ExponentialLR` used in `SequentialLR` and `ChainedScheduler`, so that all schedulers are instantiated within the loop.
```python
def constant_exponential_schedulers(opt):
    return [
        ConstantLR(opt, factor=0.1, total_iters=num_epochs // 5),
        ExponentialLR(opt, gamma=0.9),
    ]
```

Instantiate a new optimizer at each iteration to avoid state leakage between schedulers.
```python
def plot_function(scheduler):
    plt.clf()
    plt.grid(color="k", alpha=0.2, linestyle="--")
    lrs = []
    optimizer = optim.SGD(model.parameters(), lr=0.05)
    scheduler = scheduler(optimizer)
```
Pull Request resolved: https://github.com/pytorch/pytorch/pull/186468
Approved by: https://github.com/janeyx99
2026-08-06 01:28:34 +00:00
..