From a34ecad8105d4be91cdab6c077b88159121bbc8b Mon Sep 17 00:00:00 2001 From: LoneRanger <836253168@qq.com> Date: Thu, 2 Feb 2023 16:52:00 +0800 Subject: [PATCH] Fix Python IndexError of case19: paddle.nn.functional.conv2d_transpose (#50006) --- .../fluid/tests/unittests/test_conv2d_transpose_op.py | 11 +++++++++++ python/paddle/nn/functional/conv.py | 6 ++++++ 2 files changed, 17 insertions(+) diff --git a/python/paddle/fluid/tests/unittests/test_conv2d_transpose_op.py b/python/paddle/fluid/tests/unittests/test_conv2d_transpose_op.py index 8933930356..e2c80404a5 100644 --- a/python/paddle/fluid/tests/unittests/test_conv2d_transpose_op.py +++ b/python/paddle/fluid/tests/unittests/test_conv2d_transpose_op.py @@ -1013,6 +1013,17 @@ class TestConv2DTransposeRepr(unittest.TestCase): paddle.enable_static() +class TestConv2dTranspose(unittest.TestCase): + def error_weight_input(self): + array = np.array([1], dtype=np.float32) + x = paddle.to_tensor(np.reshape(array, [1, 1, 1, 1]), dtype='float32') + weight = paddle.to_tensor(np.reshape(array, [1]), dtype='float32') + paddle.nn.functional.conv2d_transpose(x, weight, bias=0) + + def test_type_error(self): + self.assertRaises(ValueError, self.error_weight_input) + + class TestTensorOutputSize1(UnittestBase): def init_info(self): self.shapes = [[2, 3, 8, 8]] diff --git a/python/paddle/nn/functional/conv.py b/python/paddle/nn/functional/conv.py index 350eeaa383..2ad865c479 100644 --- a/python/paddle/nn/functional/conv.py +++ b/python/paddle/nn/functional/conv.py @@ -1204,6 +1204,12 @@ def conv2d_transpose( x.shape ) ) + if len(weight.shape) != 4: + raise ValueError( + "Input weight should be 4D tensor, but received weight with the shape of {}".format( + weight.shape + ) + ) num_channels = x.shape[channel_dim] if num_channels < 0: raise ValueError( -- GitLab