o
    ¹ôj»  ã                   @   sŽ   d dl mZ d dlmZmZmZ d dlZd dlmZmZ d dl	m
Z
mZ d dlmZ d dlmZ d dlmZmZ es=d	gZG d
d„ deƒZdS )é    )ÚSequence)ÚAnyÚOptionalÚUnionN)ÚTensorÚtensor)Ú_mean_absolute_error_computeÚ_mean_absolute_error_update)ÚMetric)Ú_MATPLOTLIB_AVAILABLE)Ú_AX_TYPEÚ_PLOT_OUT_TYPEúMeanAbsoluteError.plotc                       sÆ   e Zd ZU dZdZeed< dZeed< dZeed< dZ	e
ed< eed	< eed
< 	ddededdf‡ fdd„Zdededdfdd„Zdefdd„Z	ddeeeee f  dee defdd„Z‡  ZS )ÚMeanAbsoluteErrora
  `Compute Mean Absolute Error`_ (MAE).

    .. math:: \text{MAE} = \frac{1}{N}\sum_i^N | y_i - \hat{y_i} |

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

    As input to ``forward`` and ``update`` the metric accepts the following input:

    - ``preds`` (:class:`~torch.Tensor`): Predictions from model
    - ``target`` (:class:`~torch.Tensor`): Ground truth values

    As output of ``forward`` and ``compute`` the metric returns the following output:

    - ``mean_absolute_error`` (:class:`~torch.Tensor`): A tensor with the mean absolute error over the state

    Args:
        num_outputs: Number of outputs in multioutput setting
        kwargs: Additional keyword arguments, see :ref:`Metric kwargs` for more info.

    Example:
        >>> from torch import tensor
        >>> from torchmetrics.regression import MeanAbsoluteError
        >>> target = tensor([3.0, -0.5, 2.0, 7.0])
        >>> preds = tensor([2.5, 0.0, 2.0, 8.0])
        >>> mean_absolute_error = MeanAbsoluteError()
        >>> mean_absolute_error(preds, target)
        tensor(0.5000)

    Example::
        Multioutput mse computation:

        >>> from torch import tensor
        >>> from torchmetrics.regression import MeanAbsoluteError
        >>> target = tensor([[0.0, 0.0, 0.0], [0.0, 0.0, 0.0]])
        >>> preds = tensor([[1.0, 2.0, 3.0], [1.0, 2.0, 3.0]])
        >>> mean_absolute_error = MeanAbsoluteError(num_outputs=3)
        >>> mean_absolute_error(preds, target)
        tensor([1., 2., 3.])

    TÚis_differentiableFÚhigher_is_betterÚfull_state_updateg        Úplot_lower_boundÚsum_abs_errorÚtotalé   Únum_outputsÚkwargsÚreturnNc                    sf   t ƒ jdi |¤Ž t|tƒr|dkstd|› �ƒ‚|| _| jdt |¡dd� | jdt	dƒdd� d S )Nr   z6Expected num_outputs to be a positive integer but got r   Úsum)ÚdefaultÚdist_reduce_fxr   © )
ÚsuperÚ__init__Ú
isinstanceÚintÚ
ValueErrorr   Ú	add_stateÚtorchÚzerosr   )Úselfr   r   ©Ú	__class__r   ún/var/www/html/academy/tools/voices/clone/cosy-venv/lib/python3.10/site-packages/torchmetrics/regression/mae.pyr   O   s   zMeanAbsoluteError.__init__ÚpredsÚtargetc                 C   s4   t ||| jd�\}}|  j|7  _|  j|7  _dS )z*Update state with predictions and targets.)r   N)r	   r   r   r   )r&   r*   r+   r   Únum_obsr   r   r)   Úupdate]   s   zMeanAbsoluteError.updatec                 C   s   t | j| jƒS )z'Compute mean absolute error over state.)r   r   r   )r&   r   r   r)   Úcomputed   s   zMeanAbsoluteError.computeÚvalÚaxc                 C   s   |   ||¡S )a  Plot a single or multiple values from the metric.

        Args:
            val: Either a single result from calling `metric.forward` or `metric.compute` or a list of these results.
                If no value is provided, will automatically call `metric.compute` and plot that result.
            ax: An matplotlib axis object. If provided will add plot to that axis

        Returns:
            Figure and Axes object

        Raises:
            ModuleNotFoundError:
                If `matplotlib` is not installed

        .. plot::
            :scale: 75

            >>> from torch import randn
            >>> # Example plotting a single value
            >>> from torchmetrics.regression import MeanAbsoluteError
            >>> metric = MeanAbsoluteError()
            >>> metric.update(randn(10,), randn(10,))
            >>> fig_, ax_ = metric.plot()

        .. plot::
            :scale: 75

            >>> from torch import randn
            >>> # Example plotting multiple values
            >>> from torchmetrics.regression import MeanAbsoluteError
            >>> metric = MeanAbsoluteError()
            >>> values = []
            >>> for _ in range(10):
            ...     values.append(metric(randn(10,), randn(10,)))
            >>> fig, ax = metric.plot(values)

        )Ú_plot)r&   r/   r0   r   r   r)   Úploth   s   (r   )r   )NN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ÚboolÚ__annotations__r   r   r   Úfloatr   r!   r   r   r-   r.   r   r   r   r   r   r2   Ú__classcell__r   r   r'   r)   r      s4   
 )þþýüÿÿÿþr   )Úcollections.abcr   Útypingr   r   r   r$   r   r   Ú&torchmetrics.functional.regression.maer   r	   Útorchmetrics.metricr
   Útorchmetrics.utilities.importsr   Útorchmetrics.utilities.plotr   r   Ú__doctest_skip__r   r   r   r   r)   Ú<module>   s   