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 Divergence Loss

From Leeroopedia


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

Overview

Divergence Loss provides Kullback-Leibler (KL) and Jensen-Shannon (JS) divergence losses between 2D probability distribution heatmaps.

Description

This module implements two statistical divergence measures for comparing probability distributions represented as 2D heatmaps:

KL Divergence (Kullback-Leibler): Measures how one probability distribution diverges from a second expected probability distribution. It is asymmetric, meaning KL(P||Q) is not equal to KL(Q||P).

JS Divergence (Jensen-Shannon): A symmetrized and smoothed version of KL divergence, computed as:

JSD(PQ)=12DKL(PM)+12DKL(QM)

where M=12(P+Q).

Both losses operate on 4D tensors shaped as (B, N, H, W) representing batches of 2D heatmaps (e.g., keypoint heatmaps in pose estimation). The heatmaps are reshaped to compute divergences per channel.

Usage

Import these losses when comparing predicted heatmaps with ground-truth probability distributions, such as in human pose estimation, keypoint detection, or any task that involves predicting spatial probability maps.

Code Reference

Source Location

Signature

def js_div_loss_2d(
    pred: torch.Tensor,
    target: torch.Tensor,
    reduction: str = "mean",
) -> torch.Tensor: ...

def kl_div_loss_2d(
    pred: torch.Tensor,
    target: torch.Tensor,
    reduction: str = "mean",
) -> torch.Tensor: ...

Import

from kornia.losses import js_div_loss_2d
from kornia.losses import kl_div_loss_2d

I/O Contract

Inputs

Name Type Required Description
pred torch.Tensor Yes Input heatmap tensor with shape (B, N, H, W)
target torch.Tensor Yes Target heatmap tensor with shape (B, N, H, W)
reduction str No Reduction mode: 'none', 'mean' (default), or 'sum'

Outputs

Name Type Description
loss torch.Tensor Computed divergence loss; shape (B, N) for 'none', scalar for 'mean'/'sum'

Usage Examples

import torch
from kornia.losses import js_div_loss_2d, kl_div_loss_2d

# Create uniform probability heatmaps
pred = torch.full((1, 1, 2, 4), 0.125)
target = torch.full((1, 1, 2, 4), 0.125)

# JS divergence (symmetric)
js_loss = js_div_loss_2d(pred, target)
# Returns 0.0 for identical distributions

# KL divergence (asymmetric)
kl_loss = kl_div_loss_2d(pred, target)
# Returns 0.0 for identical distributions

# With different distributions
pred_diff = torch.rand(2, 3, 8, 8)
pred_diff = pred_diff / pred_diff.sum(dim=(-1, -2), keepdim=True)
target_diff = torch.rand(2, 3, 8, 8)
target_diff = target_diff / target_diff.sum(dim=(-1, -2), keepdim=True)
loss = js_div_loss_2d(pred_diff, target_diff, reduction="mean")

Related Pages

Page Connections

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