Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
899c7d6b
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看板
提交
899c7d6b
编写于
9月 07, 2017
作者:
Y
Yibing Liu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
pass unit test
上级
12eaa22a
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
11 addition
and
14 deletion
+11
-14
paddle/operators/reshape_op.cc
paddle/operators/reshape_op.cc
+2
-1
paddle/operators/reshape_op.h
paddle/operators/reshape_op.h
+3
-4
python/paddle/v2/framework/tests/test_reshape_op.py
python/paddle/v2/framework/tests/test_reshape_op.py
+6
-9
未找到文件。
paddle/operators/reshape_op.cc
浏览文件 @
899c7d6b
...
...
@@ -38,6 +38,7 @@ class ReshapeOp : public framework::OperatorWithKernel {
size_t
in_size
=
framework
::
product
(
in
->
dims
());
PADDLE_ENFORCE_EQ
(
shape_size
,
in_size
,
"The size of Input(X) mismatches with Attr(shape)."
);
ctx
.
Output
<
framework
::
Tensor
>
(
"Out"
)
->
Resize
(
in
->
dims
());
}
};
...
...
@@ -51,7 +52,7 @@ class ReshapeOpMaker : public framework::OpProtoAndCheckerMaker {
AddAttr
<
std
::
vector
<
int
>>
(
"shape"
,
"Target shape of reshape operator."
);
AddComment
(
R"DOC(Reshape operator
The input tensor will be reshaped with
Attr(shape).
Reshape Input(X) into the shape specified by
Attr(shape).
)DOC"
);
}
};
...
...
paddle/operators/reshape_op.h
浏览文件 @
899c7d6b
...
...
@@ -23,13 +23,13 @@ namespace operators {
using
Tensor
=
framework
::
Tensor
;
template
<
typename
Place
,
typename
T
,
typename
AttrType
=
T
>
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"
);
out
->
mutable_data
<
T
>
(
in
->
p
lace
());
out
->
mutable_data
<
T
>
(
ctx
.
GetP
lace
());
auto
shape
=
ctx
.
Attr
<
std
::
vector
<
int
>>
(
"shape"
);
std
::
vector
<
int64_t
>
tmp
;
...
...
@@ -42,7 +42,7 @@ class ReshapeKernel : public framework::OpKernel {
}
};
template
<
typename
Place
,
typename
T
,
typename
AttrType
=
T
>
template
<
typename
Place
,
typename
T
>
class
ReshapeGradKernel
:
public
framework
::
OpKernel
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
{
...
...
@@ -51,7 +51,6 @@ class ReshapeGradKernel : public framework::OpKernel {
d_x
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
auto
in_dims
=
d_x
->
dims
();
d_x
->
CopyFrom
<
T
>
(
*
d_out
,
ctx
.
GetPlace
());
d_x
->
Resize
(
in_dims
);
}
...
...
python/paddle/v2/framework/tests/test_reshape_op.py
浏览文件 @
899c7d6b
import
unittest
import
numpy
as
np
from
gradient_checker
import
GradientChecker
,
create_op
from
gradient_checker
import
GradientChecker
,
Operator
from
op_test_util
import
OpTestMeta
...
...
@@ -9,19 +9,16 @@ class TestReshapeOp(unittest.TestCase):
def
setUp
(
self
):
self
.
type
=
"reshape"
self
.
inputs
=
{
'X'
:
np
.
random
.
random
((
2
,
4
)).
astype
(
"float32"
),
}
print
self
.
inputs
self
.
attrs
=
{
'shape'
:
[
4
,
2
]}
self
.
inputs
=
{
'X'
:
np
.
random
.
random
((
37
,
51
)).
astype
(
"float32"
),
}
self
.
attrs
=
{
'shape'
:
[
51
,
37
]}
self
.
outputs
=
{
'Out'
:
self
.
inputs
[
'X'
].
reshape
(
self
.
attrs
[
'shape'
])}
print
self
.
outputs
class
ReshapeGradOpTest
(
GradientChecker
):
def
test_normal
(
self
):
op
=
create_op
(
"reshape"
)
inputs
=
{
"X"
:
np
.
random
.
random
((
2
,
4
)).
astype
(
"float32"
)}
attrs
=
{
'shape'
:
[
4
,
2
]}
self
.
check_grad
(
op
,
inputs
,
attrs
,
set
(
"X"
),
"Out"
)
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"
)
if
__name__
==
'__main__'
:
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录