diff --git a/python/paddle/fluid/contrib/layers/metric_op.py b/python/paddle/fluid/contrib/layers/metric_op.py index f76a3283f2f81880fce5cd8b8fa4fc46434fd165..8bc372085a51d111a2a7298cf9fc0f98722f448a 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