Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
橘蓝
Paddle
提交
dd64349a
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看板
体验新版 GitCode,发现更多精彩内容 >>
提交
dd64349a
编写于
9月 11, 2017
作者:
Y
Yibing Liu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
refine reshape operator
上级
02da0d1b
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
27 addition
and
14 deletion
+27
-14
paddle/operators/reshape_op.cc
paddle/operators/reshape_op.cc
+9
-6
paddle/operators/reshape_op.h
paddle/operators/reshape_op.h
+4
-6
python/paddle/v2/framework/tests/test_reshape_op.py
python/paddle/v2/framework/tests/test_reshape_op.py
+14
-2
未找到文件。
paddle/operators/reshape_op.cc
浏览文件 @
dd64349a
...
...
@@ -29,14 +29,17 @@ class ReshapeOp : public framework::OperatorWithKernel {
void
InferShape
(
const
framework
::
InferShapeContext
&
ctx
)
const
override
{
auto
*
in
=
ctx
.
Input
<
framework
::
Tensor
>
(
"X"
);
auto
shape
=
ctx
.
Attr
<
std
::
vector
<
int
>>
(
"shape"
);
PADDLE_ENFORCE_EQ
((
unsigned
)
shape
.
size
(),
in
->
dims
().
size
(),
"The dimension of Input(X) mismatches with Attr(shape)."
);
size_t
shape_size
=
1
;
int64_t
capacity
=
-
1
;
for
(
auto
dim
:
shape
)
{
shape_size
*=
dim
;
PADDLE_ENFORCE
(
dim
>
0
,
"Each dimension of shape must be positive."
);
if
(
capacity
<
0
)
{
capacity
=
dim
;
}
else
{
capacity
*=
dim
;
}
}
size
_t
in_size
=
framework
::
product
(
in
->
dims
());
PADDLE_ENFORCE_EQ
(
shape_size
,
in_size
,
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
());
}
...
...
paddle/operators/reshape_op.h
浏览文件 @
dd64349a
...
...
@@ -21,14 +21,12 @@
namespace
paddle
{
namespace
operators
{
using
Tensor
=
framework
::
Tensor
;
template
<
typename
Place
,
typename
T
>
class
ReshapeKernel
:
public
framework
::
OpKernel
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
{
auto
*
out
=
ctx
.
Output
<
Tensor
>
(
"Out"
);
auto
*
in
=
ctx
.
Input
<
Tensor
>
(
"X"
);
auto
*
out
=
ctx
.
Output
<
framework
::
Tensor
>
(
"Out"
);
auto
*
in
=
ctx
.
Input
<
framework
::
Tensor
>
(
"X"
);
out
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
auto
shape
=
ctx
.
Attr
<
std
::
vector
<
int
>>
(
"shape"
);
...
...
@@ -46,8 +44,8 @@ template <typename Place, typename T>
class
ReshapeGradKernel
:
public
framework
::
OpKernel
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
{
auto
*
d_out
=
ctx
.
Input
<
Tensor
>
(
framework
::
GradVarName
(
"Out"
));
auto
*
d_x
=
ctx
.
Output
<
Tensor
>
(
framework
::
GradVarName
(
"X"
));
auto
*
d_out
=
ctx
.
Input
<
framework
::
Tensor
>
(
framework
::
GradVarName
(
"Out"
));
auto
*
d_x
=
ctx
.
Output
<
framework
::
Tensor
>
(
framework
::
GradVarName
(
"X"
));
d_x
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
auto
in_dims
=
d_x
->
dims
();
...
...
python/paddle/v2/framework/tests/test_reshape_op.py
浏览文件 @
dd64349a
...
...
@@ -10,15 +10,27 @@ class TestReshapeOp(unittest.TestCase):
def
setUp
(
self
):
self
.
type
=
"reshape"
self
.
inputs
=
{
'X'
:
np
.
random
.
random
((
37
,
51
)).
astype
(
"float32"
),
}
self
.
attrs
=
{
'shape'
:
[
51
,
37
]}
self
.
attrs
=
{
'shape'
:
[
51
*
37
]}
self
.
outputs
=
{
'Out'
:
self
.
inputs
[
'X'
].
reshape
(
self
.
attrs
[
'shape'
])}
class
ReshapeGradOpTest
(
GradientChecker
):
class
TestReshapeGradOp
(
GradientChecker
):
"""
def test_normal(self):
op = Operator("reshape", X='X', Out='Out', shape=[5, 40])
inputs = {"X": np.random.random((10, 20)).astype("float32")}
self.check_grad(op, inputs, set("X"), "Out")
"""
def
setUp
(
self
):
self
.
op
=
Operator
(
"reshape"
,
X
=
'X'
,
Out
=
'Out'
,
shape
=
[
5
,
40
])
self
.
inputs
=
{
"X"
:
np
.
random
.
random
((
10
,
20
)).
astype
(
"float32"
)}
def
test_normal
(
self
):
self
.
check_grad
(
self
.
op
,
self
.
inputs
,
[
"X"
],
"Out"
)
def
test_dev_compare
(
self
):
self
.
compare_grad
(
self
.
op
,
self
.
inputs
)
if
__name__
==
'__main__'
:
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录