Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
12eaa22a
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看板
提交
12eaa22a
编写于
9月 06, 2017
作者:
Y
Yibing Liu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add reshape operator
上级
a072ab9e
变更
6
显示空白变更内容
内联
并排
Showing
6 changed file
with
196 addition
and
0 deletion
+196
-0
paddle/operators/reshape_op.cc
paddle/operators/reshape_op.cc
+84
-0
paddle/operators/reshape_op.cu
paddle/operators/reshape_op.cu
+22
-0
paddle/operators/reshape_op.h
paddle/operators/reshape_op.h
+60
-0
paddle/pybind/pybind.cc
paddle/pybind/pybind.cc
+1
-0
python/paddle/v2/framework/tests/CMakeLists.txt
python/paddle/v2/framework/tests/CMakeLists.txt
+1
-0
python/paddle/v2/framework/tests/test_reshape_op.py
python/paddle/v2/framework/tests/test_reshape_op.py
+28
-0
未找到文件。
paddle/operators/reshape_op.cc
0 → 100644
浏览文件 @
12eaa22a
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#include "paddle/operators/reshape_op.h"
namespace
paddle
{
namespace
operators
{
class
ReshapeOp
:
public
framework
::
OperatorWithKernel
{
public:
ReshapeOp
(
const
std
::
string
&
type
,
const
framework
::
VariableNameMap
&
inputs
,
const
framework
::
VariableNameMap
&
outputs
,
const
framework
::
AttributeMap
&
attrs
)
:
OperatorWithKernel
(
type
,
inputs
,
outputs
,
attrs
)
{}
protected:
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
;
for
(
auto
dim
:
shape
)
{
shape_size
*=
dim
;
}
size_t
in_size
=
framework
::
product
(
in
->
dims
());
PADDLE_ENFORCE_EQ
(
shape_size
,
in_size
,
"The size of Input(X) mismatches with Attr(shape)."
);
}
};
class
ReshapeOpMaker
:
public
framework
::
OpProtoAndCheckerMaker
{
public:
ReshapeOpMaker
(
framework
::
OpProto
*
proto
,
framework
::
OpAttrChecker
*
op_checker
)
:
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
AddInput
(
"X"
,
"The input tensor of reshape operator."
);
AddOutput
(
"Out"
,
"The output tensor of reshape operator."
);
AddAttr
<
std
::
vector
<
int
>>
(
"shape"
,
"Target shape of reshape operator."
);
AddComment
(
R"DOC(Reshape operator
The input tensor will be reshaped with Attr(shape).
)DOC"
);
}
};
class
ReshapeGradOp
:
public
framework
::
OperatorWithKernel
{
public:
ReshapeGradOp
(
const
std
::
string
&
type
,
const
framework
::
VariableNameMap
&
inputs
,
const
framework
::
VariableNameMap
&
outputs
,
const
framework
::
AttributeMap
&
attrs
)
:
OperatorWithKernel
(
type
,
inputs
,
outputs
,
attrs
)
{}
protected:
void
InferShape
(
const
framework
::
InferShapeContext
&
ctx
)
const
override
{
auto
dims
=
ctx
.
Input
<
framework
::
Tensor
>
(
"X"
)
->
dims
();
auto
*
d_in
=
ctx
.
Output
<
framework
::
Tensor
>
(
framework
::
GradVarName
(
"X"
));
d_in
->
Resize
(
dims
);
}
};
}
// namespace operators
}
// namespace paddle
namespace
ops
=
paddle
::
operators
;
REGISTER_OP
(
reshape
,
ops
::
ReshapeOp
,
ops
::
ReshapeOpMaker
,
reshape_grad
,
ops
::
ReshapeGradOp
);
REGISTER_OP_CPU_KERNEL
(
reshape
,
ops
::
ReshapeKernel
<
paddle
::
platform
::
CPUPlace
,
float
>
);
REGISTER_OP_CPU_KERNEL
(
reshape_grad
,
ops
::
ReshapeGradKernel
<
paddle
::
platform
::
CPUPlace
,
float
>
);
paddle/operators/reshape_op.cu
0 → 100644
浏览文件 @
12eaa22a
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#include "paddle/operators/reshape_op.h"
REGISTER_OP_GPU_KERNEL
(
reshape
,
paddle
::
operators
::
ReshapeKernel
<
paddle
::
platform
::
GPUPlace
,
float
>
);
REGISTER_OP_GPU_KERNEL
(
reshape_grad
,
paddle
::
operators
::
ReshapeGradKernel
<
paddle
::
platform
::
GPUPlace
,
float
>
);
paddle/operators/reshape_op.h
0 → 100644
浏览文件 @
12eaa22a
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#pragma once
#include "paddle/framework/eigen.h"
#include "paddle/framework/op_registry.h"
namespace
paddle
{
namespace
operators
{
using
Tensor
=
framework
::
Tensor
;
template
<
typename
Place
,
typename
T
,
typename
AttrType
=
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
->
place
());
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
);
out
->
CopyFrom
<
T
>
(
*
in
,
ctx
.
GetPlace
());
out
->
Resize
(
out_dims
);
}
};
template
<
typename
Place
,
typename
T
,
typename
AttrType
=
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"
));
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
);
}
};
}
}
paddle/pybind/pybind.cc
浏览文件 @
12eaa22a
...
@@ -50,6 +50,7 @@ USE_OP(cos_sim);
...
@@ -50,6 +50,7 @@ USE_OP(cos_sim);
USE_CPU_ONLY_OP
(
gather
);
USE_CPU_ONLY_OP
(
gather
);
USE_CPU_ONLY_OP
(
scatter
);
USE_CPU_ONLY_OP
(
scatter
);
USE_OP
(
squared_l2_distance
);
USE_OP
(
squared_l2_distance
);
USE_OP
(
reshape
);
namespace
paddle
{
namespace
paddle
{
namespace
framework
{
namespace
framework
{
...
...
python/paddle/v2/framework/tests/CMakeLists.txt
浏览文件 @
12eaa22a
...
@@ -34,3 +34,4 @@ py_test(test_lookup_table SRCS test_lookup_table.py)
...
@@ -34,3 +34,4 @@ py_test(test_lookup_table SRCS test_lookup_table.py)
py_test
(
test_scale_and_identity_op SRCS test_scale_and_identity_op.py
)
py_test
(
test_scale_and_identity_op SRCS test_scale_and_identity_op.py
)
py_test
(
mnist SRCS mnist.py
)
py_test
(
mnist SRCS mnist.py
)
py_test
(
test_squared_l2_distance_op SRCS test_squared_l2_distance_op.py
)
py_test
(
test_squared_l2_distance_op SRCS test_squared_l2_distance_op.py
)
py_test
(
test_reshape_op SRCS test_reshape_op.py
)
python/paddle/v2/framework/tests/test_reshape_op.py
0 → 100644
浏览文件 @
12eaa22a
import
unittest
import
numpy
as
np
from
gradient_checker
import
GradientChecker
,
create_op
from
op_test_util
import
OpTestMeta
class
TestReshapeOp
(
unittest
.
TestCase
):
__metaclass__
=
OpTestMeta
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
.
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"
)
if
__name__
==
'__main__'
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录