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

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.

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.1.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.1.0-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: lrsched-0.1.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.1.0.tar.gz
Algorithm Hash digest
SHA256 65a487fc6a82e4a43578f2da0ae988980d18e56b3b41e6b5e7ac686783b3f84b
MD5 bd25ecb3f464e259aaebb135cb55afa7
BLAKE2b-256 29af225091dd4424d507dfa486178a1141b30ba3df7fe9868f6ca87d941a9ed4

See more details on using hashes here.

File details

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

File metadata

  • Download URL: lrsched-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.6 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.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ce84fb4e6ed2145d42c470897cb9b28cef796bfe83fc7f356bd72bddd82adefa
MD5 843e52ff7983de19b3de8d7985b4002c
BLAKE2b-256 3570cb6bb67ff4bbdb00ee47d76eb99b3ef6d7223b4bb0b5337771167e301705

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