Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
MegEngine 天元
MegEngine
提交
7e8f7209
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看板
提交
7e8f7209
编写于
8月 31, 2020
作者:
M
Megvii Engine Team
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
refactor(mge/tensor): tensor reduce supports keepdims
GitOrigin-RevId: 8ed95e0fb8de213481a24c7b8be3bee71a13e469
上级
59dcd3b7
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
56 addition
and
51 deletion
+56
-51
imperative/python/megengine/core/tensor/tensor_wrapper.py
imperative/python/megengine/core/tensor/tensor_wrapper.py
+46
-7
imperative/python/megengine/functional/loss.py
imperative/python/megengine/functional/loss.py
+3
-3
imperative/python/megengine/functional/math.py
imperative/python/megengine/functional/math.py
+5
-39
imperative/python/megengine/functional/nn.py
imperative/python/megengine/functional/nn.py
+1
-1
imperative/python/test/unit/test_tensor_wrapper.py
imperative/python/test/unit/test_tensor_wrapper.py
+1
-1
未找到文件。
imperative/python/megengine/core/tensor/tensor_wrapper.py
浏览文件 @
7e8f7209
...
...
@@ -134,15 +134,54 @@ def _logical_binary_elwise(mode, rev=False):
return
f
def
_remove_axis
(
inp
:
Tensor
,
axis
)
->
Tensor
:
Param
=
builtin
.
AxisAddRemove
.
Param
def
get_axes
():
if
axis
is
None
:
return
[
i
for
i
,
s
in
enumerate
(
inp
.
shape
)
if
s
==
1
]
try
:
return
[
int
(
axis
)]
except
(
TypeError
,
ValueError
):
pass
return
list
(
map
(
int
,
axis
))
axis
=
get_axes
()
axis
=
sorted
(
i
+
inp
.
ndim
if
i
<
0
else
i
for
i
in
axis
)
axis
=
[
a
-
i
for
i
,
a
in
enumerate
(
axis
)]
param
=
Param
(
*
map
(
builtin
.
AxisAddRemove
.
AxisDesc
.
make_remove
,
axis
))
op
=
builtin
.
AxisAddRemove
(
param
=
param
)
(
result
,)
=
apply
(
op
,
inp
)
return
result
def
_reduce
(
mode
):
def
f
(
self
,
axis
=
None
):
inp
=
self
def
f
(
self
,
axis
=
None
,
keepdims
:
bool
=
False
):
data
=
self
(
data
,)
=
utils
.
convert_inputs
(
data
)
if
axis
is
None
:
inp
=
self
.
flatten
()
axis
=
0
op
=
builtin
.
Reduce
(
mode
=
mode
,
axis
=
axis
)
(
result
,)
=
utils
.
convert_inputs
(
inp
)
(
result
,)
=
apply
(
op
,
result
)
data
=
data
.
reshape
(
-
1
)
assert
not
keepdims
,
"can not set axis=None and keepdims=True"
op
=
builtin
.
Reduce
(
mode
=
mode
,
axis
=
0
)
(
result
,)
=
apply
(
op
,
data
)
elif
isinstance
(
axis
,
collections
.
Iterable
):
axis
=
list
(
axis
)
axis
.
sort
(
reverse
=
True
)
for
ai
in
axis
:
op
=
builtin
.
Reduce
(
mode
=
mode
,
axis
=
ai
)
(
data
,)
=
apply
(
op
,
data
)
if
not
keepdims
:
data
=
_remove_axis
(
data
,
ai
)
result
=
data
else
:
op
=
builtin
.
Reduce
(
mode
=
mode
,
axis
=
axis
)
(
result
,)
=
apply
(
op
,
data
)
if
not
keepdims
:
result
=
_remove_axis
(
result
,
axis
)
return
result
return
f
...
...
imperative/python/megengine/functional/loss.py
浏览文件 @
7e8f7209
...
...
@@ -176,15 +176,15 @@ def cross_entropy_with_softmax(
num_classes
=
pred
.
shape
[
axis
]
# Denominator of the softmax
offset
=
pred
.
max
(
axis
=
axis
).
detach
()
offset
=
pred
.
max
(
axis
=
axis
,
keepdims
=
True
).
detach
()
pred
=
pred
-
offset
down
=
exp
(
pred
).
sum
(
axis
=
axis
)
down
=
exp
(
pred
).
sum
(
axis
=
axis
,
keepdims
=
True
)
up
=
indexing_one_hot
(
pred
,
label
,
axis
)
if
label_smooth
!=
0
:
factor
=
label_smooth
/
num_classes
up
=
up
*
(
1
-
label_smooth
)
+
pred
.
sum
(
axis
=
axis
)
*
factor
up
=
up
*
(
1
-
label_smooth
)
+
pred
.
sum
(
axis
=
axis
,
keepdims
=
True
)
*
factor
return
(
log
(
down
)
-
up
).
mean
()
...
...
imperative/python/megengine/functional/math.py
浏览文件 @
7e8f7209
...
...
@@ -117,40 +117,6 @@ def sign(inp: Tensor):
raise
NotImplementedError
def
_reduce
(
data
,
*
,
mode
,
axis
:
Optional
[
Union
[
int
,
Sequence
[
int
]]]
=
None
,
keepdims
:
bool
=
False
):
(
data
,)
=
utils
.
convert_inputs
(
data
)
if
axis
is
None
:
data
=
data
.
reshape
(
-
1
)
assert
not
keepdims
,
"can not set axis=None and keepdims=True"
op
=
builtin
.
Reduce
(
mode
=
mode
,
axis
=
0
)
(
result
,)
=
apply
(
op
,
data
)
elif
isinstance
(
axis
,
collections
.
Iterable
):
axis
=
list
(
axis
)
axis
.
sort
(
reverse
=
True
)
for
ai
in
axis
:
op
=
builtin
.
Reduce
(
mode
=
mode
,
axis
=
ai
)
(
data
,)
=
apply
(
op
,
data
)
if
not
keepdims
:
data
=
remove_axis
(
data
,
ai
)
result
=
data
else
:
op
=
builtin
.
Reduce
(
mode
=
mode
,
axis
=
axis
)
(
result
,)
=
apply
(
op
,
data
)
if
not
keepdims
:
result
=
remove_axis
(
result
,
axis
)
return
result
def
sum
(
inp
:
Tensor
,
axis
:
Optional
[
Union
[
int
,
Sequence
[
int
]]]
=
None
,
...
...
@@ -182,7 +148,7 @@ def sum(
[21]
"""
return
_reduce
(
inp
,
mode
=
"SUM"
,
axis
=
axis
,
keepdims
=
keepdims
)
return
inp
.
sum
(
axis
=
axis
,
keepdims
=
keepdims
)
def
prod
(
...
...
@@ -215,7 +181,7 @@ def prod(
[720]
"""
return
_reduce
(
inp
,
mode
=
"PRODUCT"
,
axis
=
axis
,
keepdims
=
keepdims
)
return
inp
.
prod
(
axis
=
axis
,
keepdims
=
keepdims
)
def
mean
(
...
...
@@ -248,7 +214,7 @@ def mean(
[3.5]
"""
return
_reduce
(
inp
,
mode
=
"MEAN"
,
axis
=
axis
,
keepdims
=
keepdims
)
return
inp
.
astype
(
"float32"
).
mean
(
axis
=
axis
,
keepdims
=
keepdims
)
def
median
(
...
...
@@ -362,7 +328,7 @@ def min(
[1]
"""
return
_reduce
(
inp
,
mode
=
"MIN"
,
axis
=
axis
,
keepdims
=
keepdims
)
return
inp
.
min
(
axis
=
axis
,
keepdims
=
keepdims
)
def
max
(
...
...
@@ -394,7 +360,7 @@ def max(
[6]
"""
return
_reduce
(
inp
,
mode
=
"MAX"
,
axis
=
axis
,
keepdims
=
keepdims
)
return
inp
.
max
(
axis
=
axis
,
keepdims
=
keepdims
)
def
norm
(
...
...
imperative/python/megengine/functional/nn.py
浏览文件 @
7e8f7209
...
...
@@ -580,7 +580,7 @@ def softmax(inp: Tensor, axis: Optional[int] = None) -> Tensor:
"""
if
axis
is
None
:
axis
=
_get_softmax_axis
(
len
(
inp
.
shape
))
offset
=
inp
.
max
(
axis
=
axis
).
detach
()
offset
=
inp
.
max
(
axis
=
axis
,
keepdims
=
True
).
detach
()
cached
=
exp
(
inp
-
offset
)
down
=
sum
(
cached
,
axis
=
axis
,
keepdims
=
True
)
return
cached
/
down
...
...
imperative/python/test/unit/test_tensor_wrapper.py
浏览文件 @
7e8f7209
...
...
@@ -38,7 +38,7 @@ def test_reduce():
for
m
in
[
"sum"
,
"prod"
,
"min"
,
"max"
,
"mean"
]:
x_np
=
np
.
random
.
rand
(
10
).
astype
(
"float32"
)
x
=
TensorWrapper
(
x_np
)
y
=
getattr
(
x
,
m
)(
-
1
)
y
=
getattr
(
x
,
m
)(
axis
=-
1
,
keepdims
=
True
)
np
.
testing
.
assert_almost_equal
(
y
.
numpy
(),
getattr
(
x_np
,
m
)(
-
1
),
decimal
=
6
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录