From dc0900730df09a3b18dc19bf20b01467fa14cd6e Mon Sep 17 00:00:00 2001 From: megemini Date: Tue, 14 Nov 2023 17:31:38 +0800 Subject: [PATCH] [Add] atleast_Nd as tensor method --- python/paddle/tensor/__init__.py | 3 +++ test/legacy_test/test_atleast_nd.py | 29 ++++++++++++++++++++++++++++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/python/paddle/tensor/__init__.py b/python/paddle/tensor/__init__.py index 2bfc7183343c2..a1557bb4585d8 100644 --- a/python/paddle/tensor/__init__.py +++ b/python/paddle/tensor/__init__.py @@ -734,6 +734,9 @@ 'normal_', 'index_fill', 'index_fill_', + 'atleast_1d', + 'atleast_2d', + 'atleast_3d', ] # this list used in math_op_patch.py for magic_method bind diff --git a/test/legacy_test/test_atleast_nd.py b/test/legacy_test/test_atleast_nd.py index d22149672fcdc..781534dde25ce 100644 --- a/test/legacy_test/test_atleast_nd.py +++ b/test/legacy_test/test_atleast_nd.py @@ -413,7 +413,7 @@ def test_all(self): ), ) class TestAtleastErrorCombineInputs(BaseTest): - """test combine inputs, like: `at_leastNd((x, y))`, where paddle treats like numpy, not pytorch""" + """test combine inputs, like: `at_leastNd((x, y))`, where paddle treats like numpy""" def test_all(self): with self.assertRaises(ValueError): @@ -427,5 +427,32 @@ def test_all(self): ) +class TestAtleastAsTensorMethod(unittest.TestCase): + def test_as_tensor_method(self): + input = 123 + tensor = paddle.to_tensor(input) + + for place in PLACES: + paddle.disable_static(place) + + out = tensor.atleast_1d() + out_ref = np.atleast_1d(input) + + for n, p in zip(out_ref, out): + np.testing.assert_allclose(n, p.numpy(), rtol=RTOL, atol=ATOL) + + out = tensor.atleast_2d() + out_ref = np.atleast_2d(input) + + for n, p in zip(out_ref, out): + np.testing.assert_allclose(n, p.numpy(), rtol=RTOL, atol=ATOL) + + out = tensor.atleast_3d() + out_ref = np.atleast_3d(input) + + for n, p in zip(out_ref, out): + np.testing.assert_allclose(n, p.numpy(), rtol=RTOL, atol=ATOL) + + if __name__ == '__main__': unittest.main()