diff --git a/paddle/fluid/operators/fill_constant_op.h b/paddle/fluid/operators/fill_constant_op.h index 239083f88d9c63c4790fe4dd3060a5cf473ff73c..cce28cae975001ff30040b0e81b2d90e82ed12e1 100644 --- a/paddle/fluid/operators/fill_constant_op.h +++ b/paddle/fluid/operators/fill_constant_op.h @@ -50,6 +50,8 @@ class FillConstantKernel : public framework::OpKernel { // handle NaN/Inf first, which cannot be read from stream. if (str_value == "inf") { value = static_cast(std::numeric_limits::infinity()); + } else if (str_value == "-inf") { + value = static_cast(-std::numeric_limits::infinity()); } else if (str_value == "nan") { value = static_cast(std::numeric_limits::quiet_NaN()); } else { diff --git a/python/paddle/fluid/tests/unittests/test_fill_constant_op.py b/python/paddle/fluid/tests/unittests/test_fill_constant_op.py index babfcdb9040df78f13204bbcad28ca01cff48040..c305f71aa53657761cd017e8762b744213708900 100644 --- a/python/paddle/fluid/tests/unittests/test_fill_constant_op.py +++ b/python/paddle/fluid/tests/unittests/test_fill_constant_op.py @@ -340,6 +340,12 @@ class TestFillConstantImperative(unittest.TestCase): res = fluid.layers.fill_constant([1], 'float32', np.inf) self.assertTrue(np.isinf(res.numpy().item(0))) + def test_ninf(self): + with fluid.dygraph.guard(): + res = fluid.layers.fill_constant([1], 'float32', np.NINF) + self.assertTrue(np.isinf(res.numpy().item(0))) + self.assertEqual(np.NINF, res.numpy().item(0)) + class TestFillConstantOpError(unittest.TestCase): def test_errors(self):