From f2bc1576b4177bf3e93023259fc8f07a0368a17a Mon Sep 17 00:00:00 2001 From: wangzhen38 <41941775+wangzhen38@users.noreply.github.com> Date: Tue, 9 Aug 2022 10:18:03 +0800 Subject: [PATCH] fix ctr_metric_bundle for instag (#40838) * fix ctr_metric_bundle for instag * fix ctr_metric_bundle for instag * update * fix zero set * fix zero set by to_str * update to_string pare * optmize default value * simple code * update metric for real data * update metric for real data * update metric for real data --- .../paddle/fluid/contrib/layers/metric_op.py | 52 ++++++++++++++++--- 1 file changed, 44 insertions(+), 8 deletions(-) diff --git a/python/paddle/fluid/contrib/layers/metric_op.py b/python/paddle/fluid/contrib/layers/metric_op.py index f76a3283f2..8bc372085a 100644 --- a/python/paddle/fluid/contrib/layers/metric_op.py +++ b/python/paddle/fluid/contrib/layers/metric_op.py @@ -23,11 +23,12 @@ from paddle.fluid.initializer import Normal, Constant from paddle.fluid.framework import Variable from paddle.fluid.param_attr import ParamAttr from paddle.fluid.layers import nn +from paddle.fluid.layers import tensor __all__ = ['ctr_metric_bundle'] -def ctr_metric_bundle(input, label): +def ctr_metric_bundle(input, label, ins_tag_weight=None): """ ctr related metric layer @@ -48,6 +49,9 @@ def ctr_metric_bundle(input, label): Variable indicates the probability of each label. label(Variable): A 2D int Variable indicating the label of the training data. The height is batch size and width is always 1. + ins_tag_weight(Variable): A 2D int Variable indicating the ins_tag_weight of the training + data. 1 means real data, 0 means fake data. + A LoDTensor or Tensor with type float32,float64. Returns: local_sqrerr(Variable): Local sum of squared error @@ -60,10 +64,15 @@ def ctr_metric_bundle(input, label): import paddle.fluid as fluid data = fluid.layers.data(name="data", shape=[32, 32], dtype="float32") - label = fluid.layers.data(name="label", shape=[1], dtype="int32") + label = fluid.layers.data(name="label", shape=[-1, 1], dtype="int32") + ins_tag_weight = fluid.layers.data(name="ins_tag_weight", shape=[-1, 1], dtype="float32") predict = fluid.layers.sigmoid(fluid.layers.fc(input=data, size=1)) - auc_out = fluid.contrib.layers.ctr_metric_bundle(input=predict, label=label) + auc_out = fluid.contrib.layers.ctr_metric_bundle(input=predict, label=label, ins_tag_weight=ins_tag_weight) """ + if ins_tag_weight is None: + ins_tag_weight = tensor.fill_constant( + shape=[1, 1], dtype="float32", value=1.0) + assert input.shape == label.shape helper = LayerHelper("ctr_metric_bundle", **locals()) @@ -150,11 +159,6 @@ def ctr_metric_bundle(input, label): type="reduce_sum", inputs={"X": [tmp_res_sigmoid]}, outputs={"Out": [batch_q]}) - helper.append_op( - type="elementwise_add", - inputs={"X": [batch_q], - "Y": [local_q]}, - outputs={"Out": [local_q]}) helper.append_op( type="reduce_sum", @@ -179,10 +183,42 @@ def ctr_metric_bundle(input, label): type="reduce_sum", inputs={"X": [tmp_ones]}, outputs={"Out": [batch_ins_num]}) + + #if data is fake, return 0 + inputs_slice = {'Input': ins_tag_weight} + attrs = {'axes': [0]} + attrs['starts'] = [0] + attrs['ends'] = [1] + helper.append_op( + type="slice", + inputs=inputs_slice, + attrs=attrs, + outputs={"Out": ins_tag_weight}) + + axis = helper.kwargs.get('axis', 0) + helper.append_op( + type="elementwise_mul", + inputs={"X": [batch_ins_num], + "Y": [ins_tag_weight]}, + outputs={"Out": [batch_ins_num]}, + attrs={'axis': axis}) + helper.append_op( type="elementwise_add", inputs={"X": [batch_ins_num], "Y": [local_ins_num]}, outputs={"Out": [local_ins_num]}) + helper.append_op( + type="elementwise_mul", + inputs={"X": [batch_q], + "Y": [ins_tag_weight]}, + outputs={"Out": [batch_q]}, + attrs={'axis': axis}) + helper.append_op( + type="elementwise_add", + inputs={"X": [batch_q], + "Y": [local_q]}, + outputs={"Out": [local_q]}) + return local_sqrerr, local_abserr, local_prob, local_q, local_pos_num, local_ins_num -- GitLab