a
    dS                     @   s   d dl mZ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sd	gZG d
d deZdS )    )AnyListOptionalSequenceUnion)Tensor)Literal)_cosine_similarity_compute_cosine_similarity_update)Metric)dim_zero_cat)_MATPLOTLIB_AVAILABLE)_AX_TYPE_PLOT_OUT_TYPECosineSimilarity.plotc                       s   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ed edd fddZeeddddZedddZdeeeee f  ee edddZ  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)meanr   noneNN)	reductionkwargsreturnc                    s\   t  jf i | d}||vr2td| d| || _| jdg dd | jdg dd d S )N)r   r   r   Nz+Expected argument `reduction` to be one of z	 but got r   cat)Zdist_reduce_fxr   )super__init__
ValueErrorr   Z	add_state)selfr   r   Zallowed_reduction	__class__ r/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/torchmetrics/regression/cosine_similarity.pyr!   F   s    zCosineSimilarity.__init__)r   r   r   c                 C   s*   t ||\}}| j| | j| dS )z2Update metric states with predictions and targets.N)r
   r   appendr   r#   r   r   r&   r&   r'   updateT   s    zCosineSimilarity.update)r   c                 C   s"   t | j}t | j}t||| jS )zCompute metric.)r   r   r   r	   r   r)   r&   r&   r'   compute[   s    

zCosineSimilarity.compute)valaxr   c                 C   s   |  ||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,), randn(10,))
            >>> 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,), randn(10,)))
            >>> fig, ax = metric.plot(values)

        )Z_plot)r#   r,   r-   r&   r&   r'   plota   s    (r   )r   )NN)__name__
__module____qualname____doc__r   bool__annotations__r   r   r   floatr   r   r   r   r   r!   r*   r+   r   r   r   r   r   r.   __classcell__r&   r&   r$   r'   r      s*   
  r   N)typingr   r   r   r   r   Ztorchr   Ztyping_extensionsr   Z4torchmetrics.functional.regression.cosine_similarityr	   r
   Ztorchmetrics.metricr   Ztorchmetrics.utilities.datar   Ztorchmetrics.utilities.importsr   Ztorchmetrics.utilities.plotr   r   Z__doctest_skip__r   r&   r&   r&   r'   <module>   s   