From 8c9c81cc022defe47fa194985ffda08d4736014a Mon Sep 17 00:00:00 2001 From: Feiyu Chan Date: Mon, 20 Dec 2021 11:15:23 +0800 Subject: [PATCH] add doc for is_complex and is_integer and expose them as public APIs (#38158) --- python/paddle/__init__.py | 4 ++ .../fluid/tests/unittests/test_is_complex.py | 39 +++++++++++++ .../fluid/tests/unittests/test_is_integer.py | 39 +++++++++++++ python/paddle/tensor/__init__.py | 4 ++ python/paddle/tensor/attribute.py | 57 +++++++++++++++++++ 5 files changed, 143 insertions(+) create mode 100644 python/paddle/fluid/tests/unittests/test_is_complex.py create mode 100644 python/paddle/fluid/tests/unittests/test_is_integer.py diff --git a/python/paddle/__init__.py b/python/paddle/__init__.py index e441e8f18c8..3e808262d5d 100755 --- a/python/paddle/__init__.py +++ b/python/paddle/__init__.py @@ -66,6 +66,8 @@ import paddle.vision # noqa: F401 from .tensor.random import bernoulli # noqa: F401 +from .tensor.attribute import is_complex # noqa: F401 +from .tensor.attribute import is_integer # noqa: F401 from .tensor.attribute import rank # noqa: F401 from .tensor.attribute import shape # noqa: F401 from .tensor.attribute import real # noqa: F401 @@ -379,6 +381,8 @@ __all__ = [ # noqa 'equal', 'equal_all', 'is_tensor', + 'is_complex', + 'is_integer', 'cross', 'where', 'log1p', diff --git a/python/paddle/fluid/tests/unittests/test_is_complex.py b/python/paddle/fluid/tests/unittests/test_is_complex.py new file mode 100644 index 00000000000..988c55ea61a --- /dev/null +++ b/python/paddle/fluid/tests/unittests/test_is_complex.py @@ -0,0 +1,39 @@ +# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import paddle +import numpy as np +import unittest + + +class TestIsComplex(unittest.TestCase): + def test_for_integer(self): + x = paddle.arange(10) + self.assertFalse(paddle.is_complex(x)) + + def test_for_floating_point(self): + x = paddle.randn([2, 3]) + self.assertFalse(paddle.is_complex(x)) + + def test_for_complex(self): + x = paddle.randn([2, 3]) + 1j * paddle.randn([2, 3]) + self.assertTrue(paddle.is_complex(x)) + + def test_for_exception(self): + with self.assertRaises(TypeError): + paddle.is_complex(np.array([1, 2])) + + +if __name__ == '__main__': + unittest.main() diff --git a/python/paddle/fluid/tests/unittests/test_is_integer.py b/python/paddle/fluid/tests/unittests/test_is_integer.py new file mode 100644 index 00000000000..1c33065e10b --- /dev/null +++ b/python/paddle/fluid/tests/unittests/test_is_integer.py @@ -0,0 +1,39 @@ +# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import paddle +import numpy as np +import unittest + + +class TestIsInteger(unittest.TestCase): + def test_for_integer(self): + x = paddle.arange(10) + self.assertTrue(paddle.is_integer(x)) + + def test_for_floating_point(self): + x = paddle.randn([2, 3]) + self.assertFalse(paddle.is_integer(x)) + + def test_for_complex(self): + x = paddle.randn([2, 3]) + 1j * paddle.randn([2, 3]) + self.assertFalse(paddle.is_integer(x)) + + def test_for_exception(self): + with self.assertRaises(TypeError): + paddle.is_integer(np.array([1, 2])) + + +if __name__ == '__main__': + unittest.main() diff --git a/python/paddle/tensor/__init__.py b/python/paddle/tensor/__init__.py index 95a64fcdd69..314938ad732 100755 --- a/python/paddle/tensor/__init__.py +++ b/python/paddle/tensor/__init__.py @@ -12,6 +12,8 @@ # See the License for the specific language governing permissions and # limitations under the License. +from .attribute import is_complex # noqa: F401 +from .attribute import is_integer # noqa: F401 from .attribute import rank # noqa: F401 from .attribute import shape # noqa: F401 from .attribute import real # noqa: F401 @@ -408,6 +410,8 @@ tensor_method_func = [ #noqa 'var', 'numel', 'median', + 'is_complex', + 'is_integer', 'rank', 'shape', 'real', diff --git a/python/paddle/tensor/attribute.py b/python/paddle/tensor/attribute.py index 44b34b3e2d6..aefe2e4749a 100644 --- a/python/paddle/tensor/attribute.py +++ b/python/paddle/tensor/attribute.py @@ -21,6 +21,7 @@ from ..fluid.data_feeder import check_variable_and_dtype # TODO: define functions to get tensor attributes from ..fluid.layers import rank # noqa: F401 from ..fluid.layers import shape # noqa: F401 +import paddle from paddle import _C_ops __all__ = [] @@ -45,6 +46,34 @@ def _real_to_complex_dtype(dtype): def is_complex(x): + """Return whether x is a tensor of complex data type(complex64 or complex128). + + Args: + x (Tensor): The input tensor. + + Returns: + bool: True if the data type of the input is complex data type, otherwise false. + + Examples: + .. code-block:: python + + import paddle + + x = paddle.to_tensor([1 + 2j, 3 + 4j]) + print(paddle.is_complex(x)) + # True + + x = paddle.to_tensor([1.1, 1.2]) + print(paddle.is_complex(x)) + # False + + x = paddle.to_tensor([1, 2, 3]) + print(paddle.is_complex(x)) + # False + """ + if not isinstance(x, (paddle.Tensor, paddle.static.Variable)): + raise TypeError("Expected Tensor, but received type of x: {}".format( + type(x))) dtype = x.dtype is_complex_dtype = (dtype == core.VarDesc.VarType.COMPLEX64 or dtype == core.VarDesc.VarType.COMPLEX128) @@ -61,6 +90,34 @@ def is_floating_point(x): def is_integer(x): + """Return whether x is a tensor of integeral data type. + + Args: + x (Tensor): The input tensor. + + Returns: + bool: True if the data type of the input is integer data type, otherwise false. + + Examples: + .. code-block:: python + + import paddle + + x = paddle.to_tensor([1 + 2j, 3 + 4j]) + print(paddle.is_integer(x)) + # False + + x = paddle.to_tensor([1.1, 1.2]) + print(paddle.is_integer(x)) + # False + + x = paddle.to_tensor([1, 2, 3]) + print(paddle.is_integer(x)) + # True + """ + if not isinstance(x, (paddle.Tensor, paddle.static.Variable)): + raise TypeError("Expected Tensor, but received type of x: {}".format( + type(x))) dtype = x.dtype is_int_dtype = (dtype == core.VarDesc.VarType.UINT8 or dtype == core.VarDesc.VarType.INT8 or -- GitLab