Implementation:Kornia Kornia Welsch Loss
| Knowledge Sources | |
|---|---|
| Domains | Vision, Loss_Functions |
| Last Updated | 2026-02-09 15:00 GMT |
Overview
Welsch Loss (also known as Leclerc loss) is a robust regression loss function based on a Gaussian-like weighting that heavily down-weights outliers.
Description
The Welsch loss, also known as the Leclerc loss, is a robust loss function that uses an exponential weighting to suppress the influence of outliers. It belongs to the family of robust estimators described by Barron (2019).
The mathematical formulation is:
Where is the prediction and is the target. The loss is bounded between 0 and 1, meaning that as residuals grow large, the loss asymptotically approaches 1. This provides a very smooth and gradual transition from quadratic behavior near zero to complete saturation for large residuals.
Compared to Geman-McClure loss which has a hard saturation, Welsch loss provides a smoother exponential decay, making it less aggressive but still highly robust.
Usage
Import this loss for robust regression tasks in computer vision where data may contain outliers, such as optical flow estimation, depth prediction, and image alignment. Its smooth behavior makes it particularly suitable when gradual outlier suppression is preferred over hard rejection.
Code Reference
Source Location
- Repository: Kornia
- File: kornia/losses/welsch.py
- Lines: 1-133
Signature
def welsch_loss(
img1: torch.Tensor,
img2: torch.Tensor,
reduction: str = "none",
) -> torch.Tensor: ...
class WelschLoss(nn.Module):
def __init__(self, reduction: str = "none") -> None: ...
def forward(self, img1: torch.Tensor, img2: torch.Tensor) -> torch.Tensor: ...
Import
from kornia.losses import WelschLoss
from kornia.losses import welsch_loss
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| img1 | torch.Tensor | Yes | Predicted tensor with arbitrary shape (*) |
| img2 | torch.Tensor | Yes | Target tensor with the same shape as img1 |
| reduction | str | No | Reduction mode: 'none' (default), 'mean', or 'sum' |
Outputs
| Name | Type | Description |
|---|---|---|
| loss | torch.Tensor | Computed Welsch loss; element-wise for 'none' (bounded in [0, 1]), scalar for 'mean'/'sum' |
Usage Examples
import torch
from kornia.losses import WelschLoss
# Create sample tensors
img1 = torch.randn(2, 3, 32, 32, requires_grad=True)
img2 = torch.randn(2, 3, 32, 32)
# Using the module API
criterion = WelschLoss(reduction="mean")
output = criterion(img1, img2)
output.backward()
# Using the functional API
from kornia.losses import welsch_loss
output = welsch_loss(img1, img2, reduction="sum")