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 Key Result Link
B1 Generalization Grokking with 30% Label Noise 90.4% vs 24.0% val acc View
B2 Long-Tail CIFAR-100 Imbalance (100:1) Better tail-class accuracy View
B3 Privacy DP-SGD (ε ≈ 0.37) Maintains accuracy under DP View
B4 Continual Learning Sequential LLM Fine-tuning 13% less forgetting View
B5 Finance Portfolio Optimization Lower turnover, higher Sharpe View

See benchmarks/README.md for detailed results and reproduction instructions.


🛠️ 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.3.0.tar.gz (1.6 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.3.0-py3-none-any.whl (9.0 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for casmo_optimizer-0.3.0.tar.gz
Algorithm Hash digest
SHA256 2f41c10a1e09b8eac163dd7fa988d5509aed7ba04de6a51e5f25d84f7db42b76
MD5 b14ab18cd7769c6fc385df0ba4a8a463
BLAKE2b-256 235db0d8370a0a1456c7cd6e82d3e78df246ba499edff4cb7b0e3e4ffee1e252

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for casmo_optimizer-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f1bd1f56738694c2ab3890052abf2e6da4eb373d791b49d1dea1efc6e6b63c04
MD5 ee5914d065d0d56b5a4f2b722f4fa603
BLAKE2b-256 1694c666a33ce63857c363692e958d17150a424adec5f3955fd6b2e0ff4931b6

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