From 13e254faedd2c464fa14057d90c66995b2b4f159 Mon Sep 17 00:00:00 2001 From: jerrywgz Date: Tue, 20 Nov 2018 13:08:23 +0000 Subject: [PATCH] refine code, test=develop --- paddle/fluid/API.spec | 2 +- .../operators/sigmoid_cross_entropy_with_logits_op.cc | 4 ++-- .../operators/sigmoid_cross_entropy_with_logits_op.h | 10 ---------- python/paddle/fluid/layers/nn.py | 2 +- python/paddle/fluid/tests/unittests/test_layers.py | 2 +- .../test_sigmoid_cross_entropy_with_logits_op.py | 1 - 6 files changed, 5 insertions(+), 16 deletions(-) diff --git a/paddle/fluid/API.spec b/paddle/fluid/API.spec index da8941c351..f84ec4cb3e 100644 --- a/paddle/fluid/API.spec +++ b/paddle/fluid/API.spec @@ -174,7 +174,7 @@ paddle.fluid.layers.clip ArgSpec(args=['x', 'min', 'max', 'name'], varargs=None, paddle.fluid.layers.clip_by_norm ArgSpec(args=['x', 'max_norm', 'name'], varargs=None, keywords=None, defaults=(None,)) paddle.fluid.layers.mean ArgSpec(args=['x', 'name'], varargs=None, keywords=None, defaults=(None,)) paddle.fluid.layers.mul ArgSpec(args=['x', 'y', 'x_num_col_dims', 'y_num_col_dims', 'name'], varargs=None, keywords=None, defaults=(1, 1, None)) -paddle.fluid.layers.sigmoid_cross_entropy_with_logits ArgSpec(args=['x', 'label', 'name'], varargs=None, keywords=None, defaults=(None,)) +paddle.fluid.layers.sigmoid_cross_entropy_with_logits ArgSpec(args=['x', 'label', 'ignore_index', 'name'], varargs=None, keywords=None, defaults=(-100, None)) paddle.fluid.layers.maxout ArgSpec(args=['x', 'groups', 'name'], varargs=None, keywords=None, defaults=(None,)) paddle.fluid.layers.space_to_depth ArgSpec(args=['x', 'blocksize', 'name'], varargs=None, keywords=None, defaults=(None,)) paddle.fluid.layers.affine_grid ArgSpec(args=['theta', 'out_shape', 'name'], varargs=None, keywords=None, defaults=(None,)) diff --git a/paddle/fluid/operators/sigmoid_cross_entropy_with_logits_op.cc b/paddle/fluid/operators/sigmoid_cross_entropy_with_logits_op.cc index d6a2fa6a17..368988d60d 100644 --- a/paddle/fluid/operators/sigmoid_cross_entropy_with_logits_op.cc +++ b/paddle/fluid/operators/sigmoid_cross_entropy_with_logits_op.cc @@ -102,9 +102,9 @@ class SigmoidCrossEntropyWithLogitsOpMaker " of elementwise logistic losses."); AddAttr( "ignore_index", - "(int, default -1), Specifies a target value that is ignored and" + "(int, default -100), Specifies a target value that is ignored and" "does not contribute to the input gradient.") - .SetDefault(-1); + .SetDefault(-100); AddComment(R"DOC( SigmoidCrossEntropyWithLogits Operator. diff --git a/paddle/fluid/operators/sigmoid_cross_entropy_with_logits_op.h b/paddle/fluid/operators/sigmoid_cross_entropy_with_logits_op.h index 2bfba6f170..b8731c2327 100644 --- a/paddle/fluid/operators/sigmoid_cross_entropy_with_logits_op.h +++ b/paddle/fluid/operators/sigmoid_cross_entropy_with_logits_op.h @@ -31,7 +31,6 @@ using EigenMatrix = framework::EigenMatrix; template struct SigmoidCrossEntropyWithLogitsForward { - // EIGEN_EMPTY_STRUCT_CTOR(SigmoidCrossEntropyWithLogitsForward) HOSTDEVICE SigmoidCrossEntropyWithLogitsForward(const int &ignore_index) : ignore_index(ignore_index) {} @@ -50,7 +49,6 @@ struct SigmoidCrossEntropyWithLogitsForward { template struct SigmoidCrossEntropyWithLogitsBackward { - // EIGEN_EMPTY_STRUCT_CTOR(SigmoidCrossEntropyWithLogitsForward) HOSTDEVICE SigmoidCrossEntropyWithLogitsBackward(const int &ignore_index) : ignore_index(ignore_index) {} @@ -83,14 +81,6 @@ class SigmoidCrossEntropyWithLogitsKernel : public framework::OpKernel { out.device(place) = x.binaryExpr( labels, SigmoidCrossEntropyWithLogitsForward(ignore_index)); - // term1 = max(x, 0) - // auto term1 = x.cwiseMax(static_cast(0)); - // term2 = x * labels - // auto term2 = x * labels; - // term3 = log(1 + exp(-abs(x))) - // auto term3 = (static_cast(1) + (-(x.abs())).exp()).log(); - - // out.device(place) = term1 - term2 + term3; } }; diff --git a/python/paddle/fluid/layers/nn.py b/python/paddle/fluid/layers/nn.py index e032835de3..38da9173cc 100644 --- a/python/paddle/fluid/layers/nn.py +++ b/python/paddle/fluid/layers/nn.py @@ -7892,7 +7892,7 @@ def mul(x, y, x_num_col_dims=1, y_num_col_dims=1, name=None): @templatedoc() -def sigmoid_cross_entropy_with_logits(x, label, ignore_index=-1, name=None): +def sigmoid_cross_entropy_with_logits(x, label, ignore_index=-100, name=None): """ ${comment} diff --git a/python/paddle/fluid/tests/unittests/test_layers.py b/python/paddle/fluid/tests/unittests/test_layers.py index 8e098e4961..326938e115 100644 --- a/python/paddle/fluid/tests/unittests/test_layers.py +++ b/python/paddle/fluid/tests/unittests/test_layers.py @@ -173,7 +173,7 @@ class TestBook(unittest.TestCase): ignore_index = -1 self.assertIsNotNone( layers.sigmoid_cross_entropy_with_logits( - x=dat, label=lbl, ignore_index=-1)) + x=dat, label=lbl, ignore_index=ignore_index)) print(str(program)) def test_hsigmoid(self): diff --git a/python/paddle/fluid/tests/unittests/test_sigmoid_cross_entropy_with_logits_op.py b/python/paddle/fluid/tests/unittests/test_sigmoid_cross_entropy_with_logits_op.py index 64f6f088e1..41797a241c 100644 --- a/python/paddle/fluid/tests/unittests/test_sigmoid_cross_entropy_with_logits_op.py +++ b/python/paddle/fluid/tests/unittests/test_sigmoid_cross_entropy_with_logits_op.py @@ -119,4 +119,3 @@ class TestSigmoidCrossEntropyWithLogitsOp3(OpTest): if __name__ == '__main__': unittest.main() - np.random.seed(0) -- GitLab