Training (Losses, Optimizers & Schedulers)
LR Schedulers
Learning rate scheduling with StepLR, CosineAnnealingLR, and WarmupCosineLR backed by Rust.
ochreml.nn provides three learning rate schedulers — StepLR, CosineAnnealingLR, and WarmupCosineLR. Each accepts either an Optimizer instance (automatically updating optimizer.lr on every .step()) or a raw float base learning rate.
Import & Signatures
import ochreml.nn as nn
s1 = nn.StepLR(optimizer_or_lr, step_size, gamma=0.1)
s2 = nn.CosineAnnealingLR(optimizer_or_lr, T_max, eta_min=0.0)
s3 = nn.WarmupCosineLR(optimizer_or_lr, warmup_steps, total_steps, min_lr=0.0)Parameters
| Scheduler | Parameters | Behavior |
|---|---|---|
StepLR | optimizer_or_lr, step_size, gamma=0.1 | Multiplies learning rate by gamma every step_size calls to .step(). |
CosineAnnealingLR | optimizer_or_lr, T_max, eta_min=0.0 | Smooth cosine decay from initial LR down to eta_min over T_max steps. |
WarmupCosineLR | optimizer_or_lr, warmup_steps, total_steps, min_lr=0.0 | Linear warmup over warmup_steps, followed by cosine decay to min_lr. |
Methods
scheduler.get_lr() -> float— returns the current learning rate.scheduler.step() -> float— advances the schedule by one step, updatesoptimizer.lrif attached, and returns the new LR.
Usage Example
import ochreml.nn as nn
model = nn.Linear(32, 4)
optimizer = nn.AdamW(model.parameters(), lr=1e-3)
scheduler = nn.WarmupCosineLR(optimizer, warmup_steps=10, total_steps=100, min_lr=1e-5)
for step in range(100):
# ... forward, backward, optimizer.step() ...
current_lr = scheduler.step()