Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
c8d87719
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看板
提交
c8d87719
编写于
9月 14, 2017
作者:
G
guosheng
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Revise the reduce_op unit test accordingly
上级
3994e91a
变更
4
显示空白变更内容
内联
并排
Showing
4 changed file
with
89 addition
and
86 deletion
+89
-86
paddle/operators/reduce_op.cc
paddle/operators/reduce_op.cc
+31
-25
paddle/operators/reduce_op.cu
paddle/operators/reduce_op.cu
+2
-2
paddle/operators/reduce_op.h
paddle/operators/reduce_op.h
+1
-1
python/paddle/v2/framework/tests/test_reduce_op.py
python/paddle/v2/framework/tests/test_reduce_op.py
+55
-58
未找到文件。
paddle/operators/reduce_op.cc
浏览文件 @
c8d87719
...
...
@@ -30,12 +30,14 @@ class ReduceOp : public framework::OperatorWithKernel {
auto
x_dims
=
ctx
.
Input
<
Tensor
>
(
"X"
)
->
dims
();
auto
x_rank
=
x_dims
.
size
();
PADDLE_ENFORCE_LE
(
x_rank
,
6
,
"Tensors with rank at most 6 are supported"
);
int
dim
=
static_cast
<
int
>
(
ctx
.
Attr
<
int
>
(
"dim"
)
);
int
dim
=
ctx
.
Attr
<
int
>
(
"dim"
);
if
(
dim
<
0
)
dim
=
x_rank
+
dim
;
PADDLE_ENFORCE_LT
(
dim
,
x_rank
,
"The dim should be in the range [-rank(input), rank(input)]"
);
bool
keep_dim
=
true
;
// TODO;
"The dim should be in the range [-rank(input), rank(input))"
);
PADDLE_ENFORCE_GE
(
ctx
.
Attr
<
int
>
(
"keep_dim"
),
0
,
"keep_dim must be 0 or 1"
);
PADDLE_ENFORCE_LE
(
ctx
.
Attr
<
int
>
(
"keep_dim"
),
1
,
"keep_dim must be 0 or 1"
);
bool
keep_dim
=
ctx
.
Attr
<
int
>
(
"keep_dim"
)
==
1
;
auto
dims_vector
=
vectorize
(
x_dims
);
if
(
keep_dim
||
x_rank
==
1
)
{
dims_vector
[
dim
]
=
1
;
...
...
@@ -59,11 +61,11 @@ class ReduceGradOp : public framework::OperatorWithKernel {
auto
x_dims
=
ctx
.
Input
<
Tensor
>
(
"X"
)
->
dims
();
auto
x_rank
=
x_dims
.
size
();
PADDLE_ENFORCE_LE
(
x_rank
,
6
,
"Tensors with rank at most 6 are supported"
);
int
dim
=
static_cast
<
int
>
(
ctx
.
Attr
<
int
>
(
"dim"
)
);
int
dim
=
ctx
.
Attr
<
int
>
(
"dim"
);
if
(
dim
<
0
)
dim
=
x_rank
+
dim
;
PADDLE_ENFORCE_LT
(
dim
,
x_rank
,
"The dim should be in the range [-rank(input), rank(input)
]
"
);
"The dim should be in the range [-rank(input), rank(input)
)
"
);
auto
*
x_grad
=
ctx
.
Output
<
Tensor
>
(
framework
::
GradVarName
(
"X"
));
if
(
x_grad
)
x_grad
->
Resize
(
x_dims
);
}
...
...
@@ -84,12 +86,13 @@ The result tensor has 1 fewer dimension than the input unless `keep_dim` is true
)DOC"
);
AddAttr
<
int
>
(
"dim"
,
"(int, default 0) The dimension to reduce. "
"Must be in the range [-rank(input), rank(input)]"
)
"Must be in the range [-rank(input), rank(input))"
)
.
SetDefault
(
0
);
AddAttr
<
int
>
(
"keep_dim"
,
"(int, default 0) "
"Must be 0 or 1. If 1, retain the reduced dimension with length 1."
)
.
SetDefault
(
0
);
AddAttr
<
bool
>
(
"keep_dim"
,
"(bool, default fasle) "
"If true, retain the reduced dimension with length 1."
)
.
SetDefault
(
false
);
}
};
...
...
@@ -108,12 +111,13 @@ The result tensor has 1 fewer dimension than the input unless `keep_dim` is true
)DOC"
);
AddAttr
<
int
>
(
"dim"
,
"(int, default 0) The dimension to reduce. "
"Must be in the range [-rank(input), rank(input)]"
)
"Must be in the range [-rank(input), rank(input))"
)
.
SetDefault
(
0
);
AddAttr
<
int
>
(
"keep_dim"
,
"(int, default 0) "
"Must be 0 or 1. If 1, retain the reduced dimension with length 1."
)
.
SetDefault
(
0
);
AddAttr
<
bool
>
(
"keep_dim"
,
"(bool, default fasle) "
"If true, retain the reduced dimension with length 1."
)
.
SetDefault
(
false
);
}
};
...
...
@@ -132,12 +136,13 @@ The result tensor has 1 fewer dimension than the input unless `keep_dim` is true
)DOC"
);
AddAttr
<
int
>
(
"dim"
,
"(int, default 0) The dimension to reduce. "
"Must be in the range [-rank(input), rank(input)]"
)
"Must be in the range [-rank(input), rank(input))"
)
.
SetDefault
(
0
);
AddAttr
<
int
>
(
"keep_dim"
,
"(int, default 0) "
"Must be 0 or 1. If 1, retain the reduced dimension with length 1."
)
.
SetDefault
(
0
);
AddAttr
<
bool
>
(
"keep_dim"
,
"(bool, default fasle) "
"If true, retain the reduced dimension with length 1."
)
.
SetDefault
(
false
);
}
};
...
...
@@ -156,12 +161,13 @@ The result tensor has 1 fewer dimension than the input unless `keep_dim` is true
)DOC"
);
AddAttr
<
int
>
(
"dim"
,
"(int, default 0) The dimension to reduce. "
"Must be in the range [-rank(input), rank(input)]"
)
"Must be in the range [-rank(input), rank(input))"
)
.
SetDefault
(
0
);
AddAttr
<
int
>
(
"keep_dim"
,
"(int, default 0) "
"Must be 0 or 1. If 1, retain the reduced dimension with length 1."
)
.
SetDefault
(
0
);
AddAttr
<
bool
>
(
"keep_dim"
,
"(bool, default fasle) "
"If true, retain the reduced dimension with length 1."
)
.
SetDefault
(
false
);
}
};
...
...
paddle/operators/reduce_op.cu
浏览文件 @
c8d87719
...
...
@@ -21,8 +21,8 @@ REGISTER_OP_GPU_KERNEL(
reduce_sum
,
ops
::
ReduceKernel
<
paddle
::
platform
::
GPUPlace
,
float
,
ops
::
SumFunctor
>
);
REGISTER_OP_GPU_KERNEL
(
reduce_sum_grad
,
ops
::
ReduceGrad
EigenKernel
<
paddle
::
platform
::
GPUPlace
,
float
,
ops
::
SumGradFunctor
>
);
ops
::
ReduceGrad
Kernel
<
paddle
::
platform
::
GPUPlace
,
float
,
ops
::
SumGradFunctor
>
);
REGISTER_OP_GPU_KERNEL
(
reduce_mean
,
...
...
paddle/operators/reduce_op.h
浏览文件 @
c8d87719
...
...
@@ -127,7 +127,7 @@ class ReduceKernel : public framework::OpKernel {
if
(
dim
<
0
)
dim
=
x_rank
+
dim
;
auto
reduce_dim
=
Eigen
::
array
<
int
,
1
>
({{
dim
}});
// construct the squeezed output tensor
bool
keep_dim
=
true
;
// static_cast<bool>(context.Attr<bool>("keep_dim"))
;
bool
keep_dim
=
context
.
Attr
<
int
>
(
"keep_dim"
)
==
1
;
DDim
dims
=
output
->
dims
();
auto
dims_vector
=
vectorize
(
dims
);
if
(
keep_dim
&&
x_rank
>
1
)
{
...
...
python/paddle/v2/framework/tests/test_reduce_op.py
浏览文件 @
c8d87719
import
unittest
import
numpy
as
np
from
gradient_checker
import
GradientChecker
,
create_op
from
op_test_util
import
OpTestMeta
from
paddle.v2.framework.op
import
Operator
from
op_test
import
OpTest
class
TestSumOp
(
unittest
.
TestCase
):
__metaclass__
=
OpTestMeta
class
TestSumOp
(
OpTest
):
def
setUp
(
self
):
self
.
type
=
"reduce_sum"
self
.
op_
type
=
"reduce_sum"
self
.
inputs
=
{
'X'
:
np
.
random
.
random
((
5
,
6
,
10
)).
astype
(
"float32"
)}
self
.
attrs
=
{
'dim'
:
-
2
}
out
=
self
.
inputs
[
'X'
].
sum
(
axis
=
self
.
attrs
[
'dim'
])
self
.
outputs
=
{
'Out'
:
out
}
self
.
outputs
=
{
'Out'
:
self
.
inputs
[
'X'
].
sum
(
axis
=
0
)}
def
test_check_output
(
self
):
self
.
check_output
()
class
TestSumGradOp
(
GradientChecker
):
def
test_normal
(
self
):
op
=
Operator
(
"reduce_sum"
,
X
=
"X"
,
Out
=
"Out"
,
dim
=-
2
)
# use small size to decrease the error of numerical calculation
inputs
=
{
'X'
:
np
.
random
.
random
((
5
,
6
,
10
)).
astype
(
"float32"
)}
self
.
check_grad
(
op
,
inputs
,
set
([
"X"
]),
"Out"
)
def
test_check_grad
(
self
):
self
.
check_grad
([
'X'
],
'Out'
)
def
test_1d_tensor
(
self
):
op
=
Operator
(
"reduce_sum"
,
X
=
"X"
,
Out
=
"Out"
,
dim
=
0
)
# use small size to decrease the error of numerical calculation
inputs
=
{
'X'
:
np
.
random
.
random
(
10
).
astype
(
"float32"
)}
self
.
check_grad
(
op
,
inputs
,
set
([
"X"
]),
"Out"
)
class
TestMeanOp
(
OpTest
):
def
setUp
(
self
):
self
.
op_type
=
"reduce_mean"
self
.
inputs
=
{
'X'
:
np
.
random
.
random
((
5
,
6
,
2
,
10
)).
astype
(
"float32"
)}
self
.
attrs
=
{
'dim'
:
1
}
self
.
outputs
=
{
'Out'
:
self
.
inputs
[
'X'
].
mean
(
axis
=
self
.
attrs
[
'dim'
])}
class
TestKeepdimSumOp
(
unittest
.
TestCase
):
__metaclass__
=
OpTestMeta
def
test_check_output
(
self
):
self
.
check_output
()
def
setUp
(
self
):
self
.
type
=
"reduce_sum"
self
.
inputs
=
{
'X'
:
np
.
random
.
random
((
5
,
6
,
10
)).
astype
(
"float32"
)}
self
.
attrs
=
{
'dim'
:
-
2
}
out
=
self
.
inputs
[
'X'
].
sum
(
axis
=
self
.
attrs
[
'dim'
],
keepdims
=
True
)
self
.
outputs
=
{
'Out'
:
out
}
def
test_check_grad
(
self
):
self
.
check_grad
([
'X'
],
'Out'
)
class
TestM
eanOp
(
unittest
.
TestCase
):
__metaclass__
=
OpTestMeta
class
TestM
axOp
(
OpTest
):
"""Remove Max with subgradient from gradient check to confirm the success of CI."""
def
setUp
(
self
):
self
.
type
=
"reduce_mean
"
self
.
op_type
=
"reduce_max
"
self
.
inputs
=
{
'X'
:
np
.
random
.
random
((
5
,
6
,
10
)).
astype
(
"float32"
)}
self
.
attrs
=
{
'dim'
:
-
1
}
out
=
self
.
inputs
[
'X'
].
mean
(
axis
=
self
.
attrs
[
'dim'
])
self
.
outputs
=
{
'Out'
:
out
}
self
.
outputs
=
{
'Out'
:
self
.
inputs
[
'X'
].
max
(
axis
=
self
.
attrs
[
'dim'
])}
def
test_check_output
(
self
):
self
.
check_output
()
class
TestMeanGradOp
(
GradientChecker
):
def
test_normal
(
self
):
op
=
Operator
(
"reduce_mean"
,
X
=
"X"
,
Out
=
"Out"
,
dim
=-
2
)
# use small size to decrease the error of numerical calculation
inputs
=
{
'X'
:
np
.
random
.
random
((
5
,
6
,
10
)).
astype
(
"float32"
)}
self
.
check_grad
(
op
,
inputs
,
set
([
"X"
]),
"Out"
)
class
TestMinOp
(
OpTest
):
"""Remove Min with subgradient from gradient check to confirm the success of CI."""
def
test_1d_tensor
(
self
):
op
=
Operator
(
"reduce_mean"
,
X
=
"X"
,
Out
=
"Out"
,
dim
=
0
)
# use small size to decrease the error of numerical calculation
inputs
=
{
'X'
:
np
.
random
.
random
(
10
).
astype
(
"float32"
)
}
self
.
check_grad
(
op
,
inputs
,
set
([
"X"
]),
"Out"
)
def
setUp
(
self
):
self
.
op_type
=
"reduce_min"
self
.
inputs
=
{
'X'
:
np
.
random
.
random
((
5
,
6
,
10
)).
astype
(
"float32"
)}
self
.
attrs
=
{
'dim'
:
2
}
self
.
outputs
=
{
'Out'
:
self
.
inputs
[
'X'
].
min
(
axis
=
self
.
attrs
[
'dim'
])}
def
test_check_output
(
self
):
self
.
check_output
()
class
TestMaxOp
(
unittest
.
TestCase
):
__metaclass__
=
OpTestMeta
class
TestKeepDimReduce
(
OpTest
):
def
setUp
(
self
):
self
.
type
=
"reduce_max
"
self
.
op_type
=
"reduce_sum
"
self
.
inputs
=
{
'X'
:
np
.
random
.
random
((
5
,
6
,
10
)).
astype
(
"float32"
)}
self
.
attrs
=
{
'dim'
:
-
1
}
out
=
self
.
inputs
[
'X'
].
max
(
axis
=
self
.
attrs
[
'dim'
])
self
.
outputs
=
{
'Out'
:
out
}
self
.
attrs
=
{
'dim'
:
-
2
,
'keep_dim'
:
1
}
self
.
outputs
=
{
'Out'
:
self
.
inputs
[
'X'
].
sum
(
axis
=
self
.
attrs
[
'dim'
],
keepdims
=
True
)
}
def
test_check_output
(
self
):
self
.
check_output
()
def
test_check_grad
(
self
):
self
.
check_grad
([
'X'
],
'Out'
)
class
TestMinOp
(
unittest
.
TestCase
):
__metaclass__
=
OpTestMeta
class
Test1DReduce
(
OpTest
):
def
setUp
(
self
):
self
.
type
=
"reduce_max"
self
.
inputs
=
{
'X'
:
np
.
random
.
random
((
5
,
6
,
10
)).
astype
(
"float32"
)}
self
.
attrs
=
{
'dim'
:
-
2
}
out
=
self
.
inputs
[
'X'
].
min
(
axis
=
self
.
attrs
[
'dim'
])
self
.
outputs
=
{
'Out'
:
out
}
self
.
op_type
=
"reduce_sum"
self
.
inputs
=
{
'X'
:
np
.
random
.
random
(
20
).
astype
(
"float32"
)}
self
.
outputs
=
{
'Out'
:
self
.
inputs
[
'X'
].
sum
(
axis
=
0
)}
def
test_check_output
(
self
):
self
.
check_output
()
def
test_check_grad
(
self
):
self
.
check_grad
([
'X'
],
'Out'
)
if
__name__
==
'__main__'
:
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录