Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
82f0b5ea
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看板
未验证
提交
82f0b5ea
编写于
11月 17, 2020
作者:
L
littletomatodonkey
提交者:
GitHub
11月 17, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
adapt pad const (#28585)
* adapt pad const * fix comment and rm fluid import * rm stdout * fix note
上级
bf143652
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
36 addition
and
1 deletion
+36
-1
python/paddle/fluid/tests/unittests/test_pad3d_op.py
python/paddle/fluid/tests/unittests/test_pad3d_op.py
+30
-1
python/paddle/nn/functional/common.py
python/paddle/nn/functional/common.py
+6
-0
未找到文件。
python/paddle/fluid/tests/unittests/test_pad3d_op.py
浏览文件 @
82f0b5ea
...
@@ -251,7 +251,9 @@ class TestPadAPI(unittest.TestCase):
...
@@ -251,7 +251,9 @@ class TestPadAPI(unittest.TestCase):
mode
,
mode
,
value
=
0
,
value
=
0
,
data_format
=
"NCDHW"
):
data_format
=
"NCDHW"
):
if
data_format
==
"NCDHW"
:
if
mode
==
"constant"
and
len
(
pad
)
==
len
(
input_data
.
shape
)
*
2
:
pad
=
np
.
reshape
(
pad
,
(
-
1
,
2
)).
tolist
()
elif
data_format
==
"NCDHW"
:
pad
=
[
pad
=
[
(
0
,
0
),
(
0
,
0
),
(
0
,
0
),
(
0
,
0
),
...
@@ -316,6 +318,7 @@ class TestPadAPI(unittest.TestCase):
...
@@ -316,6 +318,7 @@ class TestPadAPI(unittest.TestCase):
paddle
.
disable_static
()
paddle
.
disable_static
()
input_shape
=
(
1
,
2
,
3
,
4
,
5
)
input_shape
=
(
1
,
2
,
3
,
4
,
5
)
pad
=
[
1
,
2
,
1
,
1
,
3
,
4
]
pad
=
[
1
,
2
,
1
,
1
,
3
,
4
]
pad_3
=
[
1
,
2
,
1
,
1
,
3
,
4
,
5
,
6
,
7
,
8
]
mode
=
"constant"
mode
=
"constant"
value
=
100
value
=
100
input_data
=
np
.
random
.
rand
(
*
input_shape
).
astype
(
np
.
float32
)
input_data
=
np
.
random
.
rand
(
*
input_shape
).
astype
(
np
.
float32
)
...
@@ -323,6 +326,8 @@ class TestPadAPI(unittest.TestCase):
...
@@ -323,6 +326,8 @@ class TestPadAPI(unittest.TestCase):
input_data
,
pad
,
mode
,
value
,
data_format
=
"NCDHW"
)
input_data
,
pad
,
mode
,
value
,
data_format
=
"NCDHW"
)
np_out2
=
self
.
_get_numpy_out
(
np_out2
=
self
.
_get_numpy_out
(
input_data
,
pad
,
mode
,
value
,
data_format
=
"NDHWC"
)
input_data
,
pad
,
mode
,
value
,
data_format
=
"NDHWC"
)
np_out3
=
self
.
_get_numpy_out
(
input_data
,
pad_3
,
mode
,
value
,
data_format
=
"NCDHW"
)
tensor_data
=
paddle
.
to_tensor
(
input_data
)
tensor_data
=
paddle
.
to_tensor
(
input_data
)
y1
=
F
.
pad
(
tensor_data
,
y1
=
F
.
pad
(
tensor_data
,
...
@@ -335,14 +340,21 @@ class TestPadAPI(unittest.TestCase):
...
@@ -335,14 +340,21 @@ class TestPadAPI(unittest.TestCase):
mode
=
mode
,
mode
=
mode
,
value
=
value
,
value
=
value
,
data_format
=
"NDHWC"
)
data_format
=
"NDHWC"
)
y3
=
F
.
pad
(
tensor_data
,
pad
=
pad_3
,
mode
=
mode
,
value
=
value
,
data_format
=
"NCDHW"
)
self
.
assertTrue
(
np
.
allclose
(
y1
.
numpy
(),
np_out1
))
self
.
assertTrue
(
np
.
allclose
(
y1
.
numpy
(),
np_out1
))
self
.
assertTrue
(
np
.
allclose
(
y2
.
numpy
(),
np_out2
))
self
.
assertTrue
(
np
.
allclose
(
y2
.
numpy
(),
np_out2
))
self
.
assertTrue
(
np
.
allclose
(
y3
.
numpy
(),
np_out3
))
def
test_dygraph_2
(
self
):
def
test_dygraph_2
(
self
):
paddle
.
disable_static
()
paddle
.
disable_static
()
input_shape
=
(
2
,
3
,
4
,
5
)
input_shape
=
(
2
,
3
,
4
,
5
)
pad
=
[
1
,
1
,
3
,
4
]
pad
=
[
1
,
1
,
3
,
4
]
pad_3
=
[
1
,
2
,
1
,
1
,
3
,
4
,
5
,
6
]
mode
=
"constant"
mode
=
"constant"
value
=
100
value
=
100
input_data
=
np
.
random
.
rand
(
*
input_shape
).
astype
(
np
.
float32
)
input_data
=
np
.
random
.
rand
(
*
input_shape
).
astype
(
np
.
float32
)
...
@@ -350,6 +362,8 @@ class TestPadAPI(unittest.TestCase):
...
@@ -350,6 +362,8 @@ class TestPadAPI(unittest.TestCase):
input_data
,
pad
,
mode
,
value
,
data_format
=
"NCHW"
)
input_data
,
pad
,
mode
,
value
,
data_format
=
"NCHW"
)
np_out2
=
self
.
_get_numpy_out
(
np_out2
=
self
.
_get_numpy_out
(
input_data
,
pad
,
mode
,
value
,
data_format
=
"NHWC"
)
input_data
,
pad
,
mode
,
value
,
data_format
=
"NHWC"
)
np_out3
=
self
.
_get_numpy_out
(
input_data
,
pad_3
,
mode
,
value
,
data_format
=
"NCHW"
)
tensor_data
=
paddle
.
to_tensor
(
input_data
)
tensor_data
=
paddle
.
to_tensor
(
input_data
)
tensor_pad
=
paddle
.
to_tensor
(
pad
,
dtype
=
"int32"
)
tensor_pad
=
paddle
.
to_tensor
(
pad
,
dtype
=
"int32"
)
...
@@ -364,14 +378,21 @@ class TestPadAPI(unittest.TestCase):
...
@@ -364,14 +378,21 @@ class TestPadAPI(unittest.TestCase):
mode
=
mode
,
mode
=
mode
,
value
=
value
,
value
=
value
,
data_format
=
"NHWC"
)
data_format
=
"NHWC"
)
y3
=
F
.
pad
(
tensor_data
,
pad
=
pad_3
,
mode
=
mode
,
value
=
value
,
data_format
=
"NCHW"
)
self
.
assertTrue
(
np
.
allclose
(
y1
.
numpy
(),
np_out1
))
self
.
assertTrue
(
np
.
allclose
(
y1
.
numpy
(),
np_out1
))
self
.
assertTrue
(
np
.
allclose
(
y2
.
numpy
(),
np_out2
))
self
.
assertTrue
(
np
.
allclose
(
y2
.
numpy
(),
np_out2
))
self
.
assertTrue
(
np
.
allclose
(
y3
.
numpy
(),
np_out3
))
def
test_dygraph_3
(
self
):
def
test_dygraph_3
(
self
):
paddle
.
disable_static
()
paddle
.
disable_static
()
input_shape
=
(
3
,
4
,
5
)
input_shape
=
(
3
,
4
,
5
)
pad
=
[
3
,
4
]
pad
=
[
3
,
4
]
pad_3
=
[
3
,
4
,
5
,
6
,
7
,
8
]
mode
=
"constant"
mode
=
"constant"
value
=
100
value
=
100
input_data
=
np
.
random
.
rand
(
*
input_shape
).
astype
(
np
.
float32
)
input_data
=
np
.
random
.
rand
(
*
input_shape
).
astype
(
np
.
float32
)
...
@@ -379,6 +400,8 @@ class TestPadAPI(unittest.TestCase):
...
@@ -379,6 +400,8 @@ class TestPadAPI(unittest.TestCase):
input_data
,
pad
,
mode
,
value
,
data_format
=
"NCL"
)
input_data
,
pad
,
mode
,
value
,
data_format
=
"NCL"
)
np_out2
=
self
.
_get_numpy_out
(
np_out2
=
self
.
_get_numpy_out
(
input_data
,
pad
,
mode
,
value
,
data_format
=
"NLC"
)
input_data
,
pad
,
mode
,
value
,
data_format
=
"NLC"
)
np_out3
=
self
.
_get_numpy_out
(
input_data
,
pad_3
,
mode
,
value
,
data_format
=
"NCL"
)
tensor_data
=
paddle
.
to_tensor
(
input_data
)
tensor_data
=
paddle
.
to_tensor
(
input_data
)
tensor_pad
=
paddle
.
to_tensor
(
pad
,
dtype
=
"int32"
)
tensor_pad
=
paddle
.
to_tensor
(
pad
,
dtype
=
"int32"
)
...
@@ -392,9 +415,15 @@ class TestPadAPI(unittest.TestCase):
...
@@ -392,9 +415,15 @@ class TestPadAPI(unittest.TestCase):
mode
=
mode
,
mode
=
mode
,
value
=
value
,
value
=
value
,
data_format
=
"NLC"
)
data_format
=
"NLC"
)
y3
=
F
.
pad
(
tensor_data
,
pad
=
pad_3
,
mode
=
mode
,
value
=
value
,
data_format
=
"NCL"
)
self
.
assertTrue
(
np
.
allclose
(
y1
.
numpy
(),
np_out1
))
self
.
assertTrue
(
np
.
allclose
(
y1
.
numpy
(),
np_out1
))
self
.
assertTrue
(
np
.
allclose
(
y2
.
numpy
(),
np_out2
))
self
.
assertTrue
(
np
.
allclose
(
y2
.
numpy
(),
np_out2
))
self
.
assertTrue
(
np
.
allclose
(
y3
.
numpy
(),
np_out3
))
class
TestPad1dAPI
(
unittest
.
TestCase
):
class
TestPad1dAPI
(
unittest
.
TestCase
):
...
...
python/paddle/nn/functional/common.py
浏览文件 @
82f0b5ea
...
@@ -1158,6 +1158,9 @@ def alpha_dropout(x, p=0.5, training=True, name=None):
...
@@ -1158,6 +1158,9 @@ def alpha_dropout(x, p=0.5, training=True, name=None):
def
pad
(
x
,
pad
,
mode
=
'constant'
,
value
=
0
,
data_format
=
"NCHW"
,
name
=
None
):
def
pad
(
x
,
pad
,
mode
=
'constant'
,
value
=
0
,
data_format
=
"NCHW"
,
name
=
None
):
"""
"""
Pad tensor according to 'pad' and 'mode'.
Pad tensor according to 'pad' and 'mode'.
If mode is 'constant' and length of pad is twice as length of x dimension,
then the padding will be started from the first dimension and moved back onto x
according to 'pad' and 'value'.
If mode is 'reflect', pad[0] and pad[1] must be no greater
If mode is 'reflect', pad[0] and pad[1] must be no greater
than width-1. The height and depth dimension has the same condition.
than width-1. The height and depth dimension has the same condition.
...
@@ -1273,6 +1276,9 @@ def pad(x, pad, mode='constant', value=0, data_format="NCHW", name=None):
...
@@ -1273,6 +1276,9 @@ def pad(x, pad, mode='constant', value=0, data_format="NCHW", name=None):
unsqueezed_dim
=
[]
unsqueezed_dim
=
[]
if
mode
==
"constant"
and
isinstance
(
pad
,
list
)
and
len
(
pad
)
==
x_dim
*
2
:
return
layers
.
pad
(
x
,
pad
,
pad_value
=
value
)
if
isinstance
(
pad
,
Variable
):
if
isinstance
(
pad
,
Variable
):
if
data_format
in
[
"NCL"
,
"NCHW"
,
"NCDHW"
]:
if
data_format
in
[
"NCL"
,
"NCHW"
,
"NCDHW"
]:
data_format
=
"NCDHW"
data_format
=
"NCDHW"
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录