Skip to main content

ECG-inspired cyclical learning-rate scheduler with training modes

Project description

SandevistanLR — ECG-Inspired Cyclical Learning Rate

Official ECG template (P–QRS–T) — reference

Official ECG P–QRS–T components

Reference ECG diagram (Public Domain) from SinusRhythmLabels.svg (Wikimedia Commons).

SandevistanLR waveform (ECG-shaped learning rate)

SandevistanLR Preview

Python PyTorch TensorFlow License Contributions Welcome

A physiologically-inspired learning-rate scheduler where the LR waveform follows a realistic ECG template (P–QRS–T) instead of triangle/cosine curves.

Inspired by CLR, 1cycle, and warm restarts, SandevistanLR introduces four training modes:

  • normal — standard ECG rhythm
  • run — faster rhythm + exercise-like morphology changes
  • die — flatline (very low constant LR)
  • aed — flatline + controlled shock spikes (“defibrillation”)

Status: This project is actively under development.
The goal is to produce a research-grade scheduler with strong empirical evidence across vision + tabular benchmarks, with results packaged for a conference-ready submission (e.g., CVPR / NeurIPS / ICLR workshops).

Motivation (LLMs & short runs): SandevistanLR is step-based (per-batch updates), which makes it especially promising for large-scale training and fine-tuning where runs are often limited to very few epochs (sometimes only 1).
In these regimes, per-epoch LR schedules can be too coarse; step-level “heartbeat” cycles provide controlled exploration and stabilization even in short training budgets.


Preset Configurations (Recommended)

SandevistanLR ships with ready-to-use preset configurations that cover multiple training behaviors such as:

  • warmup → normal → cooldown
  • athlete-like training cycles
  • intermittent flatlines
  • die → aed → recovery protocols
  • interval training
  • stress-test / arrhythmia-style schedules

Where to find the presets

  • Code (presets): src/sandevistanlr/configs.py
    This file defines a dictionary of named configs (CONFIGS) and their corresponding schedule plans and parameters. The preset configurations are intentionally provided as strong starting points.
    SandevistanLR is fully parameterizable, allowing users to generate infinitely many custom schedules by tuning LR bounds, cycle length, ECG morphology, and mode transitions to match their training budget and stability requirements.

Visual previews

All preset waveforms have been pre-generated and are available in figures/.

Below are a few previews directly rendered inside the README:

Default ECG (normal)

01 Default ECG (normal)

Warmup → Normal → Cooldown

02 Warmup Normal Cooldown

Die → AED → Recovery

05 Die AED Recovery

High-energy Run mode

10 High Energy Run Mode

These images are intended to provide an immediate visual understanding of each schedule’s behavior before integrating it into training.


Install

Option 1 — PyPI

pip install sandevistanlr

Option 2 — From source

git clone https://github.com/takfarine/SandevistanLR.git

Usage

Option 1 — PyTorch (per-step / per-batch)

import torch
from sandevistanlr import SandevistanLR

optimizer = torch.optim.AdamW(model.parameters(), lr=1e-3)

sched = SandevistanLR(
    lr_min=1e-6,
    lr_max=3e-4,
    steps_per_cycle=2000,
    mode="normal",  # normal | run | die | aed
)

global_step = 0
for epoch in range(EPOCHS):
    for batch in loader:
        # update LR per step
        lr = sched(global_step)
        for pg in optimizer.param_groups:
            pg["lr"] = lr

        loss = train_step(batch)
        loss.backward()
        optimizer.step()
        optimizer.zero_grad()

        global_step += 1

Option 2 — TensorFlow / Keras (manual LR assignment)

import tensorflow as tf
from sandevistanlr import SandevistanLR

optimizer = tf.keras.optimizers.AdamW(learning_rate=1e-3)

sched = SandevistanLR(
    lr_min=1e-6,
    lr_max=3e-4,
    steps_per_cycle=2000,
    mode="run",
)

global_step = tf.Variable(0, dtype=tf.int64)

@tf.function
def train_step(x, y):
    # update LR per step
    lr = sched(int(global_step.numpy()))
    optimizer.learning_rate.assign(lr)

    with tf.GradientTape() as tape:
        pred = model(x, training=True)
        loss = loss_fn(y, pred)

    grads = tape.gradient(loss, model.trainable_variables)
    optimizer.apply_gradients(zip(grads, model.trainable_variables))

    global_step.assign_add(1)
    return loss

Code Examples

End-to-end examples are provided under code_examples/:

  • PyTorchcode_examples/pytorch_demo.ipynb
  • TensorFlow / Kerascode_examples/tensorflow_demo.ipynb
  • Waveform plotscode_examples/plot_waveforms.ipynb

References

Learning-rate scheduling

ECG foundations and original electrocardiography work


License

Apache License 2.0 — 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

sandevistanlr-0.1.0.tar.gz (22.1 kB view details)

Uploaded Source

Built Distribution

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

sandevistanlr-0.1.0-py3-none-any.whl (20.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: sandevistanlr-0.1.0.tar.gz
  • Upload date:
  • Size: 22.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.8.10

File hashes

Hashes for sandevistanlr-0.1.0.tar.gz
Algorithm Hash digest
SHA256 75b983cdfe0101710d8bcf9e727720cc8e3ebe217c1a7490957a090717e04b73
MD5 e90ec976eb99068a0a2355abe6d9c835
BLAKE2b-256 e34f62cd1d42fc839aad7c9e931939c86f8044f952a3dda6874d972f73b47010

See more details on using hashes here.

File details

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

File metadata

  • Download URL: sandevistanlr-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 20.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.8.10

File hashes

Hashes for sandevistanlr-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3da02d3fc981c8eb9212c9a67a82e2841b96bceda76dde0904774899c2867c28
MD5 143d0fb992f8effc33f714226222085a
BLAKE2b-256 05f7086d5626b1029ca5fa4211ee240943c2564abe188639f5ecca059f119d92

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