a
    dw
                     @   sr   d dl 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d	d
dZeeedddZ	dS )    )TupleN)Tensor)_check_same_shape)predstargetreturnc                 C   s.   t | | | |   }|  }||fS )zUpdate and returns variables required to compute Weighted Absolute Percentage Error.

    Check for same shape of input tensors.

    Args:
        preds: Predicted tensor
        target: Ground truth tensor

    )r   abssumr   r   sum_abs_error	sum_scale r   q/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/torchmetrics/functional/regression/wmape.py/_weighted_mean_absolute_percentage_error_update   s    
r   -`>)r   r   epsilonr   c                 C   s   | t j||d S )zCompute Weighted Absolute Percentage Error.

    Args:
        sum_abs_error: scalar with sum of absolute errors
        sum_scale: scalar with sum of target values
        epsilon: small float to prevent division by zero

    )min)torchclamp)r   r   r   r   r   r   0_weighted_mean_absolute_percentage_error_compute+   s    r   c                 C   s   t | |\}}t||S )a  Compute weighted mean absolute percentage error (`WMAPE`_).

    The output of WMAPE metric is a non-negative floating point, where the optimal value is 0. It is computes as:

    .. math::
        \text{WMAPE} = \frac{\sum_{t=1}^n | y_t - \hat{y}_t | }{\sum_{t=1}^n |y_t| }

    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 WMAPE.

    Example:
        >>> import torch
        >>> _ = torch.manual_seed(42)
        >>> preds = torch.randn(20,)
        >>> target = torch.randn(20,)
        >>> weighted_mean_absolute_percentage_error(preds, target)
        tensor(1.3967)

    )r   r   r
   r   r   r   'weighted_mean_absolute_percentage_error;   s    r   )r   )
typingr   r   r   Ztorchmetrics.utilities.checksr   r   floatr   r   r   r   r   r   <module>   s   
 