Skip to main content

Research-oriented robust loss library with PyTorch and NumPy backends

Project description

robust-loss-playground

Research-oriented robust loss library with PyTorch and NumPy backends.

Features

  • Residual-first API -- pass raw residuals, not predictions and targets
  • Unified M-estimation interface -- rho, influence, weight on every loss
  • 6 loss functions -- L2, L1, Huber, Charbonnier, Cauchy, Tukey
  • Two backends -- PyTorch (nn.Module) for training, NumPy for analysis
  • Built-in plotting -- plot_rho, plot_influence, plot_weight
  • Scale convention -- all losses follow rho(r; s) = s^2 * phi(r/s) (L1 excepted)

Installation

pip install -e ".[dev]"

Quick Start

import torch
from robust_loss.torch import Charbonnier

loss_fn = Charbonnier(scale=1.0, eps=0.01, reduction="mean")

residual = torch.randn(64)

# rho -- element-wise loss value
rho = loss_fn.rho(residual)

# influence -- psi(r) = d rho / d r
psi = loss_fn.influence(residual)

# weight -- w(r) = psi(r) / r, safe at r -> 0
w = loss_fn.weight(residual)

# forward / __call__ -- rho + reduction
scalar_loss = loss_fn(residual)

Supported Losses

Loss Key parameter Description
L2 -- Standard squared loss, rho = 1/2 r^2
L1 -- Absolute loss, rho = |r|
Huber delta Quadratic near zero, linear in the tails
Charbonnier eps Smooth L1 approximation via sqrt(r^2+eps^2)-eps
Cauchy -- Heavy-tailed, rho ~ log(1+r^2)
Tukey c Redescending; zero influence beyond |r|>c

NumPy Backend

Every loss is mirrored in the NumPy backend with the same API:

import numpy as np
from robust_loss.numpy import Cauchy

loss_fn = Cauchy(scale=1.0, reduction="none")

r = np.linspace(-5, 5, 200)
rho = loss_fn.rho(r)
psi = loss_fn.influence(r)
w = loss_fn.weight(r)

Examples

See the examples/ directory:

  • basic_usage.py -- minimal example of each loss
  • compare_losses.py -- side-by-side comparison across all 6 losses
  • irls_demo.py -- iteratively reweighted least squares using weight()
  • plot_gallery.py -- generate rho / influence / weight plots

Mathematical Details

Full formulas for every loss (rho, influence, weight, and limits) are documented in docs/formulas.md.

Design rationale is in docs/design.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

robust_loss_playground-0.1.0.tar.gz (122.3 kB view details)

Uploaded Source

Built Distribution

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

robust_loss_playground-0.1.0-py3-none-any.whl (17.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: robust_loss_playground-0.1.0.tar.gz
  • Upload date:
  • Size: 122.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.28 {"installer":{"name":"uv","version":"0.9.28","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for robust_loss_playground-0.1.0.tar.gz
Algorithm Hash digest
SHA256 3acde2cb37196cfcc50439c138fd34cc2025cedccf5603e9853ce91128017b2f
MD5 78cac27c0e38756104db4a19b2066848
BLAKE2b-256 27805a0635e8e8f3aab5f7932bfaf7371439a58c7be1555ba8ef7d0c076c9e54

See more details on using hashes here.

File details

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

File metadata

  • Download URL: robust_loss_playground-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 17.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.28 {"installer":{"name":"uv","version":"0.9.28","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for robust_loss_playground-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 783f50f94b2fda022b398b84a298a0d8914fffbf5f5d10a9d5e18ca355bb71ce
MD5 3cb783d5a36188a24a0307ea087e6ae0
BLAKE2b-256 e8941e5ba98939eba83d50cb24270a6160db6f9b6e66ff9d26da72c0a514d96e

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