Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
0289a009
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看板
提交
0289a009
编写于
9月 11, 2017
作者:
Y
Yibing Liu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
follow comments to cleanup code
上级
dd926498
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
31 addition
and
13 deletion
+31
-13
paddle/operators/reshape_op.cc
paddle/operators/reshape_op.cc
+27
-8
paddle/operators/reshape_op.h
paddle/operators/reshape_op.h
+4
-5
未找到文件。
paddle/operators/reshape_op.cc
浏览文件 @
0289a009
...
...
@@ -27,21 +27,26 @@ class ReshapeOp : public framework::OperatorWithKernel {
protected:
void
InferShape
(
const
framework
::
InferShapeContext
&
ctx
)
const
override
{
auto
*
in
=
ctx
.
Input
<
framework
::
Tensor
>
(
"X"
);
// input check
PADDLE_ENFORCE_NOT_NULL
(
ctx
.
InputVar
(
"X"
),
"Input(X) shouldn't be null"
);
auto
shape
=
ctx
.
Attr
<
std
::
vector
<
int
>>
(
"shape"
);
int64_t
capacity
=
-
1
;
PADDLE_ENFORCE
(
shape
.
size
()
>
0
,
"Attr(shape) shouldn't be empty."
)
;
for
(
auto
dim
:
shape
)
{
PADDLE_ENFORCE
(
dim
>
0
,
"Each dimension of shape must be positive."
);
if
(
capacity
<
0
)
{
capacity
=
dim
;
}
else
{
capacity
*=
dim
;
}
}
// capacity check
int64_t
capacity
=
std
::
accumulate
(
shape
.
begin
(),
shape
.
end
(),
1
,
std
::
multiplies
<
int
>
());
auto
*
in
=
ctx
.
Input
<
framework
::
Tensor
>
(
"X"
);
int64_t
in_size
=
framework
::
product
(
in
->
dims
());
PADDLE_ENFORCE_EQ
(
capacity
,
in_size
,
"The size of Input(X) mismatches with Attr(shape)."
);
ctx
.
Output
<
framework
::
Tensor
>
(
"Out"
)
->
Resize
(
in
->
dims
());
// resize output
std
::
vector
<
int64_t
>
shape_int64
(
shape
.
size
(),
0
);
std
::
transform
(
shape
.
begin
(),
shape
.
end
(),
shape_int64
.
begin
(),
[](
int
a
)
{
return
static_cast
<
int64_t
>
(
a
);
});
auto
out_dims
=
framework
::
make_ddim
(
shape_int64
);
ctx
.
Output
<
framework
::
Tensor
>
(
"Out"
)
->
Resize
(
out_dims
);
}
};
...
...
@@ -56,6 +61,17 @@ class ReshapeOpMaker : public framework::OpProtoAndCheckerMaker {
AddComment
(
R"DOC(Reshape operator
Reshape Input(X) into the shape specified by Attr(shape).
An example:
Given a 2-D tensor X with 2 rows and 2 columns
[[1, 2], [3, 4]]
with target shape = [1, 4], the reshape operator will tansform
the tensor X into a 1-D tensor:
[1, 2, 3, 4]
)DOC"
);
}
};
...
...
@@ -70,6 +86,9 @@ class ReshapeGradOp : public framework::OperatorWithKernel {
protected:
void
InferShape
(
const
framework
::
InferShapeContext
&
ctx
)
const
override
{
PADDLE_ENFORCE_NOT_NULL
(
ctx
.
InputVar
(
"X"
),
"Input(X) shouldn't be null."
);
PADDLE_ENFORCE_NOT_NULL
(
ctx
.
InputVar
(
framework
::
GradVarName
(
"Out"
)),
"Input(Out@GRAD) shouldn't be null."
);
auto
dims
=
ctx
.
Input
<
framework
::
Tensor
>
(
"X"
)
->
dims
();
auto
*
d_in
=
ctx
.
Output
<
framework
::
Tensor
>
(
framework
::
GradVarName
(
"X"
));
d_in
->
Resize
(
dims
);
...
...
paddle/operators/reshape_op.h
浏览文件 @
0289a009
...
...
@@ -30,11 +30,10 @@ class ReshapeKernel : public framework::OpKernel {
out
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
auto
shape
=
ctx
.
Attr
<
std
::
vector
<
int
>>
(
"shape"
);
std
::
vector
<
int64_t
>
tmp
;
for
(
auto
dim
:
shape
)
{
tmp
.
push_back
(
dim
);
}
auto
out_dims
=
framework
::
make_ddim
(
tmp
);
std
::
vector
<
int64_t
>
shape_int64
(
shape
.
size
(),
0
);
std
::
transform
(
shape
.
begin
(),
shape
.
end
(),
shape_int64
.
begin
(),
[](
int
a
)
{
return
static_cast
<
int64_t
>
(
a
);
});
auto
out_dims
=
framework
::
make_ddim
(
shape_int64
);
out
->
CopyFrom
<
T
>
(
*
in
,
ctx
.
GetPlace
());
out
->
Resize
(
out_dims
);
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录