PyTorch-Compatible Learning-Rate Schedulers #
Schedulers whose phase boundaries and step counters follow the corresponding
torch.optim.lr_scheduler behavior. They remain pure Lean state machines, so a training run can
store, inspect, and reason about the exact scheduler state without calling PyTorch.
Schedulers.Core documents the zero-indexed counter convention, shared scalar operations, and
literature. The Native module provides simpler total schedules when compatibility is not the
contract.
Only schedules whose semantics differ from the native ones live here. StepLR is not duplicated:
the native Scheduler.StepDecay already computes base_lr * gamma ^ (step / step_size) with the
same zero-indexed counter, so it is the PyTorch-compatible step schedule as well.
PyTorch-compatible scheduler variants #
The schedulers below use formulas and step-count conventions chosen to match PyTorch's
torch.optim.lr_scheduler.* semantics more directly.
Important convention note (PyTorch last_epoch):
- In modern PyTorch, schedulers effectively start at
last_epoch = 0right after construction (fresh run withlast_epoch = -1in the constructor triggers an initial internal step). - We model that behavior by using a
currentStep : Nat := 0counter. Think:currentStepcorresponds to PyTorch'slast_epochafter construction.
These schedulers are LR-only (they do not mutate optimizer momentum/betas). If you need the full PyTorch OneCycle momentum behavior, consider adding a separate momentum schedule and stepping both in lockstep.
CosineAnnealingLR #
PyTorch-compatible CosineAnnealingLR.
Key behavior difference from TorchLean's native Scheduler.CosineAnnealing:
- PyTorch's
CosineAnnealingLRcontinues the cosine curve pastT_max(it is periodic with period2*T_max), rather than clamping toeta_min.
PyTorch reference: torch.optim.lr_scheduler.CosineAnnealingLR.
- baseLearningRate : α
Base learning rate (
base_lrs[i]). - halfCycleSteps : ℕ
Maximum number of steps in a half-cycle (
T_max). - minimumLearningRate : α
Minimum learning rate (
eta_min). - currentStep : ℕ
Step counter matching PyTorch
last_epochafter construction (0-indexed).
Instances For
Current learning rate for PyTorch-compatible cosine annealing.
Instances For
Advance PyTorch-compatible cosine annealing by one step.
Instances For
Create PyTorch-compatible cosine annealing at step zero.
Instances For
OneCycleLR (LR-only) #
Anneal strategy used by OneCycleLR (matches PyTorch "cos" or "linear").
- cosine : AnnealingStrategy
- linear : AnnealingStrategy
Instances For
Instances For
PyTorch-compatible OneCycleLR (LR-only).
Notes:
- This mirrors PyTorch's
OneCycleLRlearning-rate schedule only. PyTorch can also cycle momentum (or Adam'sbeta1); TorchLean keeps this scheduler pure and LR-only. - PyTorch defines:
initial_lr = max_lr / div_factormin_lr = initial_lr / final_div_factor(note:min_lris derived frominitial_lr, not directly frommax_lr).
- PyTorch uses "phase end steps" that are floats:
- phase 1 ends at
pct_start * total_steps - 1 - phase 2 ends at
total_steps - 1(andthree_phaseinserts a middle phase). This means the boundary can be fractional; the schedule uses interpolation ratios (pct) computed from these float endpoints. We match that behavior usingαarithmetic.
- phase 1 ends at
PyTorch reference: torch.optim.lr_scheduler.OneCycleLR.
- maximumLearningRate : α
Peak learning rate (
max_lr). - totalSteps : ℕ
Total number of steps (
total_steps). - increasingFraction : α
Fraction of steps spent increasing LR (
pct_start). - divisionFactor : α
div_factorused to deriveinitial_lr = max_lr / div_factor. - finalDivisionFactor : α
final_div_factorused to derivemin_lr = initial_lr / final_div_factor. - annealingStrategy : AnnealingStrategy
Anneal strategy (
cosorlinear). - threePhase : Bool
Use PyTorch's
three_phasevariant whentrue. - currentStep : ℕ
Step counter matching PyTorch
last_epochafter construction (0-indexed).
Instances For
Derived initial LR (max_lr / div_factor).
Instances For
Derived minimum LR (initial_lr / final_div_factor).
Instances For
PyTorch-compatible anneal helper (no clamping).
Instances For
Current learning rate for PyTorch-compatible one-cycle scheduling (LR-only).
Instances For
Advance PyTorch-compatible one-cycle scheduling by one step.
Instances For
Constructor for OneCycleLR starting at current_step = 0 (LR-only).
This mirrors the PyTorch parameterization:
initial_lr = max_lr / div_factormin_lr = initial_lr / final_div_factor- phase endpoints computed as
pct_start * total_steps - 1andtotal_steps - 1(with the optionalthree_phasemiddle phase).