o
    MeÇ<  ã                   @   s€   d Z ddlZddlZddlmZ ddlZg Zddd„Zddd„Z	ddd	„Z
dd
d„Zddd„Zddd„Zddd„Zddd„ZdS )zFleet Metricsé    N)ÚVariablec                 C   ó˜   |du r	t j ¡ }|du rt jjj}t| tƒr#t 	| 
| j¡ ¡ ¡} nt| tƒr2t 	| 
| ¡ ¡ ¡} t 	| j¡}t | ¡d }| | d¡}| |¡}|S )a  
    distributed sum in fleet

    Args:
        input(numpy.array|Variable|string): output of a layer
        scope(Scope): specific scope

    Returns:
        global_metric(numpy.array): sum array

    Example:
        .. code-block:: python

          # in model.py
          input = fluid.layers.cast(some_input, dtype='float32')
          cnt = fluid.layers.reduce_sum(input)
          global_cnt = fluid.layers.create_global_var(persistable=True, dtype='float32', shape=[1], value=0)
          tmp = fluid.layers.elementwise_add(cnt, global_cnt)
          fluid.layers.assign(tmp, global_cnt)
          
          # in train.py, after train or infer
          res = np.array(scope.find_var(global_cnt.name).get_tensor())
          print("sum array: ", paddle.distributed.fleet.sum(res))
    Nr   Úsum©ÚpaddleÚstaticÚglobal_scopeÚdistributedÚfleetÚutilÚ
isinstancer   ÚnpÚarrayÚfind_varÚnameÚ
get_tensorÚstrÚshapeÚcopyÚ
all_reduceÚreshape©ÚinputÚscoper   Z	old_shapeÚoutput© r   úWD:\Projects\ConvertPro\env\Lib\site-packages\paddle/distributed/fleet/metrics/metric.pyr      ó   




r   c                 C   r   )a  
    distributed max in fleet

    Args:
        input(numpy.array|Variable|string): output of a layer
        scope(Scope): specific scope

    Returns:
        global_metric(numpy.array): max array

    Example:
        .. code-block:: python

          # in model.py
          input = fluid.layers.cast(some_input, dtype='float32')
          cnt = fluid.layers.reduce_sum(input)
          global_cnt = fluid.layers.create_global_var(persistable=True, dtype='float32', shape=[1], value=0)
          tmp = fluid.layers.elementwise_max(cnt, global_cnt)
          fluid.layers.assign(tmp, global_cnt)

          # in train.py, after train or infer
          res = np.array(scope.find_var(global_cnt.name).get_tensor())
          print("max array: ", paddle.distributed.fleet.max(res))
    Nr   Úmaxr   r   r   r   r   r   @   r   r   c                 C   r   )a  
    distributed min in fleet

    Args:
        input(numpy.array|Variable|string): output of a layer
        scope(Scope): specific scope

    Returns:
        global_metric(numpy.array): min array

    Example:
        .. code-block:: python

          # in model.py
          input = fluid.layers.cast(some_input, dtype='float32')
          cnt = fluid.layers.reduce_sum(input)
          global_cnt = fluid.layers.create_global_var(persistable=True, dtype='float32', shape=[1], value=0)
          tmp = fluid.layers.elementwise_min(cnt, global_cnt)
          fluid.layers.assign(tmp, global_cnt)

          # in train.py, after train or infer
          res = np.array(scope.find_var(global_cnt.name).get_tensor())
          print("min array: ", paddle.distributed.fleet.min(res))
    Nr   Úminr   r   r   r   r   r   h   r   r   c                 C   sæ  |du r	t j ¡ }|du rt jjj}t| tƒr#t 	| 
| j¡ ¡ ¡} nt| tƒr2t 	| 
| ¡ ¡ ¡} t|tƒrCt 	| 
|j¡ ¡ ¡}nt|tƒrRt 	| 
|¡ ¡ ¡}t 	| j¡}|  d¡} t | ¡d }| | d¡}| |¡}t 	|j¡}| d¡}t |¡d }| |d¡}| |¡}t|d ƒ}d}	d}
d}d}d}d}t|ƒD ]8}|d | }|
|d |  }||d | 7 }||d |  }||d | 7 }|	|| |
|  d 7 }	|}
|}q¢d}|
| dksç|dkrëd}|S |	|
|  }|S )	a=  
    distributed auc in fleet

    Args:
        stat_pos(numpy.array|Variable|string): stat_pos in output of fluid.layers.auc
        stat_neg(numpy.array|Variable|string): stat_neg in output of fluid.layers.auc
        scope(Scope): specific scope

    Returns:
        auc_value(float): auc value

    Example:
        .. code-block:: python

          # in model.py
          similarity_norm = fluid.layers.sigmoid(fluid.layers.clip(output, min=-15.0, max=15.0))
          binary_predict = fluid.layers.concat(
              input=[fluid.layers.elementwise_sub(fluid.layers.ceil(similarity_norm), similarity_norm), similarity_norm], axis=1)
          self.auc, batch_auc, [batch_stat_pos, batch_stat_neg, stat_pos, stat_neg] =
              fluid.layers.auc(input=binary_predict, label=label, curve='ROC', num_thresholds=4096)

          # in train.py, after train or infer
          pos = np.array(scope.find_var(stat_pos.name).get_tensor())
          neg = np.array(scope.find_var(stat_neg.name).get_tensor())
          print("auc: ", paddle.distributed.fleet.auc(pos, neg))
    Néÿÿÿÿr   r   g        é   é   g      à?)r   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   ÚlenÚrange)Zstat_posZstat_negr   r   Zold_pos_shapeZ
global_posZold_neg_shapeZ
global_negZ
num_bucketZareaÚposÚnegÚnew_posZnew_negÚtotal_ins_numÚiÚindexZ	auc_valuer   r   r   Úauc   sX   









þr+   c                 C   ó  |du r	t j ¡ }|du rt jjj}t| tƒr#t 	| 
| j¡ ¡ ¡} nt| tƒr2t 	| 
| ¡ ¡ ¡} t|tƒrCt 	| 
|j¡ ¡ ¡}nt|tƒrRt 	| 
|¡ ¡ ¡}t 	| j¡}|  d¡} t | ¡d }| | d¡}| |¡}| |d¡}t|d ƒt|d ƒ }|S )aÁ  
    distributed mae in fleet

    Args:
        abserr(numpy.array|Variable|string): abserr in output of fluid.contrib.layers.ctr_metric_bundle
        total_ins_num(numpy.array|Variable|string): total variable
        scope(Scope): specific scope

    Returns:
        mae(float): mae value

    Example:
        .. code-block:: python

          # in model.py
          sqrerr, abserr, prob, q, pos, total = fluid.contrib.layers.ctr_metric_bundle(similarity_norm, fluid.layers.cast(x=label, dtype='float32'))

          # in train.py, after train or infer
          res = np.array(scope.find_var(abserr.name).get_tensor())
          print("mae: ", paddle.distributed.fleet.mae(res, total_ins_num))
    Nr    r   r   ©r   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   Úfloat)Zabserrr(   r   r   Úold_metric_shapeÚglobal_metricÚglobal_total_numZ	mae_valuer   r   r   Úmaeã   s,   




ÿ


r2   c                 C   s  |du r	t j ¡ }|du rt jjj}t| tƒr#t 	| 
| j¡ ¡ ¡} nt| tƒr2t 	| 
| ¡ ¡ ¡} t|tƒrCt 	| 
|j¡ ¡ ¡}nt|tƒrRt 	| 
|¡ ¡ ¡}t 	| j¡}|  d¡} t | ¡d }| | d¡}| |¡}| |d¡}t t|d ƒt|d ƒ ¡}|S )aÆ  
    distributed rmse in fleet

    Args:
        sqrerr(numpy.array|Variable|string): sqrerr in output of fluid.contrib.layers.ctr_metric_bundle
        total_ins_num(numpy.array|Variable|string): total variable
        scope(Scope): specific scope

    Returns:
        rmse(float): rmse value

    Example:
        .. code-block:: python

          # in model.py
          sqrerr, abserr, prob, q, pos, total = fluid.contrib.layers.ctr_metric_bundle(similarity_norm, fluid.layers.cast(x=label, dtype='float32'))

          # in train.py, after train or infer
          res = np.array(scope.find_var(sqrerr.name).get_tensor())
          print("rmse: ", paddle.distributed.fleet.rmse(res, total_ins_num))
    Nr    r   r   )r   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   ÚmathÚsqrtr.   )Úsqrerrr(   r   r   r/   r0   r1   Z
rmse_valuer   r   r   Úrmse  s,   




ÿ


r6   c                 C   r,   )aÇ  
    distributed mse in fleet

    Args:
        sqrerr(numpy.array|Variable|string): sqrerr in output of fluid.contrib.layers.ctr_metric_bundle
        total_ins_num(numpy.array|Variable|string): total variable
        scope(Scope): specific scope

    Returns:
        mse(float): mse value

    Example:
        .. code-block:: python

          # in model.py
          sqrerr, abserr, prob, q, pos, total = fluid.contrib.layers.ctr_metric_bundle(similarity_norm, fluid.layers.cast(x=label, dtype='float32'))

          # in train.py, after train or infer
          metric = np.array(scope.find_var(sqrerr.name).get_tensor())
          print("mse: ", paddle.distributed.fleet.mse(metric, total_ins_num))
    Nr    r   r   r-   )r5   r(   r   r   r/   r0   r1   Z	mse_valuer   r   r   ÚmseE  s,   




ÿ


r7   c                 C   sð   |du r	t j ¡ }|du rt jjj}t| tƒr#t 	| 
| j¡ ¡ ¡} nt| tƒr2t 	| 
| ¡ ¡ ¡} t|tƒrCt 	| 
|j¡ ¡ ¡}nt|tƒrRt 	| 
|¡ ¡ ¡}t | ¡d }t |¡d }| | d¡}| |d¡}t|d ƒt|d ƒ S )a,  
    distributed accuracy in fleet

    Args:
        correct(numpy.array|Variable|string): correct Variable
        total(numpy.array|Variable): total Variable
        scope(Scope): specific scope

    Returns:
        acc(float): accuracy value

    Example:
        .. code-block:: python

          # in model.py
          correct = fluid.layers.create_global_var(dtype='float32', shape=[1], value=0)
          total = fluid.layers.create_global_var(dtype='float32', shape=[1], value=0)
          acc = fluid.layers.acc(predict, label, k=1, correct=correct, total=total)

          global_correct = fluid.layers.create_global_var(persistable=True, dtype='float32', shape=[1], value=0)
          tmp1 = fluid.layers.elementwise_min(correct, global_correct)
          fluid.layers.assign(tmp1, global_correct)

          global_total = fluid.layers.create_global_var(persistable=True, dtype='float32', shape=[1], value=0)
          tmp2 = fluid.layers.elementwise_min(total, global_total)
          fluid.layers.assign(tmp2, global_total)

          # in train.py, after train or infer
          correct_num = np.array(scope.find_var(correct.name).get_tensor())
          total_num = np.array(scope.find_var(total.name).get_tensor())
          print("accuracy: ", paddle.distributed.fleet.acc(correct_num, total_num))
    Nr   r   )r   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r.   )ZcorrectÚtotalr   r   Zglobal_correct_numr1   r   r   r   Úaccu  s"   !





r9   )NN)Ú__doc__r3   Únumpyr   Zpaddle.staticr   r   Ú__all__r   r   r   r+   r2   r6   r7   r9   r   r   r   r   Ú<module>   s   

(
(
(
S
1
10