
    3j                    h    S r SSKJr  SSKrSSKJs  Jr  S	S jrS	S jr	S
S jr
SSS jjrSSS jjrg)zALosses based on the divergence between probability distributions.    )annotationsNc                    U R                   u  p#pE[        R                  " UR                  X#-  XE-  5      R	                  5       U R                  X#-  XE-  5      SS9nUR                  S5      R                  X#5      nU$ )Nnone)	reduction)shapeFkl_divreshapelogsumview)pqbatchchansheightwidthunsummed_kl	kl_valuess           R/home/wildlama/miniconda3/lib/python3.13/site-packages/kornia/losses/divergence.py
_kl_div_2dr      sp    "#''E&((			%-0446		%-QWQ_8`lrK #((6I    c                J    SX-   -  nS[        X5      -  S[        X5      -  -   $ )Ng      ?)r   )r   r   ms      r   
_js_div_2dr   $   s,    quAA!!C*Q*:$:::r   c                v    US:X  a  U $ US:X  a  [         R                  " U 5      $ [         R                  " U 5      $ )Nr   mean)torchr   r   )lossesr   s     r   _reduce_lossr!   +   s3    F!*f!45::fK%))F:KKr   c                ,    [        [        X5      U5      $ )au  Calculate the Jensen-Shannon divergence loss between heatmaps.

Args:
    pred: the input torch.Tensor with shape :math:`(B, N, H, W)`.
    target: the target torch.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   predtargetr   s      r   js_div_loss_2dr&   1       & 
60)<<r   c                ,    [        [        X5      U5      $ )aw  Calculate the Kullback-Leibler divergence loss between heatmaps.

Args:
    pred: the input torch.Tensor with shape :math:`(B, N, H, W)`.
    target: the target torch.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#   s      r   kl_div_loss_2dr)   G   r'   r   )r   torch.Tensorr   r*   returnr*   )r    r*   r   strr+   r*   )r   )r$   r*   r%   r*   r   r,   r+   r*   )__doc__
__future__r   r   torch.nn.functionalnn
functionalr	   r   r   r!   r&   r)    r   r   <module>r3      s1   $ I "   ;L=,=r   