Skip to content

Commit 620ce8b

Browse files
authored
[Divide by 0 Error] add normalize check (#49977)
* [Divide by 0 Error] add normalize check * [Divide by 0 Error] normalize check migrate to c++
1 parent 08e7259 commit 620ce8b

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

python/paddle/fluid/tests/unittests/test_normalize.py

+12
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,18 @@ def test_gpu(self):
9797
with fluid.program_guard(fluid.Program()):
9898
self.run_static(use_gpu=True)
9999

100+
def test_errors(self):
101+
with fluid.dygraph.guard():
102+
# The size of input in Normalize should not be 0.
103+
def test_0_size():
104+
array = np.array([], dtype=np.float32)
105+
x = paddle.to_tensor(
106+
np.reshape(array, [1, 1, 0]), dtype='float32'
107+
)
108+
paddle.nn.functional.normalize(x)
109+
110+
self.assertRaises(ValueError, test_0_size)
111+
100112

101113
if __name__ == "__main__":
102114
unittest.main()

python/paddle/nn/functional/norm.py

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ def normalize(x, p=2, axis=1, epsilon=1e-12, name=None):
7777
# [[0. , 0.24253564, 0.37139067],
7878
# [1. , 0.97014254, 0.92847669]])
7979
"""
80+
8081
if in_dygraph_mode():
8182
eps = fluid.dygraph.base.to_variable([epsilon], dtype=x.dtype)
8283
out = _C_ops.p_norm(x, float(p), axis, epsilon, True, False)

0 commit comments

Comments
 (0)