a
    d%                     @   s   d dl mZmZ d dlZd dlmZ d dlmZ eeeeef dddZdeeee ed	d
dZ	deeee ed	ddZ
dS )    )OptionalTupleN)Tensor)_check_same_shape)predstargetreturnc                 C   s"   t | | |  } | }| |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

    )r   float)r   r    r
   }/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/torchmetrics/functional/regression/cosine_similarity.py_cosine_similarity_update   s    
r   sum)r   r   	reductionr   c                 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])

    )Zdimc                 S   s   | S Nr
   xr
   r
   r   <lambda>@       z,_cosine_similarity_compute.<locals>.<lambda>c                 S   s   | S r   r
   r   r
   r
   r   r   A   r   )r   meannoneN)r   Znormtorchr   )r   r   r   Zdot_productZ
preds_normZtarget_normZ
similarityZ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_similarityF   s    r   )r   )r   )typingr   r   r   r   Ztorchmetrics.utilities.checksr   r   strr   r   r
   r
   r
   r   <module>   s   
