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, polynomial_decay
  • exponential_decay: base_lr * decay_rate ** (t / decay_steps) with 0 < decay_rate < 1
  • cosine, cosine_restarts (SGDR)
  • inverse_sqrt (Transformer)
  • one_cycle
  • triangular, triangular2, exp_range (cyclical learning rates)

polynomial_decay(start_lr, end_lr, total_steps, power): (start_lr - end_lr) * (1 - t/T)**p + end_lr, holds end_lr past total_steps. exponential_decay(base_lr, decay_rate, decay_steps): base_lr * decay_rate ** (t / decay_steps). step_decay(base_lr, drop, step_size): base_lr * drop ** floor(t / step_size), with 0 < drop <= 1.

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

lrsched-0.4.0.tar.gz (952.3 kB view details)

Uploaded Source

Built Distribution

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

lrsched-0.4.0-py3-none-any.whl (11.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: lrsched-0.4.0.tar.gz
  • Upload date:
  • Size: 952.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.3

File hashes

Hashes for lrsched-0.4.0.tar.gz
Algorithm Hash digest
SHA256 c53ae44ce0efe2652ed047c5788484408a160f60dabba7e4064a29af1c4ef672
MD5 1fc6ac97b49ef4fc92b0497694b06c70
BLAKE2b-256 b43c5a35507a326b73658f50958819beeaf1db264a8d57a1c1fbdc11002874e9

See more details on using hashes here.

File details

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

File metadata

  • Download URL: lrsched-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 11.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.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 37cdb3d9edd1e9c1fb996def9d268ea68ba2c4ec24fc2cf01eedf090b1e2381d
MD5 6de65578c0d789a5fe08d83dece73c50
BLAKE2b-256 420b8f9ddc6a64efa26e01125004b40c06740a5b4fd0aaca321ce9da0a8e697b

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