Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
b76f3b27
P
Paddle
项目概览
BaiXuePrincess
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
b76f3b27
编写于
4月 08, 2020
作者:
W
wangchaochaohu
提交者:
GitHub
4月 08, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add test for fill_value Tensor and refine the doc of full Op (#23524)
上级
62aff0a7
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
39 addition
and
10 deletion
+39
-10
python/paddle/fluid/tests/unittests/test_full_op.py
python/paddle/fluid/tests/unittests/test_full_op.py
+15
-2
python/paddle/tensor/creation.py
python/paddle/tensor/creation.py
+24
-8
未找到文件。
python/paddle/fluid/tests/unittests/test_full_op.py
浏览文件 @
b76f3b27
...
@@ -68,14 +68,18 @@ class TestFullAPI(unittest.TestCase):
...
@@ -68,14 +68,18 @@ class TestFullAPI(unittest.TestCase):
out_6
=
paddle
.
full
(
out_6
=
paddle
.
full
(
shape
=
shape_tensor_int64
,
dtype
=
np
.
float32
,
fill_value
=
1.1
)
shape
=
shape_tensor_int64
,
dtype
=
np
.
float32
,
fill_value
=
1.1
)
val
=
fluid
.
layers
.
fill_constant
(
shape
=
[
1
],
dtype
=
np
.
float32
,
value
=
1.1
)
out_7
=
paddle
.
full
(
shape
=
shape_tensor_int64
,
dtype
=
np
.
float32
,
fill_value
=
val
)
exe
=
fluid
.
Executor
(
place
=
fluid
.
CPUPlace
())
exe
=
fluid
.
Executor
(
place
=
fluid
.
CPUPlace
())
res_1
,
res_2
,
res_3
,
res_4
,
res_5
,
res_6
=
exe
.
run
(
res_1
,
res_2
,
res_3
,
res_4
,
res_5
,
res_6
,
res_7
=
exe
.
run
(
fluid
.
default_main_program
(),
fluid
.
default_main_program
(),
feed
=
{
feed
=
{
"shape_tensor_int32"
:
np
.
array
([
1
,
2
]).
astype
(
"int32"
),
"shape_tensor_int32"
:
np
.
array
([
1
,
2
]).
astype
(
"int32"
),
"shape_tensor_int64"
:
np
.
array
([
1
,
2
]).
astype
(
"int64"
),
"shape_tensor_int64"
:
np
.
array
([
1
,
2
]).
astype
(
"int64"
),
},
},
fetch_list
=
[
out_1
,
out_2
,
out_3
,
out_4
,
out_5
,
out_6
])
fetch_list
=
[
out_1
,
out_2
,
out_3
,
out_4
,
out_5
,
out_6
,
out_7
])
assert
np
.
array_equal
(
res_1
,
np
.
full
([
1
,
2
],
1.1
,
dtype
=
"float32"
))
assert
np
.
array_equal
(
res_1
,
np
.
full
([
1
,
2
],
1.1
,
dtype
=
"float32"
))
assert
np
.
array_equal
(
res_2
,
np
.
full
([
1
,
2
],
1.1
,
dtype
=
"float32"
))
assert
np
.
array_equal
(
res_2
,
np
.
full
([
1
,
2
],
1.1
,
dtype
=
"float32"
))
...
@@ -83,6 +87,7 @@ class TestFullAPI(unittest.TestCase):
...
@@ -83,6 +87,7 @@ class TestFullAPI(unittest.TestCase):
assert
np
.
array_equal
(
res_4
,
np
.
full
([
1
,
2
],
1.2
,
dtype
=
"float32"
))
assert
np
.
array_equal
(
res_4
,
np
.
full
([
1
,
2
],
1.2
,
dtype
=
"float32"
))
assert
np
.
array_equal
(
res_5
,
np
.
full
([
1
,
2
],
1.1
,
dtype
=
"float32"
))
assert
np
.
array_equal
(
res_5
,
np
.
full
([
1
,
2
],
1.1
,
dtype
=
"float32"
))
assert
np
.
array_equal
(
res_6
,
np
.
full
([
1
,
2
],
1.1
,
dtype
=
"float32"
))
assert
np
.
array_equal
(
res_6
,
np
.
full
([
1
,
2
],
1.1
,
dtype
=
"float32"
))
assert
np
.
array_equal
(
res_7
,
np
.
full
([
1
,
2
],
1.1
,
dtype
=
"float32"
))
class
TestFullOpError
(
unittest
.
TestCase
):
class
TestFullOpError
(
unittest
.
TestCase
):
...
@@ -90,8 +95,16 @@ class TestFullOpError(unittest.TestCase):
...
@@ -90,8 +95,16 @@ class TestFullOpError(unittest.TestCase):
with
program_guard
(
Program
(),
Program
()):
with
program_guard
(
Program
(),
Program
()):
#for ci coverage
#for ci coverage
x1
=
fluid
.
layers
.
data
(
name
=
'x1'
,
shape
=
[
1
],
dtype
=
"int16"
)
x1
=
fluid
.
layers
.
data
(
name
=
'x1'
,
shape
=
[
1
],
dtype
=
"int16"
)
x2
=
np
.
random
.
randn
(
1
,
2
).
astype
(
'int32'
)
self
.
assertRaises
(
self
.
assertRaises
(
ValueError
,
paddle
.
full
,
shape
=
[
1
],
fill_value
=
5
,
dtype
=
'uint4'
)
ValueError
,
paddle
.
full
,
shape
=
[
1
],
fill_value
=
5
,
dtype
=
'uint4'
)
self
.
assertRaises
(
TypeError
,
paddle
.
full
,
shape
=
[
1
],
fill_value
=
5
,
dtype
=
'int32'
,
out
=
x2
)
self
.
assertRaises
(
self
.
assertRaises
(
TypeError
,
TypeError
,
paddle
.
full
,
paddle
.
full
,
...
...
python/paddle/tensor/creation.py
浏览文件 @
b76f3b27
...
@@ -346,43 +346,57 @@ def full(shape,
...
@@ -346,43 +346,57 @@ def full(shape,
stop_gradient
=
True
,
stop_gradient
=
True
,
name
=
None
):
name
=
None
):
"""
"""
This
function
return a Tensor with the `fill_value` which size is same as `shape`
This
Op
return a Tensor with the `fill_value` which size is same as `shape`
Args:
Args:
shape(list|tuple|Variable): Shape of the Tensor to be created.
shape(list|tuple|Variable): Shape of the Tensor to be created.
The data type is ``int32`` or ``int64`` . If ``shape`` is a list or tuple,
The data type is ``int32`` or ``int64`` . If ``shape`` is a list or tuple,
the elements of it should be integers or Tensors with shape [1].
the elements of it should be integers or Tensors with shape [1].
If ``shape`` is an Variable, it should be an 1-D Tensor .
If ``shape`` is an Variable, it should be an 1-D Tensor .
value(float): The constant value used to initialize the Tensor to be created.
fill_value(bool|float16|float32|float64|int32|int64|Variable): The constant value
used to initialize the Tensor to be created. If fill_value is an Variable, it must be an 1-D Tensor.
out(Variable, optional): Optional output which can be any created
out(Variable, optional): Optional output which can be any created
Variable that meets the requirements to store the result of operation.
Variable that meets the requirements to store the result of operation.
if out is None, a new Varibale will be create to store the result.
if out is None, a new Varibale will be create to store the result.
dtype(np.dtype|core.VarDesc.VarType|str, optional): Data type of the output tensor
dtype(np.dtype|core.VarDesc.VarType|str, optional): Data type of the output tensor
which can be float16, float32, float64, int32, int64, if dytpe is `None`, the data
which can be float16, float32, float64, int32, int64, if dytpe is `None`, the data
type of created tensor is `float32`
type of created tensor is `float32`
device(str, optional): This parameter specifies that the Tensor is created
device(str, optional): On which device to run this Op. The :attr:`device` must be
on the GPU or CPU.
None, 'cpu' or 'gpu'. If :attr:`device` is None, the device that the user set in
the paddle program will be chosen. Default value is None.
stop_gradient(bool, optional): Indicating if we stop gradient from current(out) Variable,
stop_gradient(bool, optional): Indicating if we stop gradient from current(out) Variable,
default value is True.
default value is True.
name(str, optional): The default value is None. Normally there is no need for user to set this
name(str, optional): The default value is None. Normally there is no need for user to set this
property. For more information, please refer to :ref:`api_guide_Name`.
property. For more information, please refer to :ref:`api_guide_Name`.
Returns:
Variable: Tensor which is created according to shape and dtype.
Raises:
TypeError: The `dtype` must be one of None, bool, float16, float32, float64, int32 and int64.
TypeError: The `out` must be a Variable.
TypeError: The `shape` must be one of Variable, list tuple.
Examples:
Examples:
.. code-block:: python
.. code-block:: python
import paddle
import paddle
import paddle.fluid as fluid
import paddle.fluid as fluid
data1 = paddle.full(shape=[2,1], f
u
ll_value=0, dtype='int64') # data1=[[0],[0]]
data1 = paddle.full(shape=[2,1], f
i
ll_value=0, dtype='int64') # data1=[[0],[0]]
data2 = paddle.full(shape=[2,1], f
u
ll_value=5, dtype='int64', device='gpu') # data2=[[5],[5]]
data2 = paddle.full(shape=[2,1], f
i
ll_value=5, dtype='int64', device='gpu') # data2=[[5],[5]]
# attr shape is a list which contains Variable Tensor.
# attr shape is a list which contains Variable Tensor.
positive_2 = fluid.layers.fill_constant([1], "int32", 2)
positive_2 = fluid.layers.fill_constant([1], "int32", 2)
data3 = paddle.full(shape=[1, positive_2], dtype='float32', f
u
ll_value=1.5) # data3=[1.5, 1.5]
data3 = paddle.full(shape=[1, positive_2], dtype='float32', f
i
ll_value=1.5) # data3=[1.5, 1.5]
# attr shape is an Variable Tensor.
# attr shape is an Variable Tensor.
shape = fluid.layers.fill_constant([1,2], "int32", 2) # shape=[2,2]
shape = fluid.layers.fill_constant([1,2], "int32", 2) # shape=[2,2]
data4 = paddle.full(shape=shape, dtype='bool', full_value=True) # data4=[[True,True],[True,True]]
data4 = paddle.full(shape=shape, dtype='bool', fill_value=True) # data4=[[True,True],[True,True]]
# attr value is an Variable Tensor.
val = fluid.layers.fill_constant([1], "float32", 2.0) # val=[2.0]
data5 = paddle.full(shape=[2,1], fill_value=val, dtype='float32') #data5=[[2.0],[2.0]]
"""
"""
helper
=
LayerHelper
(
"full"
,
**
locals
())
helper
=
LayerHelper
(
"full"
,
**
locals
())
...
@@ -394,6 +408,8 @@ def full(shape,
...
@@ -394,6 +408,8 @@ def full(shape,
[
'bool'
,
'float16'
,
'float32'
,
'float64'
,
'int32'
,
'int64'
],
[
'bool'
,
'float16'
,
'float32'
,
'float64'
,
'int32'
,
'int64'
],
'full'
)
'full'
)
check_type
(
shape
,
'shape'
,
(
Variable
,
list
,
tuple
),
'full'
)
check_type
(
shape
,
'shape'
,
(
Variable
,
list
,
tuple
),
'full'
)
if
out
is
not
None
:
check_type
(
shape
,
'out'
,
(
Variable
),
'full'
)
if
out
is
None
:
if
out
is
None
:
out
=
helper
.
create_variable_for_type_inference
(
dtype
=
dtype
)
out
=
helper
.
create_variable_for_type_inference
(
dtype
=
dtype
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录