Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Kornia Kornia Cauchy Loss

From Leeroopedia


Knowledge Sources
Domains Vision, Loss_Functions
Last Updated 2026-02-09 15:00 GMT

Overview

Cauchy Loss (also known as Lorentzian loss) is a robust regression loss function that is less sensitive to outliers than L2 loss.

Description

The Cauchy loss, also known as the Lorentzian loss, is a robust loss function used for regression tasks. It belongs to the family of robust estimators described by Barron (2019). The loss grows logarithmically for large residuals, making it significantly more robust to outliers compared to the squared error (L2) loss.

The mathematical formulation is:

CL(x,y)=log(12(xy)2+1)

Where x is the prediction and y is the target. For small residuals, the loss behaves similarly to L2 loss, but for large residuals it transitions to logarithmic growth, effectively limiting the influence of outliers.

Usage

Import this loss for robust regression tasks where the data may contain outliers. It is particularly useful in image reconstruction, depth estimation, and optical flow tasks where occasional large errors should not dominate the training signal.

Code Reference

Source Location

Signature

def cauchy_loss(
    img1: torch.Tensor,
    img2: torch.Tensor,
    reduction: str = "none",
) -> torch.Tensor: ...

class CauchyLoss(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 CauchyLoss
from kornia.losses import cauchy_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 Cauchy loss; element-wise for 'none', scalar for 'mean'/'sum'

Usage Examples

import torch
from kornia.losses import CauchyLoss

# 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 = CauchyLoss(reduction="mean")
output = criterion(img1, img2)
output.backward()

# Using the functional API
from kornia.losses import cauchy_loss
output = cauchy_loss(img1, img2, reduction="sum")

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment