diff --git a/python/paddle/fluid/tests/unittests/test_normalize.py b/python/paddle/fluid/tests/unittests/test_normalize.py index 908360d29e692..a986dd90415d5 100644 --- a/python/paddle/fluid/tests/unittests/test_normalize.py +++ b/python/paddle/fluid/tests/unittests/test_normalize.py @@ -97,6 +97,18 @@ def test_gpu(self): with fluid.program_guard(fluid.Program()): self.run_static(use_gpu=True) + def test_errors(self): + with fluid.dygraph.guard(): + # The size of input in Normalize should not be 0. + def test_0_size(): + array = np.array([], dtype=np.float32) + x = paddle.to_tensor( + np.reshape(array, [1, 1, 0]), dtype='float32' + ) + paddle.nn.functional.normalize(x) + + self.assertRaises(ValueError, test_0_size) + if __name__ == "__main__": unittest.main() diff --git a/python/paddle/nn/functional/norm.py b/python/paddle/nn/functional/norm.py index 87723f6bb8f58..0c194d45ed8d3 100644 --- a/python/paddle/nn/functional/norm.py +++ b/python/paddle/nn/functional/norm.py @@ -77,6 +77,7 @@ def normalize(x, p=2, axis=1, epsilon=1e-12, name=None): # [[0. , 0.24253564, 0.37139067], # [1. , 0.97014254, 0.92847669]]) """ + if in_dygraph_mode(): eps = fluid.dygraph.base.to_variable([epsilon], dtype=x.dtype) out = _C_ops.p_norm(x, float(p), axis, epsilon, True, False)