Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
DeepSpeech
提交
9cf8c1a5
D
DeepSpeech
项目概览
PaddlePaddle
/
DeepSpeech
大约 2 年 前同步成功
通知
210
Star
8425
Fork
1598
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
245
列表
看板
标记
里程碑
合并请求
3
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
DeepSpeech
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
245
Issue
245
列表
看板
标记
里程碑
合并请求
3
合并请求
3
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
9cf8c1a5
编写于
3月 16, 2021
作者:
H
Hui Zhang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add cmvn and label smoothing loss layer
上级
4c8c2178
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
133 addition
and
1 deletion
+133
-1
deepspeech/modules/cmvn.py
deepspeech/modules/cmvn.py
+54
-0
deepspeech/modules/loss.py
deepspeech/modules/loss.py
+79
-1
未找到文件。
deepspeech/modules/cmvn.py
0 → 100644
浏览文件 @
9cf8c1a5
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import
logging
import
paddle
from
paddle
import
nn
from
paddle.nn
import
functional
as
F
from
paddle.nn
import
initializer
as
I
logger
=
logging
.
getLogger
(
__name__
)
__all__
=
[
'GlobalCMVN'
]
class
GlobalCMVN
(
nn
.
Layer
):
def
__init__
(
self
,
mean
:
paddle
.
Tensor
,
istd
:
paddle
.
Tensor
,
norm_var
:
bool
=
True
):
"""
Args:
mean (paddle.Tensor): mean stats
istd (paddle.Tensor): inverse std, std which is 1.0 / std
"""
super
().
__init__
()
assert
mean
.
shape
==
istd
.
shape
self
.
norm_var
=
norm_var
# The buffer can be accessed from this module using self.mean
self
.
register_buffer
(
"mean"
,
mean
)
self
.
register_buffer
(
"istd"
,
istd
)
def
forward
(
self
,
x
:
paddle
.
Tensor
):
"""
Args:
x (paddle.Tensor): (batch, max_len, feat_dim)
Returns:
(paddle.Tensor): normalized feature
"""
x
=
x
-
self
.
mean
if
self
.
norm_var
:
x
=
x
*
self
.
istd
return
x
deepspeech/modules/loss.py
浏览文件 @
9cf8c1a5
...
...
@@ -21,7 +21,7 @@ from paddle.nn import initializer as I
logger
=
logging
.
getLogger
(
__name__
)
__all__
=
[
'CTCLoss'
]
__all__
=
[
'CTCLoss'
,
"LabelSmoothingLoss"
]
# TODO(Hui Zhang): remove this hack, when `norm_by_times=True` is added
...
...
@@ -80,3 +80,81 @@ class CTCLoss(nn.Layer):
# Batch-size average
# loss = loss / paddle.shape(logits)[1]
return
loss
class
LabelSmoothingLoss
(
nn
.
Layer
):
"""Label-smoothing loss.
In a standard CE loss, the label's data distribution is:
[0,1,2] ->
[
[1.0, 0.0, 0.0],
[0.0, 1.0, 0.0],
[0.0, 0.0, 1.0],
]
In the smoothing version CE Loss,some probabilities
are taken from the true label prob (1.0) and are divided
among other labels.
e.g.
smoothing=0.1
[0,1,2] ->
[
[0.9, 0.05, 0.05],
[0.05, 0.9, 0.05],
[0.05, 0.05, 0.9],
]
"""
def
__init__
(
self
,
size
:
int
,
padding_idx
:
int
,
smoothing
:
float
,
normalize_length
:
bool
=
False
):
"""Label-smoothing loss.
Args:
size (int): the number of class
padding_idx (int): padding class id which will be ignored for loss
smoothing (float): smoothing rate (0.0 means the conventional CE)
normalize_length (bool): True, normalize loss by sequence length; False, normalize loss by batch size. Defaults to False.
"""
super
().
__init__
()
self
.
size
=
size
self
.
padding_idx
=
padding_idx
self
.
smoothing
=
smoothing
self
.
confidence
=
1.0
-
smoothing
self
.
normalize_length
=
normalize_length
self
.
criterion
=
nn
.
KLDivLoss
(
reduction
=
"none"
)
def
forward
(
self
,
x
:
paddle
.
Tensor
,
target
:
paddle
.
Tensor
)
->
paddle
.
Tensor
:
"""Compute loss between x and target.
The model outputs and data labels tensors are flatten to
(batch*seqlen, class) shape and a mask is applied to the
padding part which should not be calculated for loss.
Args:
x (paddle.Tensor): prediction (batch, seqlen, class)
target (paddle.Tensor):
target signal masked with self.padding_id (batch, seqlen)
Returns:
loss (paddle.Tensor) : The KL loss, scalar float value
"""
B
,
T
,
D
=
paddle
.
shape
(
x
)
assert
D
==
self
.
size
x
=
x
.
reshape
((
-
1
,
self
.
size
))
target
=
target
.
reshape
(
-
1
)
# use zeros_like instead of torch.no_grad() for true_dist,
# since no_grad() can not be exported by JIT
true_dist
=
paddle
.
full_like
(
x
,
self
.
smoothing
/
(
self
.
size
-
1
))
ignore
=
target
==
self
.
padding_idx
# (B,)
ignore
=
ignore
.
cast
(
target
.
dtype
)
target
=
target
*
(
1
-
ignore
)
# avoid -1 index
true_dist
+=
F
.
one_hot
(
target
,
self
.
size
)
*
self
.
confidence
kl
=
self
.
criterion
(
F
.
log_softmax
(
x
,
axis
=
1
),
true_dist
)
total
=
len
(
target
)
-
int
(
ignore
.
sum
())
denom
=
total
if
self
.
normalize_length
else
B
numer
=
(
kl
*
(
1
-
ignore
)).
sum
()
return
numer
/
denom
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录