Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
DeepSpeech
提交
6a724565
D
DeepSpeech
项目概览
PaddlePaddle
/
DeepSpeech
大约 1 年 前同步成功
通知
206
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看板
体验新版 GitCode,发现更多精彩内容 >>
提交
6a724565
编写于
4月 20, 2022
作者:
Q
qingen
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[vec][loss] add FocalLoss to deal with class imbalances, test=doc fix #1721
上级
00febff7
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
65 addition
and
2 deletion
+65
-2
paddlespeech/vector/modules/loss.py
paddlespeech/vector/modules/loss.py
+64
-2
paddlespeech/vector/utils/vector_utils.py
paddlespeech/vector/utils/vector_utils.py
+1
-0
未找到文件。
paddlespeech/vector/modules/loss.py
浏览文件 @
6a724565
...
...
@@ -132,7 +132,7 @@ class NCELoss(nn.Layer):
def
forward
(
self
,
output
,
target
):
"""Forward inference
Args:
output (tensor): the model output, which is the input of loss function
"""
...
...
@@ -161,7 +161,7 @@ class NCELoss(nn.Layer):
"""Post processing the score of post model(output of nn) of batchsize data
"""
scores
=
self
.
get_scores
(
idx
,
scores
)
scale
=
paddle
.
to_tensor
([
self
.
Z_offset
],
dtype
=
'float
32
'
)
scale
=
paddle
.
to_tensor
([
self
.
Z_offset
],
dtype
=
'float
64
'
)
scores
=
paddle
.
add
(
scores
,
-
scale
)
prob
=
paddle
.
exp
(
scores
)
if
sep_target
:
...
...
@@ -225,3 +225,65 @@ class NCELoss(nn.Layer):
loss
=
-
(
model_loss
+
noise_loss
)
return
loss
class
FocalLoss
(
nn
.
Layer
):
"""This criterion is a implemenation of Focal Loss, which is proposed in
Focal Loss for Dense Object Detection.
Loss(x, class) = -
\a
lpha (1-softmax(x)[class])^gamma \log(softmax(x)[class])
The losses are averaged across observations for each minibatch.
Args:
alpha(1D Tensor, Variable) : the scalar factor for this criterion
gamma(float, double) : gamma > 0; reduces the relative loss for well-classified examples (p > .5),
putting more focus on hard, misclassified examples
size_average(bool): By default, the losses are averaged over observations for each minibatch.
However, if the field size_average is set to False, the losses are
instead summed for each minibatch.
"""
def
__init__
(
self
,
alpha
=
1
,
gamma
=
0
,
size_average
=
True
,
ignore_index
=-
100
):
super
(
FocalLoss
,
self
).
__init__
()
self
.
alpha
=
alpha
self
.
gamma
=
gamma
self
.
size_average
=
size_average
self
.
ce
=
nn
.
CrossEntropyLoss
(
ignore_index
=
ignore_index
,
reduction
=
"none"
)
def
forward
(
self
,
outputs
,
targets
):
"""Forword inference.
Args:
outputs: input tensor
target: target label tensor
"""
ce_loss
=
self
.
ce
(
outputs
,
targets
)
pt
=
paddle
.
exp
(
-
ce_loss
)
focal_loss
=
self
.
alpha
*
(
1
-
pt
)
**
self
.
gamma
*
ce_loss
if
self
.
size_average
:
return
focal_loss
.
mean
()
else
:
return
focal_loss
.
sum
()
if
__name__
==
"__main__"
:
import
numpy
as
np
from
paddlespeech.vector.utils.vector_utils
import
Q_from_tokens
paddle
.
set_device
(
"cpu"
)
input_data
=
paddle
.
uniform
([
5
,
100
],
dtype
=
"float64"
)
label_data
=
np
.
random
.
randint
(
0
,
100
,
size
=
(
5
)).
astype
(
np
.
int64
)
input
=
paddle
.
to_tensor
(
input_data
)
label
=
paddle
.
to_tensor
(
label_data
)
loss1
=
FocalLoss
()
loss
=
loss1
.
forward
(
input
,
label
)
print
(
"loss: %.5f"
%
(
loss
))
Q
=
Q_from_tokens
(
100
)
loss2
=
NCELoss
(
Q
)
loss
=
loss2
.
forward
(
input
,
label
)
print
(
"loss: %.5f"
%
(
loss
))
paddlespeech/vector/utils/vector_utils.py
浏览文件 @
6a724565
...
...
@@ -11,6 +11,7 @@
# 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
paddle
def
get_chunks
(
seg_dur
,
audio_id
,
audio_duration
):
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录