o
    oiw                     @  sp   d Z ddlmZ ddlZddlm  mZ ddlm	Z	 dd	d
Z
dddZdddZddddZddddZdS )zALosses based on the divergence between probability distributions.    )annotationsN)Tensorpr   qreturnc                 C  sX   | j \}}}}tj||| ||  | || || dd}|d||}|S )Nnone)	reduction)shapeFkl_divreshapelogsumview)r   r   batchchansheightwidthunsummed_kl	kl_values r   L/home/ubuntu/.local/lib/python3.10/site-packages/kornia/losses/divergence.py
_kl_div_2d   s   *r   c                 C  s(   d| |  }dt | | dt ||  S )Ng      ?)r   )r   r   mr   r   r   
_js_div_2d&   s   r   lossesr   strc                 C  s(   |dkr| S |dkrt | S t | S )Nr   mean)torchr   r   )r   r   r   r   r   _reduce_loss-   s   r    r   predtargetc                 C     t t|| |S )a  Calculate the Jensen-Shannon divergence loss between heatmaps.

    Args:
        pred: the input tensor with shape :math:`(B, N, H, W)`.
        target: the target tensor with shape :math:`(B, N, H, W)`.
        reduction: 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.

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

    )r    r   r!   r"   r   r   r   r   js_div_loss_2d3      r%   c                 C  r#   )a  Calculate the Kullback-Leibler divergence loss between heatmaps.

    Args:
        pred: the input tensor with shape :math:`(B, N, H, W)`.
        target: the target tensor with shape :math:`(B, N, H, W)`.
        reduction: 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.

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

    )r    r   r$   r   r   r   kl_div_loss_2dI   r&   r'   )r   r   r   r   r   r   )r   r   r   r   r   r   )r   )r!   r   r"   r   r   r   r   r   )__doc__
__future__r   r   torch.nn.functionalnn
functionalr   kornia.corer   r   r   r    r%   r'   r   r   r   r   <module>   s   



