§
    ’©ºj�  ã                   óÖ   — d dl mZ d dlZd dlmZ d dlmZ d dlmZ d dlm	Z	 deded	e
d
eeef         fd„Z	 ddedeeef         ded         d
efd„Z	 ddeded	e
ded         d
ef
d„ZdS )é    )ÚUnionN)ÚTensor)ÚLiteral)Úkl_divergence)Ú_check_same_shapeÚpÚqÚlog_probÚreturnc                 ó„  — t          | |¦  «         | j        dk    s|j        dk    r t          d| j        › d|j        › d�¦  «        ‚| j        d         }|rt	          j        t	          j        | |g¦  «        d¬¦  «        t	          j        t	          j        d¦  «        ¦  «        z
  }dt          | ||d	¬
¦  «        z  dt          |||d	¬
¦  «        z  z   }ni| |  
                    dd¬¦  «        z  } || 
                    dd¬¦  «        z  }| |z   dz  }dt          | ||d	¬
¦  «        z  dt          |||d	¬
¦  «        z  z   }||fS )a£  Update and returns jensen-shannon divergence scores for each observation and the total number of observations.

    Args:
        p: data distribution with shape ``[N, d]``
        q: prior or approximate distribution with shape ``[N, d]``
        log_prob: bool indicating if input is log-probabilities or probabilities. If given as probabilities,
            will normalize to make sure the distributes sum to 1

    é   z4Expected both p and q distribution to be 2D but got z and z respectivelyr   )Údimg       @g      à?N)r
   Ú	reductionéÿÿÿÿT)ÚaxisÚkeepdim)r   ÚndimÚ
ValueErrorÚshapeÚtorchÚ	logsumexpÚstackÚlogÚtensorr   Úsum)r   r	   r
   ÚtotalÚmeanÚmeasuress         ú€/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/torchmetrics/functional/regression/js_divergence.pyÚ_jsd_updater       s�  € õ �a˜ÑÔÐØ„v�‚{€{�a”f ’k�kÝÐrÐPQÔPVÐrÐrÐ]^Ô]cÐrÐrÐrÑsÔsÐsàŒG�AŒJ€EØð 
ÝŒ�uœ{¨A¨q¨6Ñ2Ô2¸Ð:Ñ:Ô:½U¼YÅuÄ|ÐTWÑGXÔGXÑ=YÔ=YÑYˆØ� q¨$¸ÈTÐRÑRÔRÑRÐUXÕ[hØˆt˜h°$ð\
ñ \
ô \
ñ V
ñ 
ˆˆð �—’˜2 t�Ñ,Ô,Ñ,ˆØ�—’˜2 t�Ñ,Ô,Ñ,ˆØ�A‘˜‰{ˆØ� q¨$¸ÈTÐRÑRÔRÑRÐUXÕ[hØˆt˜h°$ð\
ñ \
ô \
ñ V
ñ 
ˆð �Uˆ?Ðó    r   r   r   r   )r   r   ÚnoneNc                 óŽ   — |dk    r|                       ¦   «         S |dk    r|                       ¦   «         |z  S |�|dk    r| S | |z  S )zPCompute and reduce the Jensen-Shannon divergence based on the type of reduction.r   r   Nr"   )r   )r   r   r   s      r   Ú_jsd_computer$   7   s[   € ð �EÒÐØ�|Š|‰~Œ~ÐØ�FÒÐØ�|Š|‰~Œ~ Ñ%Ð%ØÐ˜I¨Ò/Ð/ØˆØ�eÑÐr!   Fc                 óL   — t          | ||¦  «        \  }}t          |||¦  «        S )a*  Compute `Jensen-Shannon divergence`_.

    .. math::
        D_{JS}(P||Q) = \frac{1}{2} D_{KL}(P||M) + \frac{1}{2} D_{KL}(Q||M)

    Where :math:`P` and :math:`Q` are probability distributions where :math:`P` usually represents a distribution
    over data and :math:`Q` is often a prior or approximation of :math:`P`. :math:`D_{KL}` is the `KL divergence`_ and
    :math:`M` is the average of the two distributions. It should be noted that the Jensen-Shannon divergence is a
    symmetrical metric i.e. :math:`D_{JS}(P||Q) = D_{JS}(Q||P)`.

    Args:
        p: data distribution with shape ``[N, d]``
        q: prior or approximate distribution with shape ``[N, d]``
        log_prob: bool indicating if input is log-probabilities or probabilities. If given as probabilities,
            will normalize to make sure the distributes sum to 1
        reduction:
            Determines how to reduce over the ``N``/batch dimension:

            - ``'mean'`` [default]: Averages score across samples
            - ``'sum'``: Sum score across samples
            - ``'none'`` or ``None``: Returns score per sample

    Example:
        >>> from torch import tensor
        >>> p = tensor([[0.36, 0.48, 0.16]])
        >>> q = tensor([[1/3, 1/3, 1/3]])
        >>> jensen_shannon_divergence(p, q)
        tensor(0.0225)

    )r    r$   )r   r	   r
   r   r   r   s         r   Újensen_shannon_divergencer&   D   s,   € õB " ! Q¨Ñ1Ô1�O€HˆeÝ˜ %¨Ñ3Ô3Ð3r!   )r   )Fr   )Útypingr   r   r   Útyping_extensionsr   Ú0torchmetrics.functional.regression.kl_divergencer   Útorchmetrics.utilities.checksr   ÚboolÚtupleÚintr    r$   r&   © r!   r   ú<module>r/      sJ  ðð Ð Ð Ð Ð Ð à €€€Ø Ð Ð Ð Ð Ð Ø %Ð %Ð %Ð %Ð %Ð %à JÐ JÐ JÐ JÐ JÐ JØ ;Ð ;Ð ;Ð ;Ð ;Ð ;ð�6ð ˜fð °ð ¸¸vÀs¸{Ô9Kð ð ð ð ð> djð
ð 
Øð
Ø" 3¨ ;Ô/ð
Ø<CÐD_Ô<`ð
àð
ð 
ð 
ð 
ð ekð"4ð "4Øð"4Øð"4Ø$(ð"4Ø=DÐE`Ô=að"4àð"4ð "4ð "4ð "4ð "4ð "4r!   