Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
MegEngine 天元
MegEngine
提交
5eca4da3
MegEngine
项目概览
MegEngine 天元
/
MegEngine
1 年多 前同步成功
通知
404
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看板
提交
5eca4da3
编写于
5月 21, 2020
作者:
M
Megvii Engine Team
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat(mge/functional): add softplus function
GitOrigin-RevId: 26c1ab74161e07089f846ddb4a610d6279d9571f
上级
855c49ca
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
37 addition
and
1 deletion
+37
-1
python_module/megengine/functional/__init__.py
python_module/megengine/functional/__init__.py
+1
-0
python_module/megengine/functional/nn.py
python_module/megengine/functional/nn.py
+20
-1
python_module/test/unit/functional/test_functional.py
python_module/test/unit/functional/test_functional.py
+16
-0
未找到文件。
python_module/megengine/functional/__init__.py
浏览文件 @
5eca4da3
...
@@ -72,6 +72,7 @@ from .nn import (
...
@@ -72,6 +72,7 @@ from .nn import (
roi_align
,
roi_align
,
roi_pooling
,
roi_pooling
,
softmax
,
softmax
,
softplus
,
warp_perspective
,
warp_perspective
,
)
)
from
.quantized
import
conv_bias_activation
from
.quantized
import
conv_bias_activation
...
...
python_module/megengine/functional/nn.py
浏览文件 @
5eca4da3
...
@@ -18,7 +18,8 @@ from ..jit import barrier, mark_impure
...
@@ -18,7 +18,8 @@ from ..jit import barrier, mark_impure
from
..random
import
uniform
from
..random
import
uniform
from
..utils.types
import
_pair
,
_pair_nonzero
from
..utils.types
import
_pair
,
_pair_nonzero
from
.debug_param
import
get_conv_execution_strategy
from
.debug_param
import
get_conv_execution_strategy
from
.tensor
import
concat
from
.elemwise
import
exp
,
log
from
.tensor
import
concat
,
where
from
.utils
import
_decide_comp_node_and_comp_graph
from
.utils
import
_decide_comp_node_and_comp_graph
...
@@ -267,6 +268,24 @@ def leaky_relu(inp: Tensor, negative_slope: float = 0.01) -> Tensor:
...
@@ -267,6 +268,24 @@ def leaky_relu(inp: Tensor, negative_slope: float = 0.01) -> Tensor:
)
)
@
wrap_io_tensor
def
softplus
(
inp
:
Tensor
,
beta
:
float
=
1
,
threshold
:
float
=
20
)
->
Tensor
:
r
"""
Performs the elementwise function:
.. math::
\mathsf{softplus}(x) = \log(1+\exp(\beta x)) / \beta.
For numerical stability the identity function is used when :math:`\beta x > \textrm{threshold}`.
"""
mask
=
beta
*
inp
<=
threshold
out
=
log
(
1
+
exp
(
beta
*
inp
))
/
beta
out
=
where
(
mask
,
out
,
inp
)
return
out
@
wrap_io_tensor
@
wrap_io_tensor
def
flatten
(
inp
:
Tensor
,
start_axis
:
int
=
0
,
end_axis
:
int
=
-
1
)
->
Tensor
:
def
flatten
(
inp
:
Tensor
,
start_axis
:
int
=
0
,
end_axis
:
int
=
-
1
)
->
Tensor
:
r
"""
r
"""
...
...
python_module/test/unit/functional/test_functional.py
浏览文件 @
5eca4da3
...
@@ -439,3 +439,19 @@ def test_conv_bias():
...
@@ -439,3 +439,19 @@ def test_conv_bias():
run
(
10
,
36
,
8
,
46
,
26
,
2
,
2
,
2
,
1
,
1
,
2
,
False
,
"RELU"
)
run
(
10
,
36
,
8
,
46
,
26
,
2
,
2
,
2
,
1
,
1
,
2
,
False
,
"RELU"
)
run
(
10
,
36
,
8
,
46
,
26
,
2
,
2
,
2
,
1
,
1
,
2
,
True
,
"RELU"
)
run
(
10
,
36
,
8
,
46
,
26
,
2
,
2
,
2
,
1
,
1
,
2
,
True
,
"RELU"
)
def
test_softplus
():
x
=
np
.
arange
(
1000
).
astype
(
np
.
float32
)
out
=
F
.
softplus
(
tensor
(
x
))
mask
=
x
<=
20
with
np
.
errstate
(
over
=
"ignore"
):
expected
=
np
.
where
(
mask
,
np
.
log
(
1
+
np
.
exp
(
x
)),
x
)
assertTensorClose
(
out
,
expected
)
beta
=
2
out
=
F
.
softplus
(
tensor
(
x
),
beta
=
beta
,
threshold
=
30
)
mask
=
beta
*
x
<=
30
# ignore overflow
with
np
.
errstate
(
over
=
"ignore"
):
expected
=
np
.
where
(
mask
,
np
.
log
(
1
+
np
.
exp
(
x
*
beta
))
/
beta
,
x
)
assertTensorClose
(
out
,
expected
)
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录