§
    ’©ºjs  ã                   ó¬   — d dl mZ d dlmZmZmZmZ d dlmZ d dl	m
Z
 d dlmZmZ d dlmZ d dlmZ d dlmZ d d	lmZmZ esd
gZ G d„ de¦  «        ZdS )é    )ÚSequence)ÚAnyÚListÚOptionalÚUnion)ÚTensor)ÚLiteral)Ú_cosine_similarity_computeÚ_cosine_similarity_update)ÚMetric)Údim_zero_cat)Ú_MATPLOTLIB_AVAILABLE)Ú_AX_TYPEÚ_PLOT_OUT_TYPEzCosineSimilarity.plotc                   ó   ‡ — e Zd ZU dZdZeed<   dZeed<   dZeed<   dZ	e
ed<   d	Ze
ed
<   ee         ed<   ee         ed<   	 dded         deddfˆ fd„Zdededdfd„Zdefd„Z	 ddeeeee         f                  dee         defd„Zˆ xZS )ÚCosineSimilaritya  Compute the `Cosine Similarity`_.

    .. math::
        cos_{sim}(x,y) = \frac{x \cdot y}{||x|| \cdot ||y||} =
        \frac{\sum_{i=1}^n x_i y_i}{\sqrt{\sum_{i=1}^n x_i^2}\sqrt{\sum_{i=1}^n y_i^2}}

    where :math:`y` is a tensor of target values, and :math:`x` is a tensor of predictions.

    As input to ``forward`` and ``update`` the metric accepts the following input:

    - ``preds`` (:class:`~torch.Tensor`): Predicted float tensor with shape ``(N,d)``
    - ``target`` (:class:`~torch.Tensor`): Ground truth float tensor with shape ``(N,d)``

    As output of ``forward`` and ``compute`` the metric returns the following output:

    - ``cosine_similarity`` (:class:`~torch.Tensor`): A float tensor with the cosine similarity

    Args:
        reduction: how to reduce over the batch dimension using 'sum', 'mean' or 'none' (taking the individual scores)
        kwargs: Additional keyword arguments, see :ref:`Metric kwargs` for more info.

    Example:
        >>> from torch import tensor
        >>> from torchmetrics.regression import CosineSimilarity
        >>> target = tensor([[0, 1], [1, 1]])
        >>> preds = tensor([[0, 1], [0, 1]])
        >>> cosine_similarity = CosineSimilarity(reduction = 'mean')
        >>> cosine_similarity(preds, target)
        tensor(0.8536)

    TÚis_differentiableÚhigher_is_betterFÚfull_state_updateg        Úplot_lower_boundg      ð?Úplot_upper_boundÚpredsÚtargetÚsumÚ	reduction)Úmeanr   ÚnoneNÚkwargsÚreturnNc                 óÞ   •—  t          ¦   «         j        di |¤Ž d}||vrt          d|› d|› �¦  «        ‚|| _        |                      dg d¬¦  «         |                      dg d¬¦  «         d S )	N)r   r   r   Nz+Expected argument `reduction` to be one of z	 but got r   Úcat)Údist_reduce_fxr   © )ÚsuperÚ__init__Ú
ValueErrorr   Ú	add_state)Úselfr   r   Úallowed_reductionÚ	__class__s       €úy/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/torchmetrics/regression/cosine_similarity.pyr%   zCosineSimilarity.__init__H   s’   ø€ ð
 	�‰ŒÔÐ"Ð"˜6Ð"Ð"Ð"Ø9ÐØÐ-Ð-Ð-ÝÐrÐK\ÐrÐrÐgpÐrÐrÑsÔsÐsØ"ˆŒà�Š�w °5ˆÑ9Ô9Ð9Ø�Š�x °EˆÑ:Ô:Ð:Ð:Ð:ó    c                 ó”   — t          ||¦  «        \  }}| j                             |¦  «         | j                             |¦  «         dS )z2Update metric states with predictions and targets.N)r   r   Úappendr   ©r(   r   r   s      r+   ÚupdatezCosineSimilarity.updateV   sG   € å1°%¸Ñ@Ô@‰ˆˆvàŒ
×Ò˜%Ñ Ô Ð ØŒ×Ò˜6Ñ"Ô"Ð"Ð"Ð"r,   c                 ó~   — t          | j        ¦  «        }t          | j        ¦  «        }t          ||| j        ¦  «        S )zCompute metric.)r   r   r   r
   r   r/   s      r+   ÚcomputezCosineSimilarity.compute]   s4   € å˜TœZÑ(Ô(ˆÝ˜dœkÑ*Ô*ˆÝ)¨%°¸¼ÑHÔHÐHr,   ÚvalÚaxc                 ó.   — |                       ||¦  «        S )a  Plot a single or multiple values from the metric.

        Args:
            val: Either a single result from calling `metric.forward` or `metric.compute` or a list of these results.
                If no value is provided, will automatically call `metric.compute` and plot that result.
            ax: An matplotlib axis object. If provided will add plot to that axis

        Returns:
            Figure and Axes object

        Raises:
            ModuleNotFoundError:
                If `matplotlib` is not installed

        .. plot::
            :scale: 75

            >>> from torch import randn
            >>> # Example plotting a single value
            >>> from torchmetrics.regression import CosineSimilarity
            >>> metric = CosineSimilarity()
            >>> metric.update(randn(10,2), randn(10,2))
            >>> fig_, ax_ = metric.plot()

        .. plot::
            :scale: 75

            >>> from torch import randn
            >>> # Example plotting multiple values
            >>> from torchmetrics.regression import CosineSimilarity
            >>> metric = CosineSimilarity()
            >>> values = []
            >>> for _ in range(10):
            ...     values.append(metric(randn(10,2), randn(10,2)))
            >>> fig, ax = metric.plot(values)

        )Ú_plot)r(   r3   r4   s      r+   ÚplotzCosineSimilarity.plotc   s   € ðP �zŠz˜#˜rÑ"Ô"Ð"r,   )r   )NN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ÚboolÚ__annotations__r   r   r   Úfloatr   r   r   r	   r   r%   r0   r2   r   r   r   r   r   r7   Ú__classcell__)r*   s   @r+   r   r      s‰  ø€ € € € € € ðð ð@ #Ð�tÐ"Ð"Ñ"Ø!Ð�dÐ!Ð!Ñ!Ø#Ð�tÐ#Ð#Ñ#Ø!Ð�eÐ!Ð!Ñ!Ø!Ð�eÐ!Ð!Ñ!à�Œ<ÐÐÑØ�ŒLÐÐÑð ;@ð;ð ;àÐ6Ô7ð;ð ð;ð 
ð	;ð ;ð ;ð ;ð ;ð ;ð#˜Fð #¨Fð #°tð #ð #ð #ð #ðI˜ð Ið Ið Ið Ið _cð(#ð (#Ø˜E &¨(°6Ô*:Ð":Ô;Ô<ð(#ØIQÐRZÔI[ð(#à	ð(#ð (#ð (#ð (#ð (#ð (#ð (#ð (#r,   r   N)Úcollections.abcr   Útypingr   r   r   r   Útorchr   Útyping_extensionsr	   Ú4torchmetrics.functional.regression.cosine_similarityr
   r   Útorchmetrics.metricr   Útorchmetrics.utilities.datar   Útorchmetrics.utilities.importsr   Útorchmetrics.utilities.plotr   r   Ú__doctest_skip__r   r#   r,   r+   ú<module>rJ      s  ðð %Ð $Ð $Ð $Ð $Ð $Ø -Ð -Ð -Ð -Ð -Ð -Ð -Ð -Ð -Ð -Ð -Ð -à Ð Ð Ð Ð Ð Ø %Ð %Ð %Ð %Ð %Ð %à vÐ vÐ vÐ vÐ vÐ vÐ vÐ vØ &Ð &Ð &Ð &Ð &Ð &Ø 4Ð 4Ð 4Ð 4Ð 4Ð 4Ø @Ð @Ð @Ð @Ð @Ð @Ø @Ð @Ð @Ð @Ð @Ð @Ð @Ð @àð 1Ø/Ð0Ððm#ð m#ð m#ð m#ð m#�vñ m#ô m#ð m#ð m#ð m#r,   