Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Crayon鑫
Paddle
提交
0dc485e6
P
Paddle
项目概览
Crayon鑫
/
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看板
未验证
提交
0dc485e6
编写于
8月 07, 2020
作者:
W
wangchaochaohu
提交者:
GitHub
8月 07, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
refine the value parameter's Tensor support of fill_constant Op test=… (#25986)
上级
168ea223
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
43 addition
and
7 deletion
+43
-7
python/paddle/fluid/layers/tensor.py
python/paddle/fluid/layers/tensor.py
+5
-2
python/paddle/fluid/tests/unittests/test_fill_constant_op.py
python/paddle/fluid/tests/unittests/test_fill_constant_op.py
+37
-4
python/paddle/tensor/creation.py
python/paddle/tensor/creation.py
+1
-1
未找到文件。
python/paddle/fluid/layers/tensor.py
浏览文件 @
0dc485e6
...
...
@@ -685,8 +685,9 @@ def fill_constant(shape, dtype, value, force_cpu=False, out=None, name=None):
"""
attrs
=
{
'force_cpu'
:
force_cpu
}
dtype
=
convert_dtype
(
dtype
)
if
not
isinstance
(
value
,
Variable
):
if
convert_dtype
(
dtype
)
in
[
'int64'
,
'int32'
]:
if
dtype
in
[
'int64'
,
'int32'
]:
attrs
[
'str_value'
]
=
str
(
int
(
value
))
else
:
attrs
[
'str_value'
]
=
str
(
float
(
value
))
...
...
@@ -697,7 +698,7 @@ def fill_constant(shape, dtype, value, force_cpu=False, out=None, name=None):
out
=
_varbase_creator
(
dtype
=
dtype
)
if
isinstance
(
value
,
Variable
):
if
convert_dtype
(
dtype
)
in
[
'int64'
,
'int32'
]:
if
dtype
in
[
'int64'
,
'int32'
]:
attrs
[
'str_value'
]
=
str
(
int
(
value
.
numpy
()))
else
:
attrs
[
'str_value'
]
=
str
(
float
(
value
.
numpy
()))
...
...
@@ -712,6 +713,8 @@ def fill_constant(shape, dtype, value, force_cpu=False, out=None, name=None):
helper
=
LayerHelper
(
"fill_constant"
,
**
locals
())
inputs
=
{}
if
isinstance
(
value
,
Variable
):
if
convert_dtype
(
value
.
dtype
)
!=
dtype
:
value
=
cast
(
value
,
dtype
)
inputs
[
'ValueTensor'
]
=
value
check_dtype
(
dtype
,
'dtype'
,
...
...
python/paddle/fluid/tests/unittests/test_fill_constant_op.py
浏览文件 @
0dc485e6
...
...
@@ -269,18 +269,26 @@ class TestFillConstantAPI(unittest.TestCase):
out_6
=
fluid
.
layers
.
fill_constant
(
shape
=
shape_tensor_int64
,
dtype
=
np
.
float32
,
value
=
1.1
)
val
=
fluid
.
layers
.
fill_constant
(
shape
=
[
1
],
dtype
=
np
.
float32
,
value
=
1.1
)
val1
=
fluid
.
layers
.
fill_constant
(
shape
=
[
1
],
dtype
=
np
.
float32
,
value
=
1.1
)
val2
=
fluid
.
layers
.
fill_constant
(
shape
=
[
1
],
dtype
=
np
.
float64
,
value
=
1.1
)
out_7
=
fluid
.
layers
.
fill_constant
(
shape
=
shape_tensor_int64
,
dtype
=
np
.
float32
,
value
=
val
)
shape
=
shape_tensor_int64
,
dtype
=
np
.
float32
,
value
=
val1
)
out_8
=
fluid
.
layers
.
fill_constant
(
shape
=
shape_tensor_int64
,
dtype
=
np
.
float32
,
value
=
val2
)
exe
=
fluid
.
Executor
(
place
=
fluid
.
CPUPlace
())
res_1
,
res_2
,
res_3
,
res_4
,
res_5
,
res_6
,
res_7
=
exe
.
run
(
res_1
,
res_2
,
res_3
,
res_4
,
res_5
,
res_6
,
res_7
,
res_8
=
exe
.
run
(
fluid
.
default_main_program
(),
feed
=
{
"shape_tensor_int32"
:
np
.
array
([
1
,
2
]).
astype
(
"int32"
),
"shape_tensor_int64"
:
np
.
array
([
1
,
2
]).
astype
(
"int64"
),
},
fetch_list
=
[
out_1
,
out_2
,
out_3
,
out_4
,
out_5
,
out_6
,
out_7
])
fetch_list
=
[
out_1
,
out_2
,
out_3
,
out_4
,
out_5
,
out_6
,
out_7
,
out_8
])
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"
))
...
...
@@ -289,6 +297,31 @@ class TestFillConstantAPI(unittest.TestCase):
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_7
,
np
.
full
([
1
,
2
],
1.1
,
dtype
=
"float32"
))
assert
np
.
array_equal
(
res_8
,
np
.
full
([
1
,
2
],
1.1
,
dtype
=
"float32"
))
class
TestFillConstantImperative
(
unittest
.
TestCase
):
def
test_api
(
self
):
with
fluid
.
dygraph
.
guard
():
data1
=
np
.
array
([
1
,
2
]).
astype
(
'int32'
)
data2
=
np
.
array
([
1.1
]).
astype
(
'float32'
)
shape
=
fluid
.
dygraph
.
to_variable
(
data1
)
val
=
fluid
.
dygraph
.
to_variable
(
data2
)
res1
=
fluid
.
layers
.
fill_constant
(
shape
=
[
1
,
2
],
dtype
=
'float32'
,
value
=
1.1
)
res2
=
fluid
.
layers
.
fill_constant
(
shape
=
shape
,
dtype
=
'float32'
,
value
=
1.1
)
res3
=
fluid
.
layers
.
fill_constant
(
shape
=
shape
,
dtype
=
'float32'
,
value
=
val
)
assert
np
.
array_equal
(
res1
.
numpy
(),
np
.
full
(
[
1
,
2
],
1.1
,
dtype
=
"float32"
))
assert
np
.
array_equal
(
res2
.
numpy
(),
np
.
full
(
[
1
,
2
],
1.1
,
dtype
=
"float32"
))
assert
np
.
array_equal
(
res3
.
numpy
(),
np
.
full
(
[
1
,
2
],
1.1
,
dtype
=
"float32"
))
class
TestFillConstantOpError
(
unittest
.
TestCase
):
...
...
python/paddle/tensor/creation.py
浏览文件 @
0dc485e6
...
...
@@ -248,7 +248,7 @@ def zeros(shape, dtype=None, name=None):
# shape is a Tensor
shape = paddle.fill_constant(shape=[2], dtype='int32', value=2)
data3 = paddle.
one
s(shape=shape, dtype='int32')
data3 = paddle.
zero
s(shape=shape, dtype='int32')
# [[0 0]
# [0 0]]
"""
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录