From 1a0f34905fa9a65bc376140995f8c96b0eb91c3b Mon Sep 17 00:00:00 2001 From: xiaoting <31891223+tink2123@users.noreply.github.com> Date: Tue, 7 Feb 2023 14:08:47 +0800 Subject: [PATCH] [Zero-Dim] add upsample 0D test and fix api doc (#50237) * add upsample 0d test * update api references * update api references * update api reference --- .../tests/unittests/test_zero_dim_tensor.py | 40 +++++++++++++++++++ python/paddle/nn/functional/common.py | 8 ++-- 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py b/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py index 3170f3f62c..190f0eaac8 100644 --- a/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py +++ b/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py @@ -1528,6 +1528,24 @@ class TestSundryAPI(unittest.TestCase): origin_result.numpy(), out3.numpy(), rtol=1e-05 ) + def test_upsample(self): + from paddle.nn.functional import upsample + + input_x = paddle.rand([2, 3, 6, 6]) + input_x.stop_gradient = False + + output_size = [ + paddle.full([], 12, dtype="int32"), + paddle.full([], 12, dtype="int32"), + ] + out1 = upsample( + x=input_x, size=output_size, mode="bilinear", align_corners=False + ) + out1.backward() + + self.assertEqual(out1.shape, [2, 3, 12, 12]) + self.assertEqual(input_x.grad.shape, [2, 3, 6, 6]) + def test_maseked_select(self): x = paddle.rand([]) x.stop_gradient = False @@ -2512,6 +2530,28 @@ class TestSundryAPIStatic(unittest.TestCase): self.assertEqual(res2[0].shape, (2, 3, 12, 12)) self.assertEqual(res2[1].shape, (2, 3, 6, 6)) + @prog_scope() + def test_upsample(self): + from paddle.nn.functional import upsample + + input_x = paddle.rand([2, 3, 6, 6]) + input_x.stop_gradient = False + + output_size = [ + paddle.full([], 12, dtype="int32"), + paddle.full([], 12, dtype="int32"), + ] + + out1 = upsample( + x=input_x, size=output_size, mode="bilinear", align_corners=False + ) + paddle.static.append_backward(out1.sum()) + prog = paddle.static.default_main_program() + res1 = self.exe.run(prog, feed={}, fetch_list=[out1, input_x.grad_name]) + + self.assertEqual(res1[0].shape, (2, 3, 12, 12)) + self.assertEqual(res1[1].shape, (2, 3, 6, 6)) + @prog_scope() def test_maseked_select(self): x = paddle.rand([]) diff --git a/python/paddle/nn/functional/common.py b/python/paddle/nn/functional/common.py index 05968d593e..5fa2c0eda0 100644 --- a/python/paddle/nn/functional/common.py +++ b/python/paddle/nn/functional/common.py @@ -318,11 +318,11 @@ def interpolate( size (list|tuple|Tensor|None): Output shape of image resize layer, the shape is (out_w, ) when input is a 3-D Tensor, the shape is (out_h, out_w) when input is a 4-D Tensor and is (out_d, out_h, out_w) when input is a 5-D Tensor. - Default: None. If a list/tuple, each element can be an integer or a Tensor of shape: [1]. + Default: None. If a list/tuple, each element can be an integer or a Tensor of shape: [1] or []. If a Tensor, its dimensions size should be a 1. scale_factor (float|Tensor|list|tuple|None): The multiplier for the input height or width. At least one of :attr:`size` or :attr:`scale_factor` must be set. - And :attr:`size` has a higher priority than :attr:`scale_factor`.Has to match input size if it is either a list or a tuple or a Tensor. + And :attr:`size` has a higher priority than :attr:`scale_factor`.Has to match input size if it is either a list or a tuple or a Tensor.If a list/tuple, each element can be an integer or a Tensor of shape: [1] or []. Default: None. mode (str): The resample method. It supports 'linear', 'area', 'nearest', 'bilinear', 'bicubic' and 'trilinear' currently. Default: 'nearest' @@ -870,12 +870,12 @@ def upsample( size (list|tuple|Tensor|None, optional): Output shape of image resize layer, the shape is (out_w, ) when input is a 3-D Tensor, the shape is (out_h, out_w) when input is a 4-D Tensor and is (out_d, out_h, out_w) when input is a 5-D Tensor. - Default: None. If a list/tuple, each element can be an integer or a Tensor of shape: [1]. + Default: None. If a list/tuple, each element can be an integer or a Tensor of shape: [1] or []. If a Tensor , its dimensions size should be a 1. scale_factor (float|Tensor|list|tuple|None, optional): The multiplier for the input height or width. At least one of :attr:`size` or :attr:`scale_factor` must be set. And :attr:`size` has a higher priority than :attr:`scale_factor`.Has to match input size if - it is either a list or a tuple or a Tensor. + it is either a list or a tuple or a Tensor. If a list/tuple, each element can be an integer or a Tensor of shape: [1] or []. Default: None. mode (str, optional): The resample method. It supports 'linear', 'nearest', 'bilinear', 'bicubic' and 'trilinear' currently. Default: 'nearest' -- GitLab