Skip to content

Commit

Permalink
Fix Python IndexError of case11: paddle.static.nn.data_norm (#50097)
Browse files Browse the repository at this point in the history
* 为data_norm函数的input参数增加维度的判断并增加单侧

* 完善data_norm测试样例

* fix data_norm
  • Loading branch information
longranger2 authored Feb 6, 2023
1 parent 4b4d92e commit 6908550
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
5 changes: 5 additions & 0 deletions python/paddle/fluid/tests/unittests/test_data_norm_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,11 @@ def test_errors(self):
input=x2, param_attr={}, enable_scale_and_shift=True
)

# Test input with dimension 1
paddle.enable_static()
x3 = paddle.static.data("", shape=[0], dtype="float32")
self.assertRaises(ValueError, paddle.static.nn.data_norm, x3)


if __name__ == '__main__':
unittest.main()
6 changes: 6 additions & 0 deletions python/paddle/static/nn/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,12 @@ def data_norm(
dtype = helper.input_dtype()

input_shape = input.shape
if len(input_shape) < 2:
raise ValueError(
"The shape pf Input < 2 (got {}D input, input shape is: {})".format(
len(input_shape), input_shape
)
)
if data_layout == 'NCHW':
channel_num = input_shape[1]
else:
Expand Down

0 comments on commit 6908550

Please sign in to comment.