o
    yi5                  	   @   s   d dl mZmZ d dlZd dlmZ d dlmZ dededeeef fdd	Zddededee defddZ	ddededee defddZ
dS )    )OptionalTupleN)Tensor)_check_same_shapepredstargetreturnc                 C   s"   t | | |  } | }| |fS )zUpdates and returns variables required to compute Cosine Similarity. Checks for same shape of input tensors.

    Args:
        preds: Predicted tensor
        target: Ground truth tensor
    )r   float)r   r    r
   h/home/ubuntu/.local/lib/python3.10/site-packages/torchmetrics/functional/regression/cosine_similarity.py_cosine_similarity_update   s   
r   sum	reductionc                 C   sZ   | | j dd}| jdd}|jdd}|||  }tj tjdd dd d}|| |S )a&  Computes Cosine Similarity.

    Args:
        preds: Predicted tensor
        target: Ground truth tensor
        reduction:
            The method of reducing along the batch dimension using sum, mean or taking the individual scores

    Example:
        >>> target = torch.tensor([[1, 2, 3, 4], [1, 2, 3, 4]])
        >>> preds = torch.tensor([[1, 2, 3, 4], [-1, -2, -3, -4]])
        >>> preds, target = _cosine_similarity_update(preds, target)
        >>> _cosine_similarity_compute(preds, target, 'none')
        tensor([ 1.0000, -1.0000])
    )dimc                 S      | S Nr
   xr
   r
   r   <lambda>@       z,_cosine_similarity_compute.<locals>.<lambda>c                 S   r   r   r
   r   r
   r
   r   r   A   r   )r   meannoneN)r   normtorchr   )r   r   r   dot_product
preds_normtarget_norm
similarityreduction_mappingr
   r
   r   _cosine_similarity_compute(   s   r    c                 C   s   t | |\} }t| ||S )a  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.

    Args:
        preds: Predicted tensor with shape ``(N,d)``
        target: Ground truth tensor with shape ``(N,d)``
        reduction:
            The method of reducing along the batch dimension using sum, mean or taking the individual scores

    Example:
        >>> from torchmetrics.functional.regression import cosine_similarity
        >>> target = torch.tensor([[1, 2, 3, 4],
        ...                        [1, 2, 3, 4]])
        >>> preds = torch.tensor([[1, 2, 3, 4],
        ...                       [-1, -2, -3, -4]])
        >>> cosine_similarity(preds, target, 'none')
        tensor([ 1.0000, -1.0000])
    )r   r    )r   r   r   r
   r
   r   cosine_similarityF   s   r!   )r   )typingr   r   r   r   torchmetrics.utilities.checksr   r   strr    r!   r
   r
   r
   r   <module>   s   

 $