Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
5f89272c
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看板
提交
5f89272c
编写于
7月 09, 2018
作者:
C
chenweihang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
change the bit insert to array insert for understandability
上级
fccdc1ab
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
31 addition
and
34 deletion
+31
-34
paddle/fluid/operators/unsqueeze_op.cc
paddle/fluid/operators/unsqueeze_op.cc
+23
-34
python/paddle/fluid/tests/unittests/test_unsqueeze_op.py
python/paddle/fluid/tests/unittests/test_unsqueeze_op.py
+8
-0
未找到文件。
paddle/fluid/operators/unsqueeze_op.cc
浏览文件 @
5f89272c
...
...
@@ -44,39 +44,37 @@ class UnsqueezeOpInferShape : public framework::InferShapeBase {
static
framework
::
DDim
GetOutputShape
(
const
std
::
vector
<
int
>
unsqz_dims
,
const
framework
::
DDim
&
in_dims
)
{
unsigned
int
unsqz_mask
=
0
;
unsigned
int
front
=
0
,
back
=
0
;
int
output_dims_size
=
in_dims
.
size
();
int
output_size
=
in_dims
.
size
()
+
unsqz_dims
.
size
();
int
cur_output_size
=
in_dims
.
size
();
std
::
vector
<
int64_t
>
output_shape
(
output_size
,
0
);
// Validity Check: rank range.
PADDLE_ENFORCE
(
output_size
<=
6
,
"The output tensor's rank should be less than 6."
);
// Simulate insert by bit calc.
for
(
int
axis
:
unsqz_dims
)
{
int
cur
=
axis
<
0
?
axis
+
output_dims
_size
+
1
:
axis
;
int
cur
=
axis
<
0
?
axis
+
cur_output
_size
+
1
:
axis
;
// Vaildity Check: the axis bound
PADDLE_ENFORCE
(
cur
>=
0
&&
cur
<=
output_dims
_size
,
cur
>=
0
&&
cur
<=
cur_output
_size
,
"The unsqueeze dims must be within range of current rank."
);
// Save the front part.
front
=
unsqz_mask
&
((
1
<<
cur
)
-
1
);
// Move the back part.
back
=
unsqz_mask
&
~
((
1
<<
cur
)
-
1
);
back
<<=
1
;
// Merge two part.
back
|=
(
1
<<
cur
);
unsqz_mask
=
front
|
back
;
// Move old axis, and insert new axis
for
(
int
i
=
cur_output_size
;
i
>=
cur
;
--
i
)
{
if
(
output_shape
[
i
]
==
1
)
{
// Move axis
output_shape
[
i
+
1
]
=
1
;
output_shape
[
i
]
=
0
;
}
}
output_shape
[
cur
]
=
1
;
// Add the output size.
output_dims_size
++
;
// Validity Check: rank range.
PADDLE_ENFORCE
(
output_dims_size
<=
6
,
"The output tensor's rank should be less than 6."
);
cur_output_size
++
;
}
// Make output shape
std
::
vector
<
int64_t
>
output_shape
(
output_dims_size
,
0
);
for
(
int
in_idx
=
0
,
out_idx
=
0
;
out_idx
<
output_dims_size
;
++
out_idx
)
{
if
((
unsqz_mask
&
(
1
<<
out_idx
))
==
0
)
{
for
(
int
in_idx
=
0
,
out_idx
=
0
;
out_idx
<
output_size
;
++
out_idx
)
{
if
(
output_shape
[
out_idx
]
==
0
)
{
output_shape
[
out_idx
]
=
in_dims
[
in_idx
++
];
}
else
{
output_shape
[
out_idx
]
=
1
;
}
}
...
...
@@ -86,10 +84,7 @@ class UnsqueezeOpInferShape : public framework::InferShapeBase {
class
UnsqueezeOp
:
public
framework
::
OperatorBase
{
public:
UnsqueezeOp
(
const
std
::
string
&
type
,
const
framework
::
VariableNameMap
&
inputs
,
const
framework
::
VariableNameMap
&
outputs
,
const
framework
::
AttributeMap
&
attrs
)
:
OperatorBase
(
type
,
inputs
,
outputs
,
attrs
)
{}
using
OperatorBase
::
OperatorBase
;
private:
void
RunImpl
(
const
framework
::
Scope
&
scope
,
...
...
@@ -97,8 +92,6 @@ class UnsqueezeOp : public framework::OperatorBase {
auto
&
axes
=
Attr
<
std
::
vector
<
int
>>
(
"axes"
);
auto
x_dims
=
scope
.
FindVar
(
Input
(
"X"
))
->
Get
<
framework
::
LoDTensor
>
().
dims
();
auto
out_dims
=
UnsqueezeOpInferShape
::
GetOutputShape
(
axes
,
x_dims
);
// auto out_dims =
// scope.FindVar(Output("Out"))->Get<framework::LoDTensor>().dims();
framework
::
AttributeMap
attrs
;
attrs
[
"shape"
]
=
framework
::
vectorize2int
(
out_dims
);
...
...
@@ -165,11 +158,7 @@ class UnsqueezeGradInferShape : public framework::InferShapeBase {
class
UnsqueezeGradOp
:
public
framework
::
OperatorBase
{
public:
UnsqueezeGradOp
(
const
std
::
string
&
type
,
const
framework
::
VariableNameMap
&
inputs
,
const
framework
::
VariableNameMap
&
outputs
,
const
framework
::
AttributeMap
&
attrs
)
:
OperatorBase
(
type
,
inputs
,
outputs
,
attrs
)
{}
using
OperatorBase
::
OperatorBase
;
private:
void
RunImpl
(
const
framework
::
Scope
&
scope
,
...
...
python/paddle/fluid/tests/unittests/test_unsqueeze_op.py
浏览文件 @
5f89272c
...
...
@@ -66,6 +66,14 @@ class TestUnsqueezeOp3(TestUnsqueezeOp):
self
.
new_shape
=
(
1
,
3
,
2
,
1
,
1
,
5
)
# Correct: Reversed axes.
class
TestUnsqueezeOp4
(
TestUnsqueezeOp
):
def
init_test_case
(
self
):
self
.
ori_shape
=
(
3
,
2
,
5
)
self
.
axes
=
(
3
,
1
,
1
)
self
.
new_shape
=
(
3
,
1
,
1
,
2
,
5
,
1
)
# Correct: Inplace.
class
TestUnsqueezeOpInplace1
(
TestUnsqueezeOp
):
def
init_test_case
(
self
):
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录