o
    .wi,                  	   @   s   d dl 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 )    )OptionalN)Tensor)_check_same_shapepredstargetreturnc                 C   s<   t | | | jdkrtd| j |  } | }| |fS )zUpdate and returns variables required to compute Cosine Similarity. Checks for same shape of input tensors.

    Args:
        preds: Predicted tensor
        target: Ground truth tensor

       zExpected input to cosine similarity to be 2D tensors of shape `[N,D]` where `N` is the number of samples and `D` is the number of dimensions, but got tensor of shape )r   ndim
ValueErrorshapefloat)r   r    r   q/home/ubuntu/sommelier/.venv/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&  Compute 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>E       z,_cosine_similarity_compute.<locals>.<lambda>c                 S   r   r   r   r   r   r   r   r   F   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  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.

    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_similarityK   s   r$   )r   )typingr   r   r   torchmetrics.utilities.checksr   tupler   strr#   r$   r   r   r   r   <module>   s   

 $