a
    d                     @   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 eeeeef dddZeee	eeef d	d
dZ
eeedddZeeed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      )ndimZ	unsqueeze)r   r    r   t/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/torchmetrics/functional/regression/log_cosh.py_unsqueeze_tensors   s    
r   )r   r   num_outputsr   c                 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squeezeZtensorshaper   )r   r   r   diffsum_log_cosh_errorn_obsr   r   r   _log_cosh_error_update   s    
*r   )r   r   r   c                 C   s   | |   S )zCompute Mean Squared Error.

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

    )r   )r   r   r   r   r   _log_cosh_error_compute5   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   )typingr   r   r   Z(torchmetrics.functional.regression.utilsr   Ztorchmetrics.utilities.checksr   r   intr   r   r   r   r   r   r   <module>   s   