Skip to main content

Noise-robust PyTorch optimizer with automatic confidence-based learning rate adaptation

Project description

CASMO: Confident Adaptive Selective Momentum Optimizer

PyPI version Downloads Python 3.8+ License: MIT Paper: Theory

The Optimizer That Knows What To Learn.

Abstract

CASMO is a novel optimization algorithm designed to address the limitations of uniform gradient application in deep learning. Unlike standard optimizers (e.g., AdamW, SGD) that treat all gradients as equally valid, CASMO introduces Adaptive Gradient-Aware Regularization (AGAR). This mechanism dynamically quantifies the "signal-to-noise" ratio of gradients over time, allowing the optimizer to selectively dampen updates that correspond to noise, outliers, or conflicting information while accelerating learning on generalizable patterns.

This approach yields state-of-the-art stability in challenging regimes such as label noise, long-tail distributions, differential privacy, and continual learning.


� Key Innovation: The AGAR Mechanism

The core of CASMO is the Adaptive Gradient Alignment Ratio (AGAR), a metric that measures the consistency of gradient direction over a sliding window:

$$ \text{AGAR} = \frac{||\mathbb{E}[g]||^2}{||\mathbb{E}[g]||^2 + \text{Var}[g]} $$

  • High AGAR ($\approx 1$): Indicates consistent, reliable signal (e.g., generalizable features). CASMO applies full updates.
  • Low AGAR ($\approx 0$): Indicates high variance or conflict (e.g., label noise, privacy noise, catastrophic forgetting). CASMO automatically dampens the learning rate.

This allows CASMO to act as an automatic filter during training, improving robustness without manual hyperparameter tuning or data filtering.


⚡ Quick Start

Installation

Via PyPI (Recommended):

pip install casmo-optimizer

From Source (For Development):

git clone https://github.com/abderahmane-ai/CASMO.git
cd CASMO
pip install -e .

Usage

CASMO is a drop-in replacement for torch.optim.AdamW.

from casmo import CASMO

# Initialize with standard parameters
optimizer = CASMO(
    model.parameters(), 
    lr=1e-3, 
    weight_decay=0.01,
    granularity='group'  # Recommended for efficiency
)

# Standard PyTorch training loop
for batch in dataloader:
    optimizer.zero_grad()
    loss = model(batch)
    loss.backward()
    optimizer.step()

🧪 Benchmarks & Reproducibility

We provide a comprehensive suite of benchmarks demonstrating CASMO's superiority in specific failure modes of standard optimizers. Each benchmark contains full reproduction scripts and detailed analysis.

Benchmark Domain Challenge Link
B2 Generalization Grokking with 30% Label Noise View Benchmark
B3 Long-Tail CIFAR-100 Class Imbalance (100:1) View Benchmark
B4 Privacy DP-SGD with High Noise ($\epsilon \approx 0.37$) View Benchmark
B6 Fine-Tuning Noisy Instruction Tuning (LLMs) View Benchmark
B7 Continual Learning Catastrophic Forgetting (Seq. Tasks) View Benchmark

🛠️ Configuration

Parameter Default Description
lr 1e-3 Base learning rate.
weight_decay 0.0 Decoupled weight decay (same as AdamW).
granularity 'group' 'group' (Fast, recommended) or 'parameter' (Precise).
tau_init_steps 500 Steps for initial AGAR calibration (auto-tuned).
c_min 0.1 Minimum confidence floor (prevents dead neurons).

Citation

If you use CASMO in your research, please cite:

@software{casmo2025,
  title={CASMO: Confident Adaptive Selective Momentum Optimizer},
  author={Ainouche, Abderahmane},
  year={2025},
  url={https://github.com/abderahmane-ai/CASMO}
}

Made with ❤️ for the ML research community.

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

casmo_optimizer-0.2.0.tar.gz (1.4 MB view details)

Uploaded Source

Built Distribution

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

casmo_optimizer-0.2.0-py3-none-any.whl (14.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: casmo_optimizer-0.2.0.tar.gz
  • Upload date:
  • Size: 1.4 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for casmo_optimizer-0.2.0.tar.gz
Algorithm Hash digest
SHA256 aaa3b8caee378f3735f9da889360584775a11b95af9260aeacdb34182f8eacf3
MD5 b4836cdd9a04217766e48f3d6a35a547
BLAKE2b-256 f60ddb27b5c34bb06643409440330cff4cb3a0e58bf1e5722f72a459ed361850

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for casmo_optimizer-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 efd4b6bd6736cc560fe5de5956123257b14f5e3eb48ab1d51fd8bfe455f0bb12
MD5 d8e5fc7ed28cafd2fa46691e63ce3189
BLAKE2b-256 c3d8e76268eb591851865ff3a84be93e17f8b71921dd3e3582a46135c98818ab

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