
    |j-x                    ^   d dl mZ d dlZd dlmZmZmZ d dlZd dl	Z	d dl	m
Z
mZ ddlmZ ddlmZmZ ddlmZ dd	lmZ erd d
lmZ d dlmZ d dl	mZ g Zd Z G d dej                  Z G d de          Z G d de          Z G d de          Z  G d de          Z!	 	 	 	 d%d&d$Z"dS )'    )annotationsN)TYPE_CHECKINGAnyLiteral)_C_ops_legacy_C_ops   )check_variable_and_dtype)_create_tensorin_pir_mode)LayerHelper)in_dynamic_mode)Sequence)Tensorc                N    t          | t          j        t          j        f          S N)
isinstancenpndarraygeneric)vars    e/lsinfo/ai/hellotax_ai/data_center/backend/venv/lib/python3.11/site-packages/paddle/metric/metrics.py
_is_numpy_r   (   s    cBJ
3444    c                      e Zd ZdZddZej        dd            Zej        dd            Zej        dd	            Z	ej        dd            Z
ddZdS )Metrica  
    Base class for metric, encapsulates metric logic and APIs
    Usage:

        .. code-block:: text

            m = SomeMetric()
            for prediction, label in ...:
                m.update(prediction, label)
            m.accumulate()

    Advanced usage for :code:`compute`:

    Metric calculation can be accelerated by calculating metric states
    from model outputs and labels by built-in operators not by Python/NumPy
    in :code:`compute`, metric states will be fetched as NumPy array and
    call :code:`update` with states in NumPy format.
    Metric calculated as follows (operations in Model and Metric are
    indicated with curly brackets, while data nodes not):

        .. code-block:: text

                 inputs & labels              || ------------------
                       |                      ||
                    {model}                   ||
                       |                      ||
                outputs & labels              ||
                       |                      ||    tensor data
                {Metric.compute}              ||
                       |                      ||
              metric states(tensor)           ||
                       |                      ||
                {fetch as numpy}              || ------------------
                       |                      ||
              metric states(numpy)            ||    numpy data
                       |                      ||
                {Metric.update}               \/ ------------------

    Examples:

        For :code:`Accuracy` metric, which takes :code:`pred` and :code:`label`
        as inputs, we can calculate the correct prediction matrix between
        :code:`pred` and :code:`label` in :code:`compute`.
        For examples, prediction results contains 10 classes, while :code:`pred`
        shape is [N, 10], :code:`label` shape is [N, 1], N is mini-batch size,
        and we only need to calculate accuracy of top-1 and top-5, we could
        calculate the correct prediction matrix of the top-5 scores of the
        prediction of each sample like follows, while the correct prediction
        matrix shape is [N, 5].

        .. code-block:: python
            :name: code-compute-example

            >>> import paddle

            >>> def compute(pred, label):
            ...     # sort prediction and slice the top-5 scores
            ...     pred = paddle.argsort(pred, descending=True)[:, :5]
            ...     # calculate whether the predictions are correct
            ...     correct = pred == label
            ...     return paddle.cast(correct, dtype='float32')
            ...

        With the :code:`compute`, we split some calculations to OPs (which
        may run on GPU devices, will be faster), and only fetch 1 tensor with
        shape as [N, 5] instead of 2 tensors with shapes as [N, 10] and [N, 1].
        :code:`update` can be define as follows:

        .. code-block:: python
            :name: code-update-example

            >>> def update(self, correct):
            ...     accs = []
            ...     for i, k in enumerate(self.topk):
            ...         num_corrects = correct[:, :k].sum()
            ...         num_samples = len(correct)
            ...         accs.append(float(num_corrects) / num_samples)
            ...         self.total[i] += num_corrects
            ...         self.count[i] += num_samples
            ...     return accs
    returnNonec                    d S r    selfs    r   __init__zMetric.__init__   s    r   c                <    t          d| j        j         d          ))
        Reset states and result
        z$function 'reset' not implemented in .NotImplementedError	__class____name__r!   s    r   resetzMetric.reset   s)    
 "M4>3JMMM
 
 	
r   argsr   c                <    t          d| j        j         d          )au  
        Update states for metric

        Inputs of :code:`update` is the outputs of :code:`Metric.compute`,
        if :code:`compute` is not defined, the inputs of :code:`update`
        will be flatten arguments of **output** of mode and **label** from data:
        :code:`update(output1, output2, ..., label1, label2,...)`

        see :code:`Metric.compute`
        z%function 'update' not implemented in r&   r'   r"   r,   s     r   updatezMetric.update   s)     "NDN4KNNN
 
 	
r   c                <    t          d| j        j         d          )zO
        Accumulates statistics, computes and returns the metric value
        z)function 'accumulate' not implemented in r&   r'   r!   s    r   
accumulatezMetric.accumulate   s)    
 "R8ORRR
 
 	
r   strc                <    t          d| j        j         d          )%
        Returns metric name
        z#function 'name' not implemented in r&   r'   r!   s    r   namezMetric.name   s)    
 "L$.2ILLL
 
 	
r   c                    |S )aM  
        This API is advanced usage to accelerate metric calculating, calculations
        from outputs of model to the states which should be updated by Metric can
        be defined here, where Paddle OPs is also supported. Outputs of this API
        will be the inputs of "Metric.update".

        If :code:`compute` is defined, it will be called with **outputs**
        of model and **labels** from data as arguments, all outputs and labels
        will be concatenated and flatten and each filed as a separate argument
        as follows:
        :code:`compute(output1, output2, ..., label1, label2,...)`

        If :code:`compute` is not defined, default behaviour is to pass
        input to output, so output format will be:
        :code:`return output1, output2, ..., label1, label2,...`

        see :code:`Metric.update`
        r    r.   s     r   computezMetric.compute   s	    & r   Nr   r   )r,   r   r   r   )r   r   r   r2   )r,   r   r   r   )r*   
__module____qualname____doc__r#   abcabstractmethodr+   r/   r1   r5   r7   r    r   r   r   r   ,   s        P Pd    	
 
 
 
 	
 
 
 
 	
 
 
 
 	
 
 
 
     r   r   )	metaclassc                  p     e Zd ZU dZded<   ded<   	 	 dd fdZddZddZd dZd!dZ	d"dZ
d#dZ xZS )$Accuracya  
    Encapsulates accuracy metric logic.

    Args:
        topk (list[int]|tuple[int]): Number of top elements to look at
            for computing accuracy. Default is (1,).
        name (str|None, optional): String name of the metric instance. Default
            is `acc`.

    Examples:
        .. code-block:: python
            :name: code-standalone-example

            >>> import numpy as np
            >>> import paddle

            >>> x = paddle.to_tensor(np.array([
            ...     [0.1, 0.2, 0.3, 0.4],
            ...     [0.1, 0.4, 0.3, 0.2],
            ...     [0.1, 0.2, 0.4, 0.3],
            ...     [0.1, 0.2, 0.3, 0.4]]))
            >>> y = paddle.to_tensor(np.array([[0], [1], [2], [3]]))

            >>> m = paddle.metric.Accuracy()
            >>> correct = m.compute(x, y)
            >>> m.update(correct)
            >>> res = m.accumulate()
            >>> print(res)
            0.75

        .. code-block:: python
            :name: code-model-api-example

            >>> # doctest: +TIMEOUT(80)
            >>> import paddle
            >>> from paddle.static import InputSpec
            >>> import paddle.vision.transforms as T
            >>> from paddle.vision.datasets import MNIST

            >>> input = InputSpec([None, 1, 28, 28], 'float32', 'image')
            >>> label = InputSpec([None, 1], 'int64', 'label')
            >>> transform = T.Compose([T.Transpose(), T.Normalize([127.5], [127.5])])
            >>> train_dataset = MNIST(mode='train', transform=transform)

            >>> model = paddle.Model(paddle.vision.models.LeNet(), input, label)
            >>> optim = paddle.optimizer.Adam(
            ...     learning_rate=0.001, parameters=model.parameters())
            >>> model.prepare(
            ...     optim,
            ...     loss=paddle.nn.CrossEntropyLoss(),
            ...     metrics=paddle.metric.Accuracy())
            ...
            >>> model.fit(train_dataset, batch_size=64)

    Sequence[int]topkintmaxk   Nr5   
str | Noner,   r   kwargsr   r   c                     t                      j        |i | || _        t          |          | _        |                     |           |                                  d S r   )superr#   rC   maxrE   
_init_namer+   )r"   rC   r5   r,   rI   r)   s        r   r#   zAccuracy.__init__   sX     	$)&)))	II	

r   predr   labelc                   t          j        |d          }t          j        |t          |j                  dz
  gdg| j        g          }t          |j                  dk    s)t          |j                  dk    r'|j        d         dk    rt          j        |d          }n(|j        d         dk    rt          j        |dd	          }||                    |j	                  k    }t          j
        |d
          S )a  
        Compute the top-k (maximum value in `topk`) indices.

        Args:
            pred (Tensor): The predicted value is a Tensor with dtype
                float32 or float64. Shape is [batch_size, d0, ..., dN].
            label (Tensor): The ground truth value is Tensor with dtype
                int64. Shape is [batch_size, d0, ..., 1], or
                [batch_size, d0, ..., num_classes] in one hot representation.

        Return:
            Tensor: Correct mask, a tensor with shape [batch_size, d0, ..., topk].
        T)
descendingrG   r   )axesstartsendsr	   )rU   rG   )axiskeepdimfloat32dtype)paddleargsortslicelenshaperE   reshapeargmaxastyperZ   cast)r"   rN   rO   r,   corrects        r   r7   zAccuracy.compute  s     ~dt444|DJ!+,aS	{
 
 
 !!!!ek"o&:&:
 N5'22EE[_!!M%b$???E%,,tz222{7)4444r   rd   c                   t          |t          j                  rt          j        |          }t          j        t          j        |j        dd                             }g }t          | j                  D ]r\  }}|dd|f         	                                }|
                    t          |          |z             | j        |xx         |z  cc<   | j        |xx         |z  cc<   st          | j                  dk    r|d         n|}|S )aw  
        Update the metrics states (correct count and total count), in order to
        calculate cumulative accuracy of all instances. This function also
        returns the accuracy of current step.

        Args:
            correct: Correct mask, a tensor with shape [batch_size, d0, ..., topk].

        Return:
            Tensor: the accuracy of current step.
        NrU   .rG   r   )r   r[   r   r   arrayprodr_   	enumeraterC   sumappendfloattotalcountr^   )r"   rd   r,   num_samplesaccsiknum_correctss           r   r/   zAccuracy.update+  s     gv}-- 	(hw''Ggbhw}SbS'9::;;di(( 	) 	)DAq"37+//11LKKl++k9:::JqMMM\)MMMJqMMM[(MMMMdi..A--tAww4r   c                z    dgt          | j                  z  | _        dgt          | j                  z  | _        dS )1
        Resets all of the metric state.
                r   N)r^   rC   rl   rm   r!   s    r   r+   zAccuracy.resetC  s4     US^^+
S3ty>>)


r   list[float]c                    g }t          | j        | j                  D ]4\  }}|dk    rt          |          |z  nd}|                    |           5t          | j                  dk    r|d         n|}|S )z>
        Computes and returns the accumulated metric.
        r   ru   rG   )ziprl   rm   rk   rj   r^   rC   )r"   restcrs        r   r1   zAccuracy.accumulateJ  sz     
DJ// 	 	DAq !Aa13AJJqMMMMDI!++c!ff
r   c                j    pd| j         dk    rfd| j        D             | _        d S g| _        d S )NaccrG   c                    g | ]	} d | 
S )_topr    ).0rq   r5   s     r   
<listcomp>z'Accuracy._init_name.<locals>.<listcomp>X  s'    >>>qT**q**>>>r   )rE   rC   _name)r"   r5   s    `r   rM   zAccuracy._init_nameU  sE    }u9>>>>>>DI>>>DJJJDJJJr   	list[str]c                    | j         S )z1
        Return name of metric instance.
        r   r!   s    r   r5   zAccuracy.name\       zr   )rF   N)
rC   rB   r5   rH   r,   r   rI   r   r   r   )rN   r   rO   r   r,   r   r   r   )rd   r   r,   r   r   r   r8   )r   rv   )r5   rH   r   r   )r   r   )r*   r:   r;   r<   __annotations__r#   r7   r/   r+   r1   rM   r5   __classcell__r)   s   @r   rA   rA      s         6 6p III #      5 5 5 5>   0* * * *	 	 	 	              r   rA   c                  ^     e Zd ZU dZded<   ded<   	 dd fdZddZddZddZddZ	 xZ
S )	Precisiona7  
    Precision (also called positive predictive value) is the fraction of
    relevant instances among the retrieved instances. Refer to
    https://en.wikipedia.org/wiki/Evaluation_of_binary_classifiers

    Noted that this class manages the precision score only for binary
    classification task.

    Args:
        name (str, optional): String name of the metric instance.
            Default is `precision`.

    Examples:
        .. code-block:: python
            :name: code-standalone-example

            >>> import numpy as np
            >>> import paddle

            >>> x = np.array([0.1, 0.5, 0.6, 0.7])
            >>> y = np.array([0, 1, 1, 1])

            >>> m = paddle.metric.Precision()
            >>> m.update(x, y)
            >>> res = m.accumulate()
            >>> print(res)
            1.0

        .. code-block:: python
            :name: code-model-api-example

            >>> import numpy as np

            >>> import paddle
            >>> import paddle.nn as nn

            >>> class Data(paddle.io.Dataset): # type: ignore[type-arg]
            ...     def __init__(self):
            ...         super().__init__()
            ...         self.n = 1024
            ...         self.x = np.random.randn(self.n, 10).astype('float32')
            ...         self.y = np.random.randint(2, size=(self.n, 1)).astype('float32')
            ...
            ...     def __getitem__(self, idx):
            ...         return self.x[idx], self.y[idx]
            ...
            ...     def __len__(self):
            ...         return self.n
            ...
            >>> model = paddle.Model(nn.Sequential(
            ...     nn.Linear(10, 1),
            ...     nn.Sigmoid()
            ... ))
            >>> optim = paddle.optimizer.Adam(
            ...     learning_rate=0.001, parameters=model.parameters())
            >>> model.prepare(
            ...     optim,
            ...     loss=nn.BCELoss(),
            ...     metrics=paddle.metric.Precision())
            ...
            >>> data = Data()
            >>> model.fit(data, batch_size=16)
    rD   tpfp	precisionr5   r2   r,   r   rI   r   r   c                d     t                      j        |i | d| _        d| _        || _        d S Nr   )rK   r#   r   r   r   r"   r5   r,   rI   r)   s       r   r#   zPrecision.__init__  s:     	$)&)))


r   preds-npt.NDArray[np.float32 | np.float64] | Tensorlabels)npt.NDArray[np.int32 | np.int64] | Tensorc                F   t          |t          j                  rt          j        |          }nt          |          st          d          t          |t          j                  rt          j        |          }nt          |          st          d          |j        d         }t          j        |dz             	                    d          }t          |          D ]?}||         }||         }|dk    r'||k    r| xj        dz  c_        /| xj        dz  c_        @dS )a  
        Update the states based on the current mini-batch prediction results.

        Args:
            preds (numpy.ndarray): The prediction result, usually the output
                of two-class sigmoid function. It should be a vector (column
                vector or row vector) with data type: 'float64' or 'float32'.
            labels (numpy.ndarray): The ground truth (labels),
                the shape should keep the same as preds.
                The data type is 'int32' or 'int64'.
        .The 'preds' must be a numpy ndarray or Tensor./The 'labels' must be a numpy ndarray or Tensor.r   g      ?int32rG   N)r   r[   r   r   rf   r   
ValueErrorr_   floorrb   ranger   r   r"   r   r   
sample_numrp   rN   rO   s          r   r/   zPrecision.update  s      eV]++ 	OHUOOEEE"" 	OMNNNffm,, 	PXf%%FFF## 	PNOOO\!_
%%,,W55z"" 	! 	!A8D1IEqyy5==GGqLGGGGGqLGG	! 	!r   c                "    d| _         d| _        dS rt   r   N)r   r   r!   s    r   r+   zPrecision.reset       r   rk   c                ^    | j         | j        z   }|dk    rt          | j                   |z  ndS )z
        Calculate the final precision.

        Returns:
            A scaler float: results of the calculated precision.
        r   ru   )r   r   rk   )r"   aps     r   r1   zPrecision.accumulate  s2     Wtw&(AgguTW~~""36r   c                    | j         S r4   r   r!   s    r   r5   zPrecision.name  r   r   )r   r5   r2   r,   r   rI   r   r   r   r   r   r   r   r   r   r8   r   rk   r9   )r*   r:   r;   r<   r   r#   r/   r+   r1   r5   r   r   s   @r   r   r   c  s         > >@ GGGGGG &      $! $! $! $!L   7 7 7 7       r   r   c                  \     e Zd ZU dZded<   ded<   dd fdZddZddZddZddZ	 xZ
S )Recallal  
    Recall (also known as sensitivity) is the fraction of
    relevant instances that have been retrieved over the
    total amount of relevant instances

    Refer to:
    https://en.wikipedia.org/wiki/Precision_and_recall

    Noted that this class manages the recall score only for
    binary classification task.

    Args:
        name (str, optional): String name of the metric instance.
            Default is `recall`.

    Examples:
        .. code-block:: python
            :name: code-standalone-example

            >>> import numpy as np
            >>> import paddle

            >>> x = np.array([0.1, 0.5, 0.6, 0.7])
            >>> y = np.array([1, 0, 1, 1])

            >>> m = paddle.metric.Recall()
            >>> m.update(x, y)
            >>> res = m.accumulate()
            >>> print(res)
            0.6666666666666666

        .. code-block:: python
            :name: code-model-api-example

            >>> import numpy as np

            >>> import paddle
            >>> import paddle.nn as nn

            >>> class Data(paddle.io.Dataset): # type: ignore[type-arg]
            ...     def __init__(self):
            ...         super().__init__()
            ...         self.n = 1024
            ...         self.x = np.random.randn(self.n, 10).astype('float32')
            ...         self.y = np.random.randint(2, size=(self.n, 1)).astype('float32')
            ...
            ...     def __getitem__(self, idx):
            ...         return self.x[idx], self.y[idx]
            ...
            ...     def __len__(self):
            ...         return self.n
            ...
            >>> model = paddle.Model(nn.Sequential(
            ...     nn.Linear(10, 1),
            ...     nn.Sigmoid()
            ... ))
            >>> optim = paddle.optimizer.Adam(
            ...     learning_rate=0.001, parameters=model.parameters())
            >>> model.prepare(
            ...     optim,
            ...     loss=nn.BCELoss(),
            ...     metrics=[paddle.metric.Precision(), paddle.metric.Recall()])
            ...
            >>> data = Data()
            >>> model.fit(data, batch_size=16)
    rD   r   fnrecallr5   r2   r,   r   rI   r   r   c                d     t                      j        |i | d| _        d| _        || _        d S r   )rK   r#   r   r   r   r   s       r   r#   zRecall.__init__4  s8    $)&)))


r   r   r   r   r   c                @   t          |t          j                  rt          j        |          }nt          |          st          d          t          |t          j                  rt          j        |          }nt          |          st          d          |j        d         }t          j        |          	                    d          }t          |          D ]?}||         }||         }|dk    r'||k    r| xj        dz  c_        /| xj        dz  c_        @dS )a  
        Update the states based on the current mini-batch prediction results.

        Args:
            preds(numpy.array): prediction results of current mini-batch,
                the output of two-class sigmoid function.
                Shape: [batch_size, 1]. Dtype: 'float64' or 'float32'.
            labels(numpy.array): ground truth (labels) of current mini-batch,
                the shape should keep the same as preds.
                Shape: [batch_size, 1], Dtype: 'int32' or 'int64'.
        r   r   r   r   rG   N)r   r[   r   r   rf   r   r   r_   rintrb   r   r   r   r   s          r   r/   zRecall.update:  s     eV]++ 	OHUOOEEE"" 	OMNNNffm,, 	PXf%%FFF## 	PNOOO\!_
%%g..z"" 	! 	!A8D1IEzz5==GGqLGGGGGqLGG	! 	!r   rk   c                ^    | j         | j        z   }|dk    rt          | j                   |z  ndS )z}
        Calculate the final recall.

        Returns:
            A scaler float: results of the calculated Recall.
        r   ru   )r   r   rk   )r"   r   s     r   r1   zRecall.accumulate`  s2     47"*0A++uTW~~&&3>r   c                "    d| _         d| _        dS r   )r   r   r!   s    r   r+   zRecall.resetj  r   r   c                    | j         S r   r   r!   s    r   r5   zRecall.nameq  r   r   )r   r   r   r   r8   r9   )r*   r:   r;   r<   r   r#   r/   r1   r+   r5   r   r   s   @r   r   r     s         A AF GGGGGG      $! $! $! $!L? ? ? ?          r   r   c                  d     e Zd ZdZ	 	 	 dd  fdZd!dZed"d            Zd#dZd$dZ	d%dZ
 xZS )&AucaD  
    The auc metric is for binary classification.
    Refer to https://en.wikipedia.org/wiki/Receiver_operating_characteristic#Area_under_the_curve.
    Please notice that the auc metric is implemented with python, which may be a little bit slow.

    The `auc` function creates four local variables, `true_positives`,
    `true_negatives`, `false_positives` and `false_negatives` that are used to
    compute the AUC. To discretize the AUC curve, a linearly spaced set of
    thresholds is used to compute pairs of recall and precision values. The area
    under the ROC-curve is therefore computed using the height of the recall
    values by the false positive rate, while the area under the PR-curve is the
    computed using the height of the precision values by the recall.

    Args:
        curve (str): Specifies the mode of the curve to be computed,
            'ROC' or 'PR' for the Precision-Recall-curve. Default is 'ROC'.
        num_thresholds (int): The number of thresholds to use when
            discretizing the roc curve. Default is 4095.
        name (str, optional): String name of the metric instance. Default
            is `auc`.

    "NOTE: only implement the ROC curve type via Python now."

    Examples:
        .. code-block:: python
            :name: code-standalone-example

            >>> import numpy as np
            >>> import paddle

            >>> m = paddle.metric.Auc()

            >>> n = 8
            >>> class0_preds = np.random.random(size = (n, 1))
            >>> class1_preds = 1 - class0_preds

            >>> preds = np.concatenate((class0_preds, class1_preds), axis=1)
            >>> labels = np.random.randint(2, size = (n, 1))

            >>> m.update(preds=preds, labels=labels)
            >>> res = m.accumulate()

        .. code-block:: python
            :name: code-model-api-example

            >>> import numpy as np
            >>> import paddle
            >>> import paddle.nn as nn

            >>> class Data(paddle.io.Dataset): # type: ignore[type-arg]
            ...     def __init__(self):
            ...         super().__init__()
            ...         self.n = 1024
            ...         self.x = np.random.randn(self.n, 10).astype('float32')
            ...         self.y = np.random.randint(2, size=(self.n, 1)).astype('int64')
            ...
            ...     def __getitem__(self, idx):
            ...         return self.x[idx], self.y[idx]
            ...
            ...     def __len__(self):
            ...         return self.n
            ...
            >>> model = paddle.Model(nn.Sequential(
            ...     nn.Linear(10, 2), nn.Softmax())
            ... )
            >>> optim = paddle.optimizer.Adam(
            ...     learning_rate=0.001, parameters=model.parameters())
            ...
            >>> def loss(x, y):
            ...     return nn.functional.nll_loss(paddle.log(x), y)
            ...
            >>> model.prepare(
            ...     optim,
            ...     loss=loss,
            ...     metrics=paddle.metric.Auc())
            >>> data = Data()
            >>> model.fit(data, batch_size=16)
    ROC  auccurveLiteral['ROC', 'PR']num_thresholdsrD   r5   r2   r,   r   rI   r   r   c                     t                      j        |i | || _        || _        |dz   }t	          j        |          | _        t	          j        |          | _        || _        d S )NrG   )	rK   r#   _curve_num_thresholdsr   zeros	_stat_pos	_stat_negr   )r"   r   r   r5   r,   rI   _num_pred_bucketsr)   s          r   r#   zAuc.__init__  sj     	$)&)))-*Q."344"344


r   r   r   r   r   c                   t          |t          j                  rt          j        |          }nt          |          st          d          t          |t          j                  rt          j        |          }nt          |          st          d          t          |          D ]`\  }}||df         }t          || j	        z            }|| j	        k    sJ |r| j
        |xx         dz  cc<   K| j        |xx         dz  cc<   adS )a  
        Update the auc curve with the given predictions and labels.

        Args:
            preds (numpy.array): An numpy array in the shape of
                (batch_size, 2), preds[i][j] denotes the probability of
                classifying the instance i into the class j.
            labels (numpy.array): an numpy array in the shape of
                (batch_size, 1), labels[i] is either o or 1,
                representing the label of the instance i.
        r   r   rG   g      ?N)r   r[   r   r   rf   r   r   rh   rD   r   r   r   )r"   r   r   rp   lblvaluebin_idxs          r   r/   z
Auc.update  s3     ffm,, 	PXf%%FFF## 	PNOOOeV]++ 	OHUOOEEE"" 	OMNNN'' 	/ 	/FAs!Q$KE%$"6677Gd22222 /w'''3.''''w'''3.''''	/ 	/r   x1rk   x2y1y2c                8    t          | |z
            ||z   z  dz  S )Ng       @)abs)r   r   r   r   s       r   trapezoid_areazAuc.trapezoid_area  s     27||rBw'#--r   c                    d}d}d}| j         }|dk    rJ|}|}|| j        |         z  }|| j        |         z  }||                     ||||          z  }|dz  }|dk    J|dk    r|dk    r||z  |z  ndS )z~
        Return the area (a float score) under auc curve

        Return:
            float: the area under auc curve
        ru   r   rG   )r   r   r   r   )r"   tot_postot_negr   idxtot_pos_prevtot_neg_prevs          r   r1   zAuc.accumulate   s     "Qhh"L"Lt~c**Gt~c**G4&&w  C 1HC Qhh (/}}3C'MG##C	
r   c                ~    | j         dz   }t          j        |          | _        t          j        |          | _        dS )r%   rG   N)r   r   r   r   r   )r"   r   s     r   r+   z	Auc.reset  s:     !014"344"344r   c                    | j         S r   r   r!   s    r   r5   zAuc.name"  r   r   )r   r   r   )r   r   r   rD   r5   r2   r,   r   rI   r   r   r   r   )
r   rk   r   rk   r   rk   r   rk   r   rk   r   r8   r9   )r*   r:   r;   r<   r#   r/   staticmethodr   r1   r+   r5   r   r   s   @r   r   r   x  s        M Mb ',"	      "!/ !/ !/ !/F . . . \.
 
 
 
45 5 5 5       r   r   rG   inputr   rO   rq   rD   rd   Tensor | Nonerl   r5   rH   r   c                (   |j         t          j        k    rt          j        |t          j                  }t                      r[|t          d          }|t          d          }t          j        | |          \  }}t          j	        |||||          \  }}	}	|S t                      r5t          j        | |          \  }}t          j	        |||          \  }}	}	|S t          di t                      }
t          | dg dd           t          j        | |          \  }}|
                    d          }||
                    d          }||
                    d          }|
                    d|g|g|gd	|g|g|gd
           |S )a  
    accuracy layer.
    Refer to the https://en.wikipedia.org/wiki/Precision_and_recall

    This function computes the accuracy using the input and label.
    If the correct label occurs in top k predictions, then correct will increment by one.
    Note: the dtype of accuracy is determined by input. the input and label dtype can be different.

    Args:
        input(Tensor): The input of accuracy layer, which is the predictions of network. A Tensor with type float32,float64.
            The shape is ``[sample_number, class_dim]`` .
        label(Tensor): The label of dataset. Tensor with type int64 or int32. The shape is ``[sample_number, 1]`` .
        k(int, optional): The top k predictions for each class will be checked. Data type is int64 or int32.
        correct(Tensor, optional): The correct predictions count. A Tensor with type int64 or int32.
        total(Tensor, optional): The total entries count. A tensor with type int64 or int32.
        name(str|None, optional): The default value is None. Normally there is no need for
            user to set this property. For more information, please refer to :ref:`api_guide_Name`

    Returns:
        Tensor, the correct rate. A Tensor with type float32.

    Examples:
        .. code-block:: python

            >>> import paddle

            >>> predictions = paddle.to_tensor([[0.2, 0.1, 0.4, 0.1, 0.1], [0.2, 0.3, 0.1, 0.15, 0.25]], dtype='float32')
            >>> label = paddle.to_tensor([[2], [0]], dtype="int64")
            >>> result = paddle.metric.accuracy(input=predictions, label=label, k=1)
            >>> print(result)
            Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
            0.50000000)
    Nr   rY   )rq   accuracyr   )float16uint16rX   float64rX   )OutIndicesLabel)rA   CorrectTotal)typeinputsoutputs)r   )rZ   r[   r   rc   int64r   r   rC   r   r   r   r   r   localsr
   "create_variable_for_type_inference	append_op)r   rO   rq   rd   rl   r5   topk_outtopk_indices_acc_helperacc_outs               r   r   r   )  s   R {fl""E6<00 ?$7333G="111E!'Ua!8!8!8,"+lE7E
 

a 	 !'Ua!8!8!8,_X|UCC
a00vxx00FwCCCZ   $[!444Hl77i7HHG;;';JJ}999HH
 z|nwOO 	yW
 
     Nr   )rG   NNN)r   r   rO   r   rq   rD   rd   r   rl   r   r5   rH   r   r   )#
__future__r   r=   typingr   r   r   numpyr   r[   r   r   base.data_feederr
   base.frameworkr   r   base.layer_helperr   	frameworkr   collections.abcr   numpy.typingnptr   __all__r   ABCMetar   rA   r   r   r   r   r    r   r   <module>r      sN   # " " " " " 



 . . . . . . . . . .      ( ( ( ( ( ( ( ( 7 7 7 7 7 7 8 8 8 8 8 8 8 8 + + + + + + ' ' ' ' ' ' (((((( 5 5 5T T T T Ts{ T T T Tn] ] ] ] ]v ] ] ]@G G G G G G G GTH H H H HV H H HVn n n n n& n n nh !O O O O O O Or   