o
    yiH                     @   sl   d dl mZmZ d dl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 G dd	 d	eZdS )
    )AnyListN)Tensor)Literal)_cosine_similarity_compute_cosine_similarity_update)Metric)dim_zero_catc                       s   e Zd ZU dZ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dZde
de
ddfddZde
fddZ  ZS )CosineSimilaritya  Computes the `Cosine Similarity`_ between targets and predictions:

    .. 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 torchmetrics import CosineSimilarity
        >>> target = torch.tensor([[0, 1], [1, 1]])
        >>> preds = torch.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_updatepredstargetsum	reduction)meanr   noneNkwargsreturnNc                    s\   t  jdi | d}||vrt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)dist_reduce_fxr    )super__init__
ValueErrorr   	add_state)selfr   r   allowed_reduction	__class__r   ]/home/ubuntu/.local/lib/python3.10/site-packages/torchmetrics/regression/cosine_similarity.pyr   =   s   zCosineSimilarity.__init__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!   updateK   s   zCosineSimilarity.updatec                 C   s"   t | j}t | j}t||| jS )N)r	   r   r   r   r   r#   r   r   r!   computeR   s   

zCosineSimilarity.compute)r   )__name__
__module____qualname____doc__r   bool__annotations__r   r   r   r   r   r   r   r$   r%   __classcell__r   r   r   r!   r
      s"   
 r
   )typingr   r   torchr   typing_extensionsr   4torchmetrics.functional.regression.cosine_similarityr   r   torchmetrics.metricr   torchmetrics.utilities.datar	   r
   r   r   r   r!   <module>   s   