Skip to content

Commit

Permalink
[Add] atleast_Nd as tensor method
Browse files Browse the repository at this point in the history
  • Loading branch information
megemini committed Nov 14, 2023
1 parent 78d7788 commit dc09007
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
3 changes: 3 additions & 0 deletions python/paddle/tensor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 28 additions & 1 deletion test/legacy_test/test_atleast_nd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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()

0 comments on commit dc09007

Please sign in to comment.