Distributions#

kornia.losses.js_div_loss_2d(pred, target, reduction='mean')[source]#

Calculate the Jensen-Shannon divergence loss between heatmaps.

Convention:
  • The divergence is \(\frac{1}{2} \mathrm{KL}(P \,\|\, M) + \frac{1}{2} \mathrm{KL}(Q \,\|\, M)\) with \(M = (P + Q) / 2\), in nats: symmetric in pred and target, and at most \(\ln 2\). The inputs and the reductions are those of kl_div_loss_2d(); see its Convention block.

  • A cell that is zero in both inputs contributes zero, including to the gradients. Both inputs must have the same shape; unsupported reductions raise NotImplementedError.

  • Float16 and bfloat16 inputs are evaluated in float32 to avoid underflow of the mixture probabilities. The returned loss retains the promoted input dtype.

Parameters:
  • pred (Tensor) – the input torch.Tensor with shape \((B, N, H, W)\).

  • target (Tensor) – the target torch.Tensor with shape \((B, N, H, W)\).

  • reduction (str, optional) – Specifies the reduction to apply to the output: 'none' | 'mean' | 'sum'. 'none': no reduction will be applied, 'mean': the sum of the output will be divided by the number of elements in the output, 'sum': the output will be summed. Default: "mean"

Return type:

Tensor

Examples

>>> pred = torch.full((1, 1, 2, 4), 0.125)
>>> loss = js_div_loss_2d(pred, pred)
>>> loss.item()
0.0
kornia.losses.kl_div_loss_2d(pred, target, reduction='mean')[source]#

Calculate the Kullback-Leibler divergence loss between heatmaps.

Convention:
  • kl_div_loss_2d(pred, target) is \(\mathrm{KL}(\text{target} \,\|\, \text{pred})\), the sum of target * (log(target) - log(pred)) over \(H \times W\) for every \((b, n)\): the order of torch’s F.kl_div(pred.log(), target). Losses and metrics maps it onto torch, scipy and torchmetrics.

  • The inputs are probabilities, each \((b, n)\) slice a distribution over \(H \times W\). The log is taken inside, so log-probabilities give NaN, and nothing is normalised. A zero in pred where target is positive gives inf.

  • reduction='none' returns \((B, N)\); the default 'mean' averages over the \(B N\) distributions and 'sum' adds them.

  • Both inputs must have the same shape. Unsupported reductions, including torch’s 'batchmean', raise NotImplementedError. A zero in target with a finite nonnegative pred contributes zero to the value and gradients, including when both entries are zero.

Parameters:
  • pred (Tensor) – the input torch.Tensor with shape \((B, N, H, W)\).

  • target (Tensor) – the target torch.Tensor with shape \((B, N, H, W)\).

  • reduction (str, optional) – Specifies the reduction to apply to the output: 'none' | 'mean' | 'sum'. 'none': no reduction will be applied, 'mean': the sum of the output will be divided by the number of elements in the output, 'sum': the output will be summed. Default: "mean"

Return type:

Tensor

Examples

>>> pred = torch.full((1, 1, 2, 4), 0.125)
>>> loss = kl_div_loss_2d(pred, pred)
>>> loss.item()
0.0