Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
MegEngine 天元
MegEngine
提交
984d85ca
MegEngine
项目概览
MegEngine 天元
/
MegEngine
大约 1 年 前同步成功
通知
399
Star
4705
Fork
582
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
MegEngine
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
984d85ca
编写于
5月 05, 2021
作者:
M
Megvii Engine Team
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat(mge/functional): argmin and argmax support negtive axis
GitOrigin-RevId: a1bd1102a6c24b6c4ceb257f962ffdebe1669744
上级
87d6ff22
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
76 addition
and
17 deletion
+76
-17
imperative/python/megengine/core/tensor/array_method.py
imperative/python/megengine/core/tensor/array_method.py
+3
-3
imperative/python/megengine/core/tensor/utils.py
imperative/python/megengine/core/tensor/utils.py
+25
-0
imperative/python/megengine/functional/math.py
imperative/python/megengine/functional/math.py
+12
-14
imperative/python/test/unit/functional/test_functional.py
imperative/python/test/unit/functional/test_functional.py
+16
-0
imperative/python/test/unit/functional/test_math.py
imperative/python/test/unit/functional/test_math.py
+20
-0
未找到文件。
imperative/python/megengine/core/tensor/array_method.py
浏览文件 @
984d85ca
...
...
@@ -165,7 +165,7 @@ def _remove_axis(inp: Tensor, axis) -> Tensor:
return
list
(
map
(
int
,
axis
))
axis
=
get_axes
()
axis
=
sorted
(
i
+
inp
.
ndim
if
i
<
0
else
i
for
i
in
axis
)
axis
=
utils
.
_normalize_axis
(
inp
.
ndim
,
axis
)
axis
=
[
a
-
i
for
i
,
a
in
enumerate
(
axis
)]
op
=
builtin
.
RemoveAxis
(
axis
=
axis
)
...
...
@@ -190,8 +190,7 @@ def _reduce(mode):
op
=
builtin
.
Reduce
(
mode
=
mode
,
axis
=
0
)
(
result
,)
=
apply
(
op
,
data
)
elif
isinstance
(
axis
,
collections
.
abc
.
Iterable
):
axis
=
list
(
axis
)
axis
.
sort
(
reverse
=
True
)
axis
=
utils
.
_normalize_axis
(
self
.
ndim
,
axis
,
reverse
=
True
)
for
ai
in
axis
:
op
=
builtin
.
Reduce
(
mode
=
mode
,
axis
=
ai
)
(
data
,)
=
apply
(
op
,
data
)
...
...
@@ -199,6 +198,7 @@ def _reduce(mode):
data
=
_remove_axis
(
data
,
ai
)
result
=
data
else
:
# builtin.Reduce already accept negtive axis
op
=
builtin
.
Reduce
(
mode
=
mode
,
axis
=
axis
)
(
result
,)
=
apply
(
op
,
data
)
...
...
imperative/python/megengine/core/tensor/utils.py
浏览文件 @
984d85ca
...
...
@@ -178,3 +178,28 @@ def make_shape_tuple(shape):
s
=
[]
_expand_int
(
s
,
shape
)
return
tuple
(
s
)
def
_normalize_axis
(
ndim
:
int
,
axis
:
Union
[
int
,
Iterable
],
reverse
=
False
)
->
Union
[
int
,
list
]:
def
convert
(
x
):
x_org
=
x
if
x
<
0
:
x
=
ndim
+
x
assert
(
x
>=
0
and
x
<
ndim
),
"axis {} is out of bounds for tensor of dimension {}"
.
format
(
x_org
,
ndim
)
return
x
if
isinstance
(
axis
,
int
):
return
convert
(
axis
)
elif
isinstance
(
axis
,
Iterable
):
axis_org
=
axis
axis
=
list
(
sorted
(
map
(
convert
,
axis
),
reverse
=
reverse
))
for
i
in
range
(
len
(
axis
)
-
1
):
assert
axis
[
i
]
!=
axis
[
i
+
1
],
"axis {} contains duplicated indices"
.
format
(
axis_org
)
return
axis
raise
imperative/python/megengine/functional/math.py
浏览文件 @
984d85ca
...
...
@@ -466,9 +466,13 @@ def argmin(
0
"""
if
axis
is
None
:
assert
not
keepdims
,
"can not set axis=None and keepdims=True"
inp
=
inp
.
flatten
()
axis
=
0
axis
=
utils
.
_normalize_axis
(
inp
.
ndim
,
axis
,
reverse
=
True
)
if
isinstance
(
axis
,
collections
.
abc
.
Iterable
):
axis
=
list
(
axis
)
axis
.
sort
(
reverse
=
True
)
for
ai
in
axis
:
op
=
builtin
.
Argmin
(
axis
=
ai
)
...
...
@@ -479,11 +483,6 @@ def argmin(
return
inp
if
axis
is
None
:
assert
not
keepdims
,
"can not set axis=None and keepdims=True"
inp
=
inp
.
flatten
()
axis
=
0
op
=
builtin
.
Argmin
(
axis
=
axis
)
(
result
,)
=
apply
(
op
,
inp
)
if
not
keepdims
:
...
...
@@ -525,9 +524,13 @@ def argmax(
5
"""
if
axis
is
None
:
assert
not
keepdims
,
"can not set axis=None and keepdims=True"
inp
=
inp
.
flatten
()
axis
=
0
axis
=
utils
.
_normalize_axis
(
inp
.
ndim
,
axis
,
reverse
=
True
)
if
isinstance
(
axis
,
collections
.
abc
.
Iterable
):
axis
=
list
(
axis
)
axis
.
sort
(
reverse
=
True
)
for
ai
in
axis
:
op
=
builtin
.
Argmax
(
axis
=
ai
)
...
...
@@ -538,11 +541,6 @@ def argmax(
return
inp
if
axis
is
None
:
assert
not
keepdims
,
"can not set axis=None and keepdims=True"
inp
=
inp
.
flatten
()
axis
=
0
op
=
builtin
.
Argmax
(
axis
=
axis
)
(
result
,)
=
apply
(
op
,
inp
)
if
not
keepdims
:
...
...
imperative/python/test/unit/functional/test_functional.py
浏览文件 @
984d85ca
...
...
@@ -811,3 +811,19 @@ def test_assert_not_equal():
y
=
F
.
zeros
(
shape
,
dtype
=
np
.
float32
)
+
1.1
with
pytest
.
raises
(
RuntimeError
):
z
=
F
.
utils
.
_assert_equal
(
x
,
y
)
def
test_neg_axis
():
x
=
tensor
(
np
.
random
.
normal
(
0
,
1
,
(
32
,
5
)))
y
=
F
.
argmax
(
x
,
axis
=-
1
)
yy
=
F
.
argmax
(
x
,
axis
=
1
)
np
.
testing
.
assert_equal
(
y
.
numpy
(),
yy
.
numpy
())
y
=
F
.
argmax
(
x
,
axis
=
(
-
1
,
-
2
))
yy
=
F
.
argmax
(
x
,
axis
=
(
0
,
1
))
np
.
testing
.
assert_equal
(
y
.
numpy
(),
yy
.
numpy
())
y
=
F
.
argmin
(
x
,
axis
=
(
-
1
,
-
2
))
yy
=
F
.
argmin
(
x
,
axis
=
(
0
,
1
))
np
.
testing
.
assert_equal
(
y
.
numpy
(),
yy
.
numpy
())
imperative/python/test/unit/functional/test_math.py
浏览文件 @
984d85ca
...
...
@@ -9,6 +9,7 @@
from
functools
import
partial
import
numpy
as
np
import
pytest
from
utils
import
opr_test
import
megengine.functional
as
F
...
...
@@ -48,6 +49,14 @@ def common_test_reduce(opr, ref_opr):
ref_fn
=
lambda
x
:
ref_opr
(
x
,
axis
=
axis
).
astype
(
np
.
int32
),
axis
=
axis
,
)
# test negative axis
axis
=
axis
-
len
(
data1_shape
)
opr_test
(
cases
,
opr
,
ref_fn
=
lambda
x
:
ref_opr
(
x
,
axis
=
axis
).
astype
(
np
.
int32
),
axis
=
axis
,
)
def
test_sum
():
...
...
@@ -137,3 +146,14 @@ def test_normalize():
cases
[
0
][
"input"
][
0
,
0
,
0
,
:]
=
0
cases
[
1
][
"input"
][
0
,
0
,
0
,
:]
=
0
opr_test
(
cases
,
partial
(
F
.
normalize
,
axis
=
3
),
ref_fn
=
partial
(
np_normalize
,
axis
=
3
))
def
test_sum_neg_axis
():
shape
=
(
2
,
3
)
data
=
np
.
random
.
random
(
shape
).
astype
(
np
.
float32
)
for
axis
in
(
-
1
,
-
2
,
(
-
2
,
1
),
(
-
1
,
0
)):
get
=
F
.
sum
(
tensor
(
data
),
axis
=
axis
)
ref
=
np
.
sum
(
data
,
axis
=
axis
)
np
.
testing
.
assert_allclose
(
get
.
numpy
(),
ref
,
rtol
=
1e-6
)
with
pytest
.
raises
(
AssertionError
):
F
.
sum
(
tensor
(
data
),
axis
=
(
-
1
,
1
))
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录