o
    .wi                  
   @   s   d dl Z d dl m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eded
edeeef fddZ	dededefddZ
dededefddZdS )    N)Tensor) _check_data_shape_to_num_outputs)_check_same_shapepredstargetreturnc                 C   s&   | j dkr	| |fS | d|dfS )N      )ndim	unsqueeze)r   r    r   h/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/torchmetrics/functional/regression/log_cosh.py_unsqueeze_tensors   s   
r   num_outputsc                 C   st   t | | t| || t| |\} }| | }tt|t|  d d }tj|j	d | j
d}||fS )aT  Update and returns variables required to compute LogCosh error.

    Check for same shape of input tensors.

    Args:
        preds: Predicted tensor
        target: Ground truth tensor
        num_outputs: Number of outputs in multioutput setting

    Return:
        Sum of LogCosh error over examples, and total number of examples

    r   r   )device)r   r   r   torchlogexpsumsqueezetensorshaper   )r   r   r   diffsum_log_cosh_errornum_obsr   r   r   _log_cosh_error_update   s   
*r   r   r   c                 C   s   | |   S )zCompute Mean Squared Error.

    Args:
        sum_log_cosh_error: Sum of LogCosh errors over all observations
        num_obs: Number of predictions or observations

    )r   )r   r   r   r   r   _log_cosh_error_compute4   s   r   c                 C   s0   t | || jdkr
dn| jd d\}}t||S )aZ  Compute the `LogCosh Error`_.

    .. math:: \text{LogCoshError} = \log\left(\frac{\exp(\hat{y} - y) + \exp(\hat{y - y})}{2}\right)

    Where :math:`y` is a tensor of target values, and :math:`\hat{y}` is a tensor of predictions.

    Args:
        preds: estimated labels with shape ``(batch_size,)`` or `(batch_size, num_outputs)``
        target: ground truth labels with shape ``(batch_size,)`` or `(batch_size, num_outputs)``

    Return:
        Tensor with LogCosh error

    Example (single output regression)::
        >>> from torchmetrics.functional.regression import log_cosh_error
        >>> preds = torch.tensor([3.0, 5.0, 2.5, 7.0])
        >>> target = torch.tensor([2.5, 5.0, 4.0, 8.0])
        >>> log_cosh_error(preds, target)
        tensor(0.3523)

    Example (multi output regression)::
        >>> from torchmetrics.functional.regression import log_cosh_error
        >>> preds = torch.tensor([[3.0, 5.0, 1.2], [-2.1, 2.5, 7.0]])
        >>> target = torch.tensor([[2.5, 5.0, 1.3], [0.3, 4.0, 8.0]])
        >>> log_cosh_error(preds, target)
        tensor([0.9176, 0.4277, 0.2194])

    r	   )r   )r   r
   r   r   )r   r   r   r   r   r   r   log_cosh_error?   s   

r   )r   r   (torchmetrics.functional.regression.utilsr   torchmetrics.utilities.checksr   tupler   intr   r   r   r   r   r   r   <module>   s   "