diff --git a/python/paddle/fluid/tests/unittests/test_cross_entropy_loss.py b/python/paddle/fluid/tests/unittests/test_cross_entropy_loss.py index 897d76a35dcabfc5b39c9cfadd123e4ad4c27b6f..a89d47d351d0087ac351561a138ac5db9e26e169 100644 --- a/python/paddle/fluid/tests/unittests/test_cross_entropy_loss.py +++ b/python/paddle/fluid/tests/unittests/test_cross_entropy_loss.py @@ -20,6 +20,7 @@ import numpy as np import unittest from test_softmax_op import stable_softmax from test_softmax_with_cross_entropy_op import cross_entropy +from paddle.fluid import Program, program_guard def stable_softmax(x): @@ -1363,5 +1364,37 @@ class CrossEntropyLoss(unittest.TestCase): self.assertTrue(np.allclose(dy_ret_value, expected)) +class TestCrossEntropyFAPIError(unittest.TestCase): + def test_errors(self): + with program_guard(Program(), Program()): + + def test_LabelValue(): + input_data = paddle.rand(shape=[20, 100]) + label_data = paddle.randint( + 0, 100, shape=[20, 1], dtype="int64") + label_data[0] = 255 + weight_data = paddle.rand([100]) + paddle.nn.functional.cross_entropy( + input=input_data, + label=label_data, + weight=weight_data, + ignore_index=255) + + self.assertRaises(ValueError, test_LabelValue) + + def test_LabelValueNeg(): + input_data = paddle.rand(shape=[20, 100]) + label_data = paddle.randint( + 0, 100, shape=[20, 1], dtype="int64") + label_data[0] = -1 + weight_data = paddle.rand([100]) + paddle.nn.functional.cross_entropy( + input=input_data, + label=label_data, + weight=weight_data, + ignore_index=-1) + + self.assertRaises(ValueError, test_LabelValueNeg) + if __name__ == "__main__": unittest.main() diff --git a/python/paddle/nn/functional/loss.py b/python/paddle/nn/functional/loss.py index aa0bd8a8c5e3d5dcae7c26018d28ce93637947bd..eeb00625876468fac7ce3d1ebefd4b46a796d2c0 100755 --- a/python/paddle/nn/functional/loss.py +++ b/python/paddle/nn/functional/loss.py @@ -1411,6 +1411,13 @@ def cross_entropy(input, out = core.ops.elementwise_mul(out, weight_gather_reshape) else: + label_min = paddle.min(label) + label_max = paddle.max(label) + if label_min < 0 or label_max >= input.shape[-1]: + raise ValueError( + 'Expected 0 <= label_value < class_dimension({}), but got {} <= label_value <= {} '. + format(input.shape[-1], + label_min.numpy(), label_max.numpy())) weight_gather = core.ops.gather_nd(weight, label) input_shape = list(label.shape) weight_gather_reshape = reshape(