Framework-agnostic learning-rate schedules as pure functions, in Python with zero dependencies.
Project description
lrsched
Framework-agnostic learning-rate schedules as pure functions, in Python with zero dependencies. Each schedule maps a step to a learning rate, so it works in any training loop or framework, or none.
Install
pip install lrsched
30-second example
from lrsched import cosine, with_warmup, sample
schedule = with_warmup(
cosine(base_lr=1e-3, min_lr=1e-5, total_steps=1000),
warmup_steps=100,
start_lr=0.0,
)
lr = schedule(250) # learning rate at step 250
curve = sample(schedule, num_steps=1000) # the whole curve, for plotting or logging
A schedule is just Callable[[int], float]. Plug schedule(step) into your optimizer
however your framework expects, or use it to drive a plain training loop.
Why this exists
Every learning-rate scheduler is tied to a framework: torch.optim.lr_scheduler,
timm, transformers, or optax for JAX. If you write a custom loop, use a
non-PyTorch stack, or just want to plot a schedule, you end up pasting a LambdaLR
snippet. lrsched is a small, dependency-free library where each schedule is a pure
function, easy to test, plot, log, and reuse anywhere.
Comparison
| lrsched | torch / timm | optax | |
|---|---|---|---|
| Framework | none | PyTorch | JAX |
| Pure step to lr function | yes | no (optimizer-bound) | partial |
| Zero dependencies | yes | no | no |
| Composable warmup and phases | yes | partial | yes |
Schedules
constant,step_decay,multi_step,exponentiallinear,polynomialcosine,cosine_restarts(SGDR)inverse_sqrt(Transformer)one_cycletriangular,triangular2,exp_range(cyclical learning rates)
Composition
with_warmup(schedule, ...)prepends a linear warmup.sequential(phases)runs schedules back to back.sample(schedule, num_steps=...)evaluates a schedule over a range.
Parameters are required keyword arguments, so every schedule reads explicitly at the call site. Schedules hold their final value past the end rather than erroring, and a negative step raises.
Per-parameter-group learning rates
scale_by_group applies fixed per-group multipliers to any base schedule, returning a
function from step to dict[str, float]. Useful for discriminative learning rates where
different layers or modules train at different rates:
from lrsched import cosine, scale_by_group
base = cosine(base_lr=1e-3, min_lr=1e-5, total_steps=1000)
group_lr = scale_by_group(base, multipliers={"backbone": 0.1, "head": 1.0})
lrs = group_lr(250) # {"backbone": ..., "head": ...}
# plug into your optimizer's param_groups, keyed by name
Each multiplier must be a positive finite number. An empty mapping and non-positive or
non-finite multipliers all raise ValueError with a descriptive message.
Command line
Sample a schedule from the terminal, one value per step or as a sparkline:
lrsched cosine --base-lr 1e-3 --min-lr 1e-5 --total-steps 1000 --steps 1000
lrsched triangular --min-lr 1e-4 --max-lr 1e-2 --step-size 200 --steps 800 --sparkline
Supports cosine, linear, exponential, step, triangular, and one-cycle.
Examples
python examples/schedules.py
Testing
pip install -e ".[dev]"
pytest
Tests cover the exact value of each schedule at known steps, schedule-specific shapes (restarts, the one-cycle peak, the warmup handoff), and invariants checked with Hypothesis (cosine stays within bounds, warmup is monotone).
Contributing
Issues and pull requests are welcome. See CONTRIBUTING.md.
License
MIT. See LICENSE.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file lrsched-0.3.0.tar.gz.
File metadata
- Download URL: lrsched-0.3.0.tar.gz
- Upload date:
- Size: 1.0 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ad9d4b262bb49549e0fb8e3f10c8c140d8d2c9a0b382486dce3b498a207be2aa
|
|
| MD5 |
baf91b2250e0d779ffcd9688490086f6
|
|
| BLAKE2b-256 |
f9a40b1b66d39d47241458bb965dc42c4093147f57f6e6ecbca58e967433a43d
|
File details
Details for the file lrsched-0.3.0-py3-none-any.whl.
File metadata
- Download URL: lrsched-0.3.0-py3-none-any.whl
- Upload date:
- Size: 11.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
67ea941058af44eeac614c7609affc6dc821943c7fc3159cdd0ccc2283b81025
|
|
| MD5 |
7d3d93d6e6748462c6a7d80af81f19ea
|
|
| BLAKE2b-256 |
0e2848861804764e50285b4e3ba5a7d25cfbe8f57179fae0a86f763457bca12e
|