Skip to main content

Framework-agnostic learning-rate schedules as pure functions, in Python with zero dependencies.

Project description

lrsched

lrsched logo

PyPI CI License: MIT

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, exponential
  • linear, polynomial
  • cosine, cosine_restarts (SGDR)
  • inverse_sqrt (Transformer)
  • one_cycle
  • triangular, 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.

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

lrsched-0.2.0.tar.gz (1.0 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

lrsched-0.2.0-py3-none-any.whl (9.9 kB view details)

Uploaded Python 3

File details

Details for the file lrsched-0.2.0.tar.gz.

File metadata

  • Download URL: lrsched-0.2.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

Hashes for lrsched-0.2.0.tar.gz
Algorithm Hash digest
SHA256 c98b3103ff382a6355b61dd6e246bdba73ee903823353141197fac28d0f039bf
MD5 0824ebe8cd74a7f4a0d4c12ecaca036a
BLAKE2b-256 95b49858739c003c4c0b963e87193e3fc6e0b7dc73b5c8996b6f5c6932366cf7

See more details on using hashes here.

File details

Details for the file lrsched-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: lrsched-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 9.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.3

File hashes

Hashes for lrsched-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6b5a261137ddb935ec90b6a0e448a7fe61b7024875441a8b42130962fa025769
MD5 c4ad917ef39777172668626df235f22b
BLAKE2b-256 8206b91848b07a03a99eded08c625ed660aa7e5972eb01f27a30ce94a31e63ce

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page