a
    d                     @   s~   d dl mZmZ d dlZd dlmZ d dlmZ deeeeeef dddZ	eeeef ed	d
dZ
eeedddZdS )    )TupleUnionN)Tensor)_check_same_shape-`>)predstargetepsilonreturnc                 C   sX   t | | t| | }|tjt|t|  |d }dt| }| }||fS )a
  Update and returns variables required to compute Symmetric Mean Absolute Percentage Error.

    Check for same shape of input tensors.

    Args:
        preds: Predicted tensor
        target: Ground truth tensor
        epsilon: Avoids ``ZeroDivisionError``.

    )min   )r   torchabsclampsumZnumel)r   r   r	   Zabs_diffZabs_per_errorsum_abs_per_errornum_obs r   z/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/torchmetrics/functional/regression/symmetric_mape.py0_symmetric_mean_absolute_percentage_error_update   s    
"r   )r   r   r
   c                 C   s   | | S )a  Compute Symmetric Mean Absolute Percentage Error.

    Args:
        sum_abs_per_error: Sum of values of symmetric absolute percentage errors over all observations
            ``(symmetric absolute percentage error = 2 * |target - prediction| / (target + prediction))``
        num_obs: Number of predictions or observations

    Example:
        >>> target = torch.tensor([1, 10, 1e6])
        >>> preds = torch.tensor([0.9, 15, 1.2e6])
        >>> sum_abs_per_error, num_obs = _symmetric_mean_absolute_percentage_error_update(preds, target)
        >>> _symmetric_mean_absolute_percentage_error_compute(sum_abs_per_error, num_obs)
        tensor(0.2290)

    r   )r   r   r   r   r   1_symmetric_mean_absolute_percentage_error_compute1   s    r   )r   r   r
   c                 C   s   t | |\}}t||S )a  Compute symmetric mean absolute percentage error (SMAPE_).

    .. math:: \text{SMAPE} = \frac{2}{n}\sum_1^n\frac{|   y_i - \hat{y_i} |}{max(| y_i | + | \hat{y_i} |, \epsilon)}

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

    Args:
        preds: estimated labels
        target: ground truth labels

    Return:
        Tensor with SMAPE.

    Example:
        >>> from torchmetrics.functional.regression import symmetric_mean_absolute_percentage_error
        >>> target = torch.tensor([1, 10, 1e6])
        >>> preds = torch.tensor([0.9, 15, 1.2e6])
        >>> symmetric_mean_absolute_percentage_error(preds, target)
        tensor(0.2290)

    )r   r   )r   r   r   r   r   r   r   (symmetric_mean_absolute_percentage_errorD   s    r   )r   )typingr   r   r   r   Ztorchmetrics.utilities.checksr   floatintr   r   r   r   r   r   r   <module>   s    
