Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
0032b4a4
P
Paddle
项目概览
PaddlePaddle
/
Paddle
1 年多 前同步成功
通知
2302
Star
20931
Fork
5422
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
0032b4a4
编写于
4月 16, 2018
作者:
G
Guo Sheng
提交者:
GitHub
4月 16, 2018
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #9940 from guoshengCS/add-python-label-smooth
Add python wrapper for label smoothing
上级
a097d082
f086ccb7
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
82 addition
and
0 deletion
+82
-0
doc/fluid/api/layers.rst
doc/fluid/api/layers.rst
+6
-0
python/paddle/fluid/layers/nn.py
python/paddle/fluid/layers/nn.py
+66
-0
python/paddle/fluid/tests/unittests/test_layers.py
python/paddle/fluid/tests/unittests/test_layers.py
+10
-0
未找到文件。
doc/fluid/api/layers.rst
浏览文件 @
0032b4a4
...
...
@@ -473,6 +473,12 @@ multiplex
.. autofunction:: paddle.fluid.layers.multiplex
:noindex:
label_smooth
------------
.. autofunction:: paddle.fluid.layers.label_smooth
:noindex:
ops
===
...
...
python/paddle/fluid/layers/nn.py
浏览文件 @
0032b4a4
...
...
@@ -77,6 +77,7 @@ __all__ = [
'lod_reset'
,
'lrn'
,
'pad'
,
'label_smooth'
,
]
...
...
@@ -3678,3 +3679,68 @@ def pad(x, paddings, pad_value=0., name=None):
attrs
=
{
'paddings'
:
paddings
,
'pad_value'
:
float
(
pad_value
)})
return
out
def
label_smooth
(
label
,
prior_dist
=
None
,
epsilon
=
0.1
,
dtype
=
"float32"
,
name
=
None
):
"""
Label smoothing is a mechanism to regularize the classifier layer and is
called label-smoothing regularization (LSR).
Label smoothing is proposed to encourage the model to be less confident,
since optimizing the log-likelihood of the correct label directly may
cause overfitting and reduce the ability of the model to adapt. Label
smoothing replaces the ground-truth label :math:`y` with the weighted sum
of itself and some fixed distribution :math:`\mu`. For class :math:`k`,
i.e.
.. math::
\\
tilde{y_k} = (1 - \epsilon) * y_k + \epsilon * \mu_k,
where :math:`1 - \epsilon` and :math:`\epsilon` are the weights
respectively, and :math:`
\\
tilde{y}_k` is the smoothed label. Usually
uniform distribution is used for :math:`\mu`.
See more details about label smoothing in https://arxiv.org/abs/1512.00567.
Args:
label(Variable): The input variable containing the label data. The
label data should use one-hot representation.
prior_dist(Variable): The prior distribution to be used to smooth
labels. If not provided, an uniform distribution
is used. The shape of :attr:`prior_dist` should
be :math:`(1, class\_num)`.
epsilon(float): The weight used to mix up the original ground-truth
distribution and the fixed distribution.
dtype(np.dtype|core.VarDesc.VarType|str): The type of data : float32,
float_64, int etc.
name(str|None): A name for this layer(optional). If set None, the layer
will be named automatically.
Returns:
Variable: The tensor variable containing the smoothed labels.
Examples:
.. code-block:: python
label = layers.data(name="label", shape=[1], dtype="float32")
one_hot_label = layers.one_hot(input=label, depth=10)
smooth_label = layers.label_smooth(
label=one_hot_label, epsilon=0.1, dtype="float32")
"""
if
epsilon
>
1.
or
epsilon
<
0.
:
raise
ValueError
(
"The value of epsilon must be between 0 and 1."
)
helper
=
LayerHelper
(
"label_smooth"
,
**
locals
())
label
.
stop_gradient
=
True
smooth_label
=
helper
.
create_tmp_variable
(
dtype
)
helper
.
append_op
(
type
=
"label_smooth"
,
inputs
=
{
"X"
:
label
,
"PriorDist"
:
prior_dist
}
if
prior_dist
else
{
"X"
:
label
},
outputs
=
{
"Out"
:
smooth_label
},
attrs
=
{
"epsilon"
:
float
(
epsilon
)})
return
smooth_label
python/paddle/fluid/tests/unittests/test_layers.py
浏览文件 @
0032b4a4
...
...
@@ -340,6 +340,16 @@ class TestBook(unittest.TestCase):
print
(
layers
.
lod_reset
(
x
=
x
,
y
=
y
))
print
(
str
(
program
))
def
test_label_smooth
(
self
):
program
=
Program
()
with
program_guard
(
program
):
label
=
layers
.
data
(
name
=
"label"
,
shape
=
[
1
],
dtype
=
"float32"
)
one_hot_label
=
layers
.
one_hot
(
input
=
label
,
depth
=
10
)
smooth_label
=
layers
.
label_smooth
(
label
=
one_hot_label
,
epsilon
=
0.1
,
dtype
=
"float32"
)
self
.
assertIsNotNone
(
smooth_label
)
print
(
str
(
program
))
if
__name__
==
'__main__'
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录