Exemple #1
0
def gauc(predicts, labels, indicator, **kwargs):
    namescope = "gauc"
    if "namescope" in kwargs:
        namescope = kwargs["namescope"]

    label_filter = np.array([], dtype=dtype_xdl_2_np(labels.dtype))
    if "filter" in kwargs:
        label_filter = kwargs["filter"]

    auc = _create_variable(namescope + "/auc", xdl.DataType.double)
    pv_num = _create_variable(namescope + "/pv_num", xdl.DataType.int64)

    cur_auc, cur_pv_num = xdl.gauc_calc_op(labels, predicts, indicator,
                                           label_filter)

    update_auc = _update_var(auc, cur_auc)
    update_pv_num = _update_var(pv_num, cur_pv_num)

    with xdl.control_dependencies([update_auc, update_pv_num]):
        auc_value = xdl.ps_pull_op(var_name=namescope + '/auc',
                                   var_type='index',
                                   dtype=xdl.DataType.double)
        pv_num_value = xdl.ps_pull_op(var_name=namescope + '/pv_num',
                                      var_type='index',
                                      dtype=xdl.DataType.int64)
        gauc = xdl.gauc_op(auc_value, pv_num_value)
    return gauc
Exemple #2
0
def auc(predictions, labels, **kwargs):
    """compute accumulative auc
       Args:
         predictions: prediction tensor
         labels: label tensor
         num_thresholds: the number of thresholds to use when discretizing the roc curve
       Returns:
         auc: a float auc value
    """
    num_thresholds = 200
    namescope = "auc"
    if "num_thresholds" in kwargs:
        num_thresholds = kwargs["num_thresholds"]
    if "namescope" in kwargs:
        namescope = kwargs["namescope"]
    # variable create to score the total tp,fp, tn, fn value
    tp = _create_auc_variable(namescope + "/tp", num_thresholds)
    fp = _create_auc_variable(namescope + "/fp", num_thresholds)
    tn = _create_auc_variable(namescope + "/tn", num_thresholds)
    fn = _create_auc_variable(namescope + "/fn", num_thresholds)

    cur_tp, cur_fp, cur_tn, cur_fn = xdl.confusion_matrix_op(
        predictions=predictions, labels=labels, num_thresholds=num_thresholds)

    update_tp = _update_auc(tp, cur_tp)
    update_fp = _update_auc(fp, cur_fp)
    update_tn = _update_auc(tn, cur_tn)
    update_fn = _update_auc(fn, cur_fn)
    with xdl.control_dependencies([update_tp, update_fp, update_tn,
                                   update_fn]):
        auc = xdl.auc_op(tp.value, fp.value, tn.value, fn.value)
    return auc
 def __init__(self, var_name, ids):
     super(GlobalStepMarkHook, self).__init__()
     from xdl.python.sparse_engine.embedding import *
     embedding_info = get_embedding_info_by_name(var_name)
     global_step = get_global_step().value
     with xdl.control_dependencies([embedding_info.embedding]):
         self._update = xdl.ps_mark_op(ids=ids,
                                       i=global_step,
                                       var_name=var_name,
                                       pattern="global_step")