Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Crayon鑫
Paddle
提交
2d6cc0b1
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看板
未验证
提交
2d6cc0b1
编写于
8月 13, 2020
作者:
W
wawltor
提交者:
GitHub
8月 13, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
support the tuple for attribute of axis in min, max for api2.0
Update the code for the min,max, test=develop
上级
68203566
变更
4
显示空白变更内容
内联
并排
Showing
4 changed file
with
92 addition
and
7 deletion
+92
-7
paddle/fluid/operators/reduce_ops/reduce_op.h
paddle/fluid/operators/reduce_ops/reduce_op.h
+36
-0
python/paddle/fluid/tests/unittests/test_max_op.py
python/paddle/fluid/tests/unittests/test_max_op.py
+18
-0
python/paddle/fluid/tests/unittests/test_min_op.py
python/paddle/fluid/tests/unittests/test_min_op.py
+18
-0
python/paddle/tensor/math.py
python/paddle/tensor/math.py
+20
-7
未找到文件。
paddle/fluid/operators/reduce_ops/reduce_op.h
浏览文件 @
2d6cc0b1
...
...
@@ -15,6 +15,7 @@ limitations under the License. */
#pragma once
#include <algorithm>
#include <set>
#include <string>
#include <vector>
...
...
@@ -98,6 +99,18 @@ class ReduceKernel : public framework::OpKernel<T> {
int
out_dtype
=
context
.
Attr
<
int
>
(
"out_dtype"
);
framework
::
proto
::
VarType
::
Type
cast_out_dtype
;
// The dims has full dim, set the reduce_all is True
const
auto
&
input_dim_size
=
context
.
Input
<
Tensor
>
(
"X"
)
->
dims
().
size
();
std
::
set
<
int
>
dims_set
(
dims
.
begin
(),
dims
.
end
());
bool
full_dim
=
true
;
for
(
auto
i
=
0
;
i
<
input_dim_size
;
i
++
)
{
if
(
dims_set
.
find
(
i
)
==
dims_set
.
end
())
{
full_dim
=
false
;
break
;
}
}
reduce_all
=
(
reduce_all
||
full_dim
);
if
(
out_dtype
<
0
)
{
auto
*
cast_input
=
context
.
Input
<
Tensor
>
(
"X"
);
cast_out_dtype
=
...
...
@@ -137,6 +150,18 @@ class BoolReduceKernel : public framework::OpKernel<OutT> {
auto
dims
=
context
.
Attr
<
std
::
vector
<
int
>>
(
"dim"
);
bool
keep_dim
=
context
.
Attr
<
bool
>
(
"keep_dim"
);
// The dims has full dim, set the reduce_all is True
const
auto
&
input_dim_size
=
context
.
Input
<
Tensor
>
(
"X"
)
->
dims
().
size
();
std
::
set
<
int
>
dims_set
(
dims
.
begin
(),
dims
.
end
());
bool
full_dim
=
true
;
for
(
auto
i
=
0
;
i
<
input_dim_size
;
i
++
)
{
if
(
dims_set
.
find
(
i
)
==
dims_set
.
end
())
{
full_dim
=
false
;
break
;
}
}
reduce_all
=
(
reduce_all
||
full_dim
);
if
(
reduce_all
)
{
// Flatten and reduce 1-D tensor
auto
x
=
EigenVector
<
OutT
>::
Flatten
(
*
input
);
...
...
@@ -183,6 +208,17 @@ class ReduceGradKernel : public framework::OpKernel<T> {
auto
*
output
=
context
.
Output
<
Tensor
>
(
framework
::
GradVarName
(
"X"
));
output
->
mutable_data
<
T
>
(
context
.
GetPlace
());
// The dims has full dim, set the reduce_all is True
const
auto
&
input_dim_size
=
context
.
Input
<
Tensor
>
(
"X"
)
->
dims
().
size
();
std
::
set
<
int
>
dims_set
(
dims
.
begin
(),
dims
.
end
());
bool
full_dim
=
true
;
for
(
auto
i
=
0
;
i
<
input_dim_size
;
i
++
)
{
if
(
dims_set
.
find
(
i
)
==
dims_set
.
end
())
{
full_dim
=
false
;
break
;
}
}
reduce_all
=
(
reduce_all
||
full_dim
);
// NOTE: EigenTensor::From() uses tensor->data()
// if op has NoNeedBufferVarsInferer, the corresponding kNoNeedBufferX or
// kNoNeedBufferY should set true
...
...
python/paddle/fluid/tests/unittests/test_max_op.py
浏览文件 @
2d6cc0b1
...
...
@@ -48,6 +48,15 @@ class ApiMaxTest(unittest.TestCase):
res
,
=
exe
.
run
(
feed
=
{
"data"
:
input_data
},
fetch_list
=
[
result_max
])
self
.
assertEqual
((
res
==
np
.
max
(
input_data
,
axis
=
0
)).
all
(),
True
)
with
paddle
.
static
.
program_guard
(
paddle
.
static
.
Program
(),
paddle
.
static
.
Program
()):
data
=
paddle
.
nn
.
data
(
"data"
,
shape
=
[
10
,
10
],
dtype
=
"int64"
)
result_max
=
paddle
.
max
(
x
=
data
,
axis
=
(
0
,
1
))
exe
=
paddle
.
static
.
Executor
(
self
.
place
)
input_data
=
np
.
random
.
randint
(
10
,
size
=
(
10
,
10
)).
astype
(
np
.
int64
)
res
,
=
exe
.
run
(
feed
=
{
"data"
:
input_data
},
fetch_list
=
[
result_max
])
self
.
assertEqual
((
res
==
np
.
max
(
input_data
,
axis
=
(
0
,
1
))).
all
(),
True
)
def
test_errors
(
self
):
paddle
.
enable_static
()
...
...
@@ -59,6 +68,15 @@ class ApiMaxTest(unittest.TestCase):
self
.
assertRaises
(
TypeError
,
test_input_type
)
def
test_axis_type
():
with
paddle
.
static
.
program_guard
(
paddle
.
static
.
Program
(),
paddle
.
static
.
Program
()):
data
=
paddle
.
nn
.
data
(
"data"
,
shape
=
[
10
,
10
],
dtype
=
"int64"
)
axis
=
paddle
.
nn
.
data
(
"axis"
,
shape
=
[
10
,
10
],
dtype
=
"int64"
)
result_min
=
paddle
.
min
(
data
,
axis
)
self
.
assertRaises
(
TypeError
,
test_axis_type
)
def
test_imperative_api
(
self
):
paddle
.
disable_static
()
np_x
=
np
.
array
([
10
,
10
]).
astype
(
'float64'
)
...
...
python/paddle/fluid/tests/unittests/test_min_op.py
浏览文件 @
2d6cc0b1
...
...
@@ -48,6 +48,15 @@ class ApiMinTest(unittest.TestCase):
res
,
=
exe
.
run
(
feed
=
{
"data"
:
input_data
},
fetch_list
=
[
result_min
])
self
.
assertEqual
((
res
==
np
.
min
(
input_data
,
axis
=
0
)).
all
(),
True
)
with
paddle
.
static
.
program_guard
(
paddle
.
static
.
Program
(),
paddle
.
static
.
Program
()):
data
=
paddle
.
nn
.
data
(
"data"
,
shape
=
[
10
,
10
],
dtype
=
"int64"
)
result_min
=
paddle
.
min
(
x
=
data
,
axis
=
(
0
,
1
))
exe
=
paddle
.
static
.
Executor
(
self
.
place
)
input_data
=
np
.
random
.
randint
(
10
,
size
=
(
10
,
10
)).
astype
(
np
.
int64
)
res
,
=
exe
.
run
(
feed
=
{
"data"
:
input_data
},
fetch_list
=
[
result_min
])
self
.
assertEqual
((
res
==
np
.
min
(
input_data
,
axis
=
(
0
,
1
))).
all
(),
True
)
def
test_errors
(
self
):
paddle
.
enable_static
()
...
...
@@ -59,6 +68,15 @@ class ApiMinTest(unittest.TestCase):
self
.
assertRaises
(
TypeError
,
test_input_type
)
def
test_axis_type
():
with
paddle
.
static
.
program_guard
(
paddle
.
static
.
Program
(),
paddle
.
static
.
Program
()):
data
=
paddle
.
nn
.
data
(
"data"
,
shape
=
[
10
,
10
],
dtype
=
"int64"
)
axis
=
paddle
.
nn
.
data
(
"axis"
,
shape
=
[
10
,
10
],
dtype
=
"int64"
)
result_min
=
paddle
.
min
(
data
,
axis
)
self
.
assertRaises
(
TypeError
,
test_axis_type
)
def
test_imperative_api
(
self
):
paddle
.
disable_static
()
np_x
=
np
.
array
([
10
,
10
]).
astype
(
'float64'
)
...
...
python/paddle/tensor/math.py
浏览文件 @
2d6cc0b1
...
...
@@ -1177,19 +1177,19 @@ def max(x, axis=None, keepdim=False, name=None):
float64, int32, int64.
axis(list|int, optional): The axis along which the maximum is computed.
If :attr:`None`, compute the maximum over all elements of
:attr:`input
` and return a Tensor variable with a single element,
`x
` and return a Tensor variable with a single element,
otherwise must be in the range :math:`[-x.ndim(x), x.ndim(x))`.
If :math:`axis[i] < 0`, the axis to reduce is :math:`x.ndim + axis[i]`.
keepdim(bool, optional): Whether to reserve the reduced dimension in the
output Tensor. The result tensor will have one fewer dimension
than the
:attr:`input
` unless :attr:`keepdim` is true, default
than the
`x
` unless :attr:`keepdim` is true, default
value is False.
name(str, optional): The default value is None. Normally there is no need for
user to set this property. For more information, please refer to :ref:`api_guide_Name`
Returns:
Tensor, results of maximum on the specified axis of input tensor,
it's data type is the same as
input's Tensor
.
it's data type is the same as
`x`
.
Examples:
.. code-block:: python
...
...
@@ -1232,7 +1232,14 @@ def max(x, axis=None, keepdim=False, name=None):
"""
if
axis
is
not
None
and
not
isinstance
(
axis
,
list
):
axis
=
[
axis
]
if
isinstance
(
axis
,
tuple
):
axis
=
list
(
axis
)
elif
isinstance
(
axis
,
int
):
axis
=
[
axis
]
else
:
raise
TypeError
(
"The type of axis must be int, list or tuple, but received {}"
.
format
(
type
(
axis
)))
reduce_all
=
True
if
axis
==
None
or
axis
==
[]
else
False
axis
=
axis
if
axis
!=
None
and
axis
!=
[]
else
[
0
]
if
in_dygraph_mode
():
...
...
@@ -1265,12 +1272,12 @@ def min(x, axis=None, keepdim=False, name=None):
x(Tensor): A tensor, the data type is float32, float64, int32, int64.
axis(list|int, optional): The axis along which the minimum is computed.
If :attr:`None`, compute the minimum over all elements of
:attr:`input
` and return a Tensor variable with a single element,
`x
` and return a Tensor variable with a single element,
otherwise must be in the range :math:`[-x.ndim, x.ndim)`.
If :math:`axis[i] < 0`, the axis to reduce is :math:`x.ndim + axis[i]`.
keepdim(bool, optional): Whether to reserve the reduced dimension in the
output Tensor. The result tensor will have one fewer dimension
than the
:attr:`input
` unless :attr:`keepdim` is true, default
than the
`x
` unless :attr:`keepdim` is true, default
value is False.
name(str, optional): The default value is None. Normally there is no need for
user to set this property. For more information, please refer to :ref:`api_guide_Name`
...
...
@@ -1320,7 +1327,13 @@ def min(x, axis=None, keepdim=False, name=None):
"""
if
axis
is
not
None
and
not
isinstance
(
axis
,
list
):
if
isinstance
(
axis
,
tuple
):
axis
=
list
(
axis
)
elif
isinstance
(
axis
,
int
):
axis
=
[
axis
]
else
:
raise
TypeError
(
"The type of axis must be int, list or tuple, but received {}"
.
format
(
type
(
axis
)))
reduce_all
=
True
if
axis
==
None
or
axis
==
[]
else
False
axis
=
axis
if
axis
!=
None
and
axis
!=
[]
else
[
0
]
if
in_dygraph_mode
():
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录