Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
8c9c81cc
P
Paddle
项目概览
PaddlePaddle
/
Paddle
1 年多 前同步成功
通知
2302
Star
20931
Fork
5422
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
8c9c81cc
编写于
12月 20, 2021
作者:
F
Feiyu Chan
提交者:
GitHub
12月 20, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add doc for is_complex and is_integer and expose them as public APIs (#38158)
上级
a615002a
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
143 addition
and
0 deletion
+143
-0
python/paddle/__init__.py
python/paddle/__init__.py
+4
-0
python/paddle/fluid/tests/unittests/test_is_complex.py
python/paddle/fluid/tests/unittests/test_is_complex.py
+39
-0
python/paddle/fluid/tests/unittests/test_is_integer.py
python/paddle/fluid/tests/unittests/test_is_integer.py
+39
-0
python/paddle/tensor/__init__.py
python/paddle/tensor/__init__.py
+4
-0
python/paddle/tensor/attribute.py
python/paddle/tensor/attribute.py
+57
-0
未找到文件。
python/paddle/__init__.py
浏览文件 @
8c9c81cc
...
...
@@ -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'
,
...
...
python/paddle/fluid/tests/unittests/test_is_complex.py
0 → 100644
浏览文件 @
8c9c81cc
# 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
()
python/paddle/fluid/tests/unittests/test_is_integer.py
0 → 100644
浏览文件 @
8c9c81cc
# 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
()
python/paddle/tensor/__init__.py
浏览文件 @
8c9c81cc
...
...
@@ -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'
,
...
...
python/paddle/tensor/attribute.py
浏览文件 @
8c9c81cc
...
...
@@ -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
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录