Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Divide by 0 Error] add normalize check #49977

Merged
merged 3 commits into from
Feb 3, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions python/paddle/fluid/tests/unittests/test_normalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
1 change: 1 addition & 0 deletions python/paddle/nn/functional/norm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down