o
    y“©ij  ã                
   @   s¨   d dl mZ 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 )é    )ÚTupleN)Ú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   ú_/home/ubuntu/.local/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 )a  Updates and returns variables required to compute LogCosh error.

    Checks for same shape of input tensors.

    Args:
        preds: Predicted tensor
        target: Ground truth tensor

    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Ún_obsr   r   r   Ú_log_cosh_error_update   s   
*r   r   r   c                 C   s   | |   ¡ S )z¨Computes Mean Squared Error.

    Args:
        sum_squared_error: Sum of LogCosh errors over all observations
        n_obs: Number of predictions or observations
    )r   )r   r   r   r   r   Ú_log_cosh_error_compute3   s   r   c                 C   s0   t | || jdkr
dn| jd d\}}t||ƒS )aC  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 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 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   )Útypingr   r   r   Ú(torchmetrics.functional.regression.utilsr   Útorchmetrics.utilities.checksr   r   Úintr   r   r   r   r   r   r   Ú<module>   s   "
