Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Crayon鑫
Paddle
提交
15306ffd
P
Paddle
项目概览
Crayon鑫
/
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看板
提交
15306ffd
编写于
3月 07, 2018
作者:
Z
zhouhanqing
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add product reduction for reduce_op
上级
84aea8a8
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
90 addition
and
1 deletion
+90
-1
paddle/fluid/operators/reduce_op.cc
paddle/fluid/operators/reduce_op.cc
+12
-0
paddle/fluid/operators/reduce_op.h
paddle/fluid/operators/reduce_op.h
+18
-1
python/paddle/fluid/layers/nn.py
python/paddle/fluid/layers/nn.py
+47
-0
python/paddle/fluid/tests/unittests/test_reduce_op.py
python/paddle/fluid/tests/unittests/test_reduce_op.py
+13
-0
未找到文件。
paddle/fluid/operators/reduce_op.cc
浏览文件 @
15306ffd
...
...
@@ -173,6 +173,15 @@ class ReduceMinOpMaker : public ReduceOpMaker {
}
};
class
ReduceProdOpMaker
:
public
ReduceOpMaker
{
public:
ReduceProdOpMaker
(
OpProto
*
proto
,
OpAttrChecker
*
op_checker
)
:
ReduceOpMaker
(
proto
,
op_checker
)
{
SetComment
(
"ReduceProd"
,
"prod"
);
AddComment
(
comment_
);
}
};
}
// namespace operators
}
// namespace paddle
...
...
@@ -190,6 +199,9 @@ REGISTER_OP(reduce_max, ops::ReduceOp, ops::ReduceMaxOpMaker, reduce_max_grad,
REGISTER_OP
(
reduce_min
,
ops
::
ReduceOp
,
ops
::
ReduceMinOpMaker
,
reduce_min_grad
,
ops
::
ReduceGradOp
);
REGISTER_OP
(
reduce_prod
,
ops
::
ReduceOp
,
ops
::
ReduceProdOpMaker
,
reduce_prod_grad
,
ops
::
ReduceGradOp
);
#define REGISTER_REDUCE_CPU_KERNEL(reduce_type, functor, grad_functor) \
REGISTER_OP_CPU_KERNEL(reduce_type, \
ops::ReduceKernel<paddle::platform::CPUDeviceContext, \
...
...
paddle/fluid/operators/reduce_op.h
浏览文件 @
15306ffd
...
...
@@ -93,6 +93,22 @@ struct MaxOrMinGradFunctor {
}
};
struct
ProdFunctor
{
template
<
typename
DeviceContext
,
typename
X
,
typename
Y
,
typename
Dim
>
void
operator
()(
const
DeviceContext
&
place
,
X
&
x
,
Y
&
y
,
const
Dim
&
dim
)
{
y
.
device
(
place
)
=
x
.
prod
(
dim
);
}
};
struct
ProdGradFunctor
{
template
<
typename
DeviceContext
,
typename
X
,
typename
Y
,
typename
DX
,
typename
DY
,
typename
Dim
>
void
operator
()(
const
DeviceContext
&
place
,
X
&
x
,
Y
&
y
,
DX
&
dx
,
DY
&
dy
,
const
Dim
&
dim
,
int
size
)
{
dx
.
device
(
place
)
=
dy
.
broadcast
(
dim
)
*
y
.
broadcast
(
dim
)
*
x
.
inverse
();
}
};
template
<
typename
DeviceContext
,
typename
T
,
typename
Functor
>
class
ReduceKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
...
...
@@ -254,4 +270,5 @@ class ReduceGradKernel : public framework::OpKernel<T> {
__macro(reduce_sum, SumFunctor, SumGradFunctor); \
__macro(reduce_mean, MeanFunctor, MeanGradFunctor); \
__macro(reduce_max, MaxFunctor, MaxOrMinGradFunctor); \
__macro(reduce_min, MinFunctor, MaxOrMinGradFunctor);
__macro(reduce_min, MinFunctor, MaxOrMinGradFunctor); \
__macro(reduce_prod, ProdFunctor, ProdGradFunctor);
python/paddle/fluid/layers/nn.py
浏览文件 @
15306ffd
...
...
@@ -49,6 +49,7 @@ __all__ = [
'reduce_mean'
,
'reduce_max'
,
'reduce_min'
,
'reduce_prod'
,
'sequence_first_step'
,
'sequence_last_step'
,
'dropout'
,
...
...
@@ -2200,6 +2201,52 @@ def reduce_min(input, dim=None, keep_dim=False, name=None):
return
out
def
reduce_prod
(
input
,
dim
=
None
,
keep_dim
=
False
,
name
=
None
):
"""
Computes the product of tensor elements over the given dimension.
Args:
input (Variable): The input variable which is a Tensor or LoDTensor.
dim (int|None): The dimension along which the product is performed. If
:attr:`None`, multipy all elements of :attr:`input` and return a
Tensor variable with a single element, otherwise must be in the
range :math:`[-rank(input), rank(input))`. If :math:`dim < 0`,
the dimension to reduce is :math:`rank + dim`.
keep_dim (bool|False): Whether to reserve the reduced dimension in the
output Tensor. The result tensor will have one fewer dimension
than the :attr:`input` unless :attr:`keep_dim` is true.
name(str|None): A name for this layer(optional). If set None, the layer
will be named automatically.
Returns:
Variable: The reduced Tensor variable.
Examples:
.. code-block:: python
# x is a Tensor variable with following elements:
# [[0.2, 0.3, 0.5, 0.9]
# [0.1, 0.2, 0.6, 0.7]]
# Each example is followed by the correspending output tensor.
fluid.layers.reduce_prod(x) # [0.0002268]
fluid.layers.reduce_prod(x, dim=0) # [0.02, 0.06, 0.3, 0.63]
fluid.layers.reduce_prod(x, dim=-1) # [0.027, 0.0084]
fluid.layers.reduce_prod(x, dim=1, keep_dim=True) # [[0.027], [0.0084]]
"""
helper
=
LayerHelper
(
'reduce_prod'
,
**
locals
())
out
=
helper
.
create_tmp_variable
(
dtype
=
helper
.
input_dtype
())
helper
.
append_op
(
type
=
'reduce_prod'
,
inputs
=
{
'X'
:
input
},
outputs
=
{
'Out'
:
out
},
attrs
=
{
'dim'
:
dim
if
dim
!=
None
else
0
,
'keep_dim'
:
keep_dim
,
'reduce_all'
:
True
if
dim
==
None
else
False
})
return
out
def
split
(
input
,
num_or_sections
,
dim
=-
1
,
name
=
None
):
"""
Split the input tensor into multiple sub-tensors.
...
...
python/paddle/fluid/tests/unittests/test_reduce_op.py
浏览文件 @
15306ffd
...
...
@@ -70,6 +70,19 @@ class TestMinOp(OpTest):
self
.
check_output
()
class
TestProdOp
(
OpTest
):
def
setUp
(
self
):
self
.
op_type
=
"reduce_prod"
self
.
inputs
=
{
'X'
:
np
.
random
.
random
((
5
,
6
,
10
)).
astype
(
"float64"
)}
self
.
outputs
=
{
'Out'
:
self
.
inputs
[
'X'
].
prod
(
axis
=
0
)}
def
test_check_output
(
self
):
self
.
check_output
()
def
test_check_grad
(
self
):
self
.
check_grad
([
'X'
],
'Out'
)
class
TestKeepDimReduce
(
OpTest
):
def
setUp
(
self
):
self
.
op_type
=
"reduce_sum"
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录