Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
e8edfc7d
P
Paddle
项目概览
机器未来
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
e8edfc7d
编写于
9月 07, 2020
作者:
A
AshburnLee
提交者:
GitHub
9月 07, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add shape check for fill_constant OP and remove doc of type error (#26919) (#27080)
上级
66144efc
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
30 addition
and
17 deletion
+30
-17
python/paddle/fluid/layers/tensor.py
python/paddle/fluid/layers/tensor.py
+2
-17
python/paddle/fluid/layers/utils.py
python/paddle/fluid/layers/utils.py
+20
-0
python/paddle/fluid/tests/unittests/test_fill_constant_op.py
python/paddle/fluid/tests/unittests/test_fill_constant_op.py
+8
-0
未找到文件。
python/paddle/fluid/layers/tensor.py
浏览文件 @
e8edfc7d
...
...
@@ -29,6 +29,7 @@ from ..data_feeder import check_variable_and_dtype, check_type, check_dtype, con
from
paddle.utils
import
deprecated
import
numpy
import
warnings
from
.utils
import
check_shape
__all__
=
[
'create_tensor'
,
'create_parameter'
,
'create_global_var'
,
'cast'
,
...
...
@@ -652,12 +653,6 @@ def fill_constant(shape, dtype, value, force_cpu=False, out=None, name=None):
Returns:
Tensor: Tensor which is created according to shape and dtype.
Raises:
TypeError: The dtype must be one of bool, float16, float32, float64, int32 and int64
and the data type of ``out`` must be the same as the ``dtype``.
TypeError: The shape must be one of list, tuple and Tensor, the data type of ``shape``
must be int32 or int64 when ``shape`` is a Tensor
Examples:
.. code-block:: python
...
...
@@ -713,14 +708,12 @@ def fill_constant(shape, dtype, value, force_cpu=False, out=None, name=None):
value
=
cast
(
value
,
dtype
)
inputs
[
'ValueTensor'
]
=
value
check_shape
(
shape
)
check_dtype
(
dtype
,
'dtype'
,
[
'bool'
,
'float16'
,
'float32'
,
'float64'
,
'int32'
,
'int64'
],
'fill_constant'
)
check_type
(
shape
,
'shape'
,
(
Variable
,
list
,
tuple
),
'fill_constant'
)
if
isinstance
(
shape
,
Variable
):
check_dtype
(
shape
.
dtype
,
'shape'
,
[
'int32'
,
'int64'
],
'fill_constant'
)
if
out
is
not
None
:
check_variable_and_dtype
(
out
,
'out'
,
[
convert_dtype
(
dtype
)],
'fill_constant'
)
...
...
@@ -1045,10 +1038,6 @@ def ones(shape, dtype, force_cpu=False):
Returns:
Tensor: A tensor of data type :attr:`dtype` with shape :attr:`shape` and all elements set to 1.
Raises:
TypeError: The ``dtype`` must be one of bool, float16, float32, float64, int32, int64.
TypeError: The ``shape`` must be one of list, tuple and Tensor. The data type of ``shape`` must
be int32 or int64 when it's a Tensor.
Examples:
.. code-block:: python
...
...
@@ -1081,10 +1070,6 @@ def zeros(shape, dtype, force_cpu=False, name=None):
Returns:
Tensor: A tensor of data type :attr:`dtype` with shape :attr:`shape` and all elements set to 0.
Raises:
TypeError: The ``dtype`` must be one of bool, float16, float32, float64, int32, int64.
TypeError: The ``shape`` must be one of list, tuple and Tensor. The data type of ``shape`` must
be int32 or int64 when it's a Tensor.
Examples:
.. code-block:: python
...
...
python/paddle/fluid/layers/utils.py
浏览文件 @
e8edfc7d
...
...
@@ -20,6 +20,7 @@ import numpy as np
from
..framework
import
Variable
from
..data_feeder
import
convert_dtype
,
check_variable_and_dtype
,
check_type
,
check_dtype
from
..layer_helper
import
LayerHelper
from
sys
import
version_info
def
convert_to_list
(
value
,
n
,
name
,
dtype
=
np
.
int
):
...
...
@@ -358,3 +359,22 @@ def convert_shape_to_list(shape):
else
:
shape
=
list
(
shape
.
numpy
().
astype
(
int
))
return
shape
def
check_shape
(
shape
):
"""
Check shape type and shape elements type before passing it to fill_constant
"""
if
isinstance
(
shape
,
Variable
):
check_dtype
(
shape
.
dtype
,
'shape'
,
[
'int32'
,
'int64'
],
'fill_constant'
)
else
:
for
ele
in
shape
:
if
not
isinstance
(
ele
,
Variable
):
if
ele
<
0
:
raise
ValueError
(
"All elements in ``shape`` must be positive when it's a list or tuple"
)
if
not
isinstance
(
ele
,
six
.
integer_types
):
raise
TypeError
(
"All elements in ``shape`` must be integers when it's a list or tuple"
)
python/paddle/fluid/tests/unittests/test_fill_constant_op.py
浏览文件 @
e8edfc7d
...
...
@@ -350,6 +350,14 @@ class TestFillConstantOpError(unittest.TestCase):
dtype
=
'int16'
,
out
=
x1
)
self
.
assertRaises
(
TypeError
,
fluid
.
layers
.
fill_constant
,
shape
=
[
1.1
],
value
=
5
,
dtype
=
'float32'
,
out
=
x1
)
# The argument dtype of fill_constant_op must be one of bool, float16,
#float32, float64, int32 or int64
x2
=
fluid
.
layers
.
data
(
name
=
'x2'
,
shape
=
[
1
],
dtype
=
"int32"
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录