Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
d6ee0868
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看板
未验证
提交
d6ee0868
编写于
9月 08, 2020
作者:
L
Leo Chen
提交者:
GitHub
9月 08, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix unsqueeze in dygraph (#27107)
* fix unsqueeze in dygraph * add test * add test
上级
58f3ef98
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
47 addition
and
9 deletion
+47
-9
python/paddle/fluid/layers/nn.py
python/paddle/fluid/layers/nn.py
+9
-0
python/paddle/fluid/tests/unittests/test_unsqueeze_op.py
python/paddle/fluid/tests/unittests/test_unsqueeze_op.py
+38
-7
python/paddle/tensor/manipulation.py
python/paddle/tensor/manipulation.py
+0
-2
未找到文件。
python/paddle/fluid/layers/nn.py
浏览文件 @
d6ee0868
...
@@ -6306,6 +6306,15 @@ def unsqueeze(input, axes, name=None):
...
@@ -6306,6 +6306,15 @@ def unsqueeze(input, axes, name=None):
"""
"""
if in_dygraph_mode():
if in_dygraph_mode():
if isinstance(axes, int):
axes = [axes]
elif isinstance(axes, Variable):
axes = [axes.numpy().item(0)]
elif isinstance(axes, (list, tuple)):
axes = [
item.numpy().item(0) if isinstance(item, Variable) else item
for item in axes
]
out, _ = core.ops.unsqueeze2(input, 'axes', axes)
out, _ = core.ops.unsqueeze2(input, 'axes', axes)
return out
return out
...
...
python/paddle/fluid/tests/unittests/test_unsqueeze_op.py
浏览文件 @
d6ee0868
...
@@ -134,29 +134,60 @@ class API_TestUnsqueeze3(unittest.TestCase):
...
@@ -134,29 +134,60 @@ class API_TestUnsqueeze3(unittest.TestCase):
result1
,
=
exe
.
run
(
feed
=
{
"data1"
:
input
,
result1
,
=
exe
.
run
(
feed
=
{
"data1"
:
input
,
"data2"
:
input2
},
"data2"
:
input2
},
fetch_list
=
[
result_squeeze
])
fetch_list
=
[
result_squeeze
])
self
.
assertTrue
(
np
.
allclose
(
input1
,
result1
))
self
.
assertTrue
(
np
.
array_equal
(
input1
,
result1
))
self
.
assertEqual
(
input1
.
shape
,
result1
.
shape
)
class
API_TestDyUnsqueeze
(
unittest
.
TestCase
):
class
API_TestDyUnsqueeze
(
unittest
.
TestCase
):
def
test_out
(
self
):
def
test_out
(
self
):
with
fluid
.
dygraph
.
guard
():
with
fluid
.
dygraph
.
guard
():
input_1
=
np
.
random
.
random
([
5
,
1
,
10
]).
astype
(
"int32"
)
input_1
=
np
.
random
.
random
([
5
,
1
,
10
]).
astype
(
"int32"
)
input1
=
np
.
squeeze
(
input_1
,
axis
=
1
)
input1
=
np
.
expand_dims
(
input_1
,
axis
=
1
)
input
=
fluid
.
dygraph
.
to_variable
(
input_1
)
input
=
fluid
.
dygraph
.
to_variable
(
input_1
)
output
=
paddle
.
unsqueeze
(
input
,
axis
=
[
1
])
output
=
paddle
.
unsqueeze
(
input
,
axis
=
[
1
])
out_np
=
output
.
numpy
()
out_np
=
output
.
numpy
()
self
.
assertTrue
(
np
.
allclose
(
input1
,
out_np
))
self
.
assertTrue
(
np
.
array_equal
(
input1
,
out_np
))
self
.
assertEqual
(
input1
.
shape
,
out_np
.
shape
)
class
API_TestDyUnsqueeze2
(
unittest
.
TestCase
):
class
API_TestDyUnsqueeze2
(
unittest
.
TestCase
):
def
test_out
(
self
):
def
test_out
(
self
):
with
fluid
.
dygraph
.
guard
():
with
fluid
.
dygraph
.
guard
():
input
_1
=
np
.
random
.
random
([
5
,
1
,
10
]).
astype
(
"int32"
)
input
1
=
np
.
random
.
random
([
5
,
10
]).
astype
(
"int32"
)
input1
=
np
.
squeeze
(
input_
1
,
axis
=
1
)
out1
=
np
.
expand_dims
(
input
1
,
axis
=
1
)
input
=
fluid
.
dygraph
.
to_variable
(
input
_
1
)
input
=
fluid
.
dygraph
.
to_variable
(
input1
)
output
=
paddle
.
unsqueeze
(
input
,
axis
=
1
)
output
=
paddle
.
unsqueeze
(
input
,
axis
=
1
)
out_np
=
output
.
numpy
()
out_np
=
output
.
numpy
()
self
.
assertTrue
(
np
.
allclose
(
input1
,
out_np
))
self
.
assertTrue
(
np
.
array_equal
(
out1
,
out_np
))
self
.
assertEqual
(
out1
.
shape
,
out_np
.
shape
)
class
API_TestDyUnsqueezeAxisTensor
(
unittest
.
TestCase
):
def
test_out
(
self
):
with
fluid
.
dygraph
.
guard
():
input1
=
np
.
random
.
random
([
5
,
10
]).
astype
(
"int32"
)
out1
=
np
.
expand_dims
(
input1
,
axis
=
1
)
input
=
fluid
.
dygraph
.
to_variable
(
input1
)
output
=
paddle
.
unsqueeze
(
input
,
axis
=
paddle
.
to_tensor
([
1
]))
out_np
=
output
.
numpy
()
self
.
assertTrue
(
np
.
array_equal
(
out1
,
out_np
))
self
.
assertEqual
(
out1
.
shape
,
out_np
.
shape
)
class
API_TestDyUnsqueezeAxisTensorList
(
unittest
.
TestCase
):
def
test_out
(
self
):
with
fluid
.
dygraph
.
guard
():
input1
=
np
.
random
.
random
([
5
,
10
]).
astype
(
"int32"
)
# Actually, expand_dims supports tuple since version 1.18.0
out1
=
np
.
expand_dims
(
input1
,
axis
=
1
)
out1
=
np
.
expand_dims
(
out1
,
axis
=
2
)
input
=
fluid
.
dygraph
.
to_variable
(
input1
)
output
=
paddle
.
unsqueeze
(
fluid
.
dygraph
.
to_variable
(
input1
),
axis
=
[
paddle
.
to_tensor
([
1
]),
paddle
.
to_tensor
([
2
])])
out_np
=
output
.
numpy
()
self
.
assertTrue
(
np
.
array_equal
(
out1
,
out_np
))
self
.
assertEqual
(
out1
.
shape
,
out_np
.
shape
)
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
...
...
python/paddle/tensor/manipulation.py
浏览文件 @
d6ee0868
...
@@ -746,8 +746,6 @@ def unsqueeze(x, axis, name=None):
...
@@ -746,8 +746,6 @@ def unsqueeze(x, axis, name=None):
print(out3.shape) # [1, 1, 1, 5, 10]
print(out3.shape) # [1, 1, 1, 5, 10]
"""
"""
if
isinstance
(
axis
,
int
):
axis
=
[
axis
]
return
layers
.
unsqueeze
(
x
,
axis
,
name
)
return
layers
.
unsqueeze
(
x
,
axis
,
name
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录