未验证 提交 f2bc1576 编写于 作者: W wangzhen38 提交者: GitHub

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
上级 7460a891
...@@ -23,11 +23,12 @@ from paddle.fluid.initializer import Normal, Constant ...@@ -23,11 +23,12 @@ from paddle.fluid.initializer import Normal, Constant
from paddle.fluid.framework import Variable from paddle.fluid.framework import Variable
from paddle.fluid.param_attr import ParamAttr from paddle.fluid.param_attr import ParamAttr
from paddle.fluid.layers import nn from paddle.fluid.layers import nn
from paddle.fluid.layers import tensor
__all__ = ['ctr_metric_bundle'] __all__ = ['ctr_metric_bundle']
def ctr_metric_bundle(input, label): def ctr_metric_bundle(input, label, ins_tag_weight=None):
""" """
ctr related metric layer ctr related metric layer
...@@ -48,6 +49,9 @@ def ctr_metric_bundle(input, label): ...@@ -48,6 +49,9 @@ def ctr_metric_bundle(input, label):
Variable indicates the probability of each label. Variable indicates the probability of each label.
label(Variable): A 2D int Variable indicating the label of the training label(Variable): A 2D int Variable indicating the label of the training
data. The height is batch size and width is always 1. 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: Returns:
local_sqrerr(Variable): Local sum of squared error local_sqrerr(Variable): Local sum of squared error
...@@ -60,10 +64,15 @@ def ctr_metric_bundle(input, label): ...@@ -60,10 +64,15 @@ def ctr_metric_bundle(input, label):
import paddle.fluid as fluid import paddle.fluid as fluid
data = fluid.layers.data(name="data", shape=[32, 32], dtype="float32") 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)) 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 assert input.shape == label.shape
helper = LayerHelper("ctr_metric_bundle", **locals()) helper = LayerHelper("ctr_metric_bundle", **locals())
...@@ -150,11 +159,6 @@ def ctr_metric_bundle(input, label): ...@@ -150,11 +159,6 @@ def ctr_metric_bundle(input, label):
type="reduce_sum", type="reduce_sum",
inputs={"X": [tmp_res_sigmoid]}, inputs={"X": [tmp_res_sigmoid]},
outputs={"Out": [batch_q]}) outputs={"Out": [batch_q]})
helper.append_op(
type="elementwise_add",
inputs={"X": [batch_q],
"Y": [local_q]},
outputs={"Out": [local_q]})
helper.append_op( helper.append_op(
type="reduce_sum", type="reduce_sum",
...@@ -179,10 +183,42 @@ def ctr_metric_bundle(input, label): ...@@ -179,10 +183,42 @@ def ctr_metric_bundle(input, label):
type="reduce_sum", type="reduce_sum",
inputs={"X": [tmp_ones]}, inputs={"X": [tmp_ones]},
outputs={"Out": [batch_ins_num]}) 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( helper.append_op(
type="elementwise_add", type="elementwise_add",
inputs={"X": [batch_ins_num], inputs={"X": [batch_ins_num],
"Y": [local_ins_num]}, "Y": [local_ins_num]},
outputs={"Out": [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 return local_sqrerr, local_abserr, local_prob, local_q, local_pos_num, local_ins_num
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册