Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
MegEngine 天元
MegEngine
提交
f7d8b516
MegEngine
项目概览
MegEngine 天元
/
MegEngine
接近 2 年 前同步成功
通知
414
Star
4708
Fork
583
代码
文件
提交
分支
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看板
提交
f7d8b516
编写于
5月 18, 2020
作者:
M
Megvii Engine Team
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat(mge/functional): add smooth l1 loss
GitOrigin-RevId: c1437788d732e55ca3f99557c8049d556f4d2b67
上级
3c49d1d3
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
63 addition
and
0 deletion
+63
-0
python_module/megengine/functional/__init__.py
python_module/megengine/functional/__init__.py
+1
-0
python_module/megengine/functional/loss.py
python_module/megengine/functional/loss.py
+49
-0
python_module/test/unit/functional/test_functional.py
python_module/test/unit/functional/test_functional.py
+13
-0
未找到文件。
python_module/megengine/functional/__init__.py
浏览文件 @
f7d8b516
...
...
@@ -46,6 +46,7 @@ from .loss import (
hinge_loss
,
l1_loss
,
nll_loss
,
smooth_l1_loss
,
square_loss
,
triplet_margin_loss
,
)
...
...
python_module/megengine/functional/loss.py
浏览文件 @
f7d8b516
...
...
@@ -340,3 +340,52 @@ def hinge_loss(pred: Tensor, label: Tensor, norm: str = "L1") -> Tensor:
return
loss
.
sum
(
axis
=
1
).
mean
()
else
:
return
(
loss
**
2
).
sum
(
axis
=
1
).
mean
()
def
smooth_l1_loss
(
pred
:
Tensor
,
label
:
Tensor
)
->
Tensor
:
r
"""
Caculate the smooth l1 loss proposed in `Fast R-CNN paper by Ross Girshick`.
The smooth l1 loss can be described as:
.. math::
\text{loss}(x, y) = \frac{1}{n} \sum_{i} l_{i}
where :math:`l_{i}` is given by:
.. math::
l_{i} =
\begin{cases}
0.5 (x_i - y_i)^2, & \text{if } |x_i - y_i| < 1 \\
|x_i - y_i| - 0.5, & \text{otherwise }
\end{cases}
:param pred: The predicted result from model.
:param label: The ground truth to compare.
Examples:
.. testcode::
from megengine import tensor
import megengine.functional as F
pred = tensor([[0.5, -0.5, 0.1], [-0.6, 0.7, 0.8]])
label = tensor([[0.4, 1.5, 1.2], [0., 0.1, 2.2]])
loss = F.smooth_l1_loss(pred, label)
print(loss.numpy())
Outputs:
.. testoutput::
[0.5608334]
"""
diff
=
abs
(
pred
-
label
)
l2_loss
=
0.5
*
(
diff
**
2
)
l1_loss
=
diff
-
0.5
mask
=
diff
<
1
loss
=
where
(
mask
,
l2_loss
,
l1_loss
)
return
loss
.
mean
()
python_module/test/unit/functional/test_functional.py
浏览文件 @
f7d8b516
...
...
@@ -362,6 +362,19 @@ def test_hinge_loss():
opr_test
(
cases
,
hinge_loss_with_l2_norm
)
def
test_smooth_l1_loss
():
np
.
random
.
seed
(
123
)
cases
=
[]
for
shape
in
[(
2
,
2
),
(
2
,
3
)]:
data
=
np
.
random
.
uniform
(
size
=
shape
).
astype
(
np
.
float32
)
label
=
np
.
random
.
uniform
(
size
=
shape
).
astype
(
np
.
float32
)
diff
=
np
.
abs
(
data
-
label
)
expect
=
np
.
where
(
diff
<
1
,
0.5
*
diff
**
2
,
diff
-
0.5
).
mean
()
cases
.
append
({
"input"
:
[
data
,
label
],
"output"
:
tensor
(
expect
)})
opr_test
(
cases
,
F
.
smooth_l1_loss
)
@
pytest
.
mark
.
skip
def
test_conv_bias
():
inp_scale
=
0.01
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录