Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
hapi
提交
d2c796bd
H
hapi
项目概览
PaddlePaddle
/
hapi
通知
11
Star
2
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
4
列表
看板
标记
里程碑
合并请求
7
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
H
hapi
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
4
Issue
4
列表
看板
标记
里程碑
合并请求
7
合并请求
7
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
d2c796bd
编写于
4月 24, 2020
作者:
L
LielinJiang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
move loss to loss.py
上级
5ac55a4d
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
103 addition
and
0 deletion
+103
-0
hapi/loss.py
hapi/loss.py
+103
-0
未找到文件。
hapi/loss.py
0 → 100644
浏览文件 @
d2c796bd
# Copyright (c) 2019 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.
from
__future__
import
absolute_import
import
os
from
paddle
import
fluid
from
paddle.fluid.framework
import
in_dygraph_mode
,
Variable
from
paddle.fluid.dygraph.base
import
to_variable
__all__
=
[
'Loss'
,
'CrossEntropy'
,
'SoftmaxWithCrossEntropy'
]
def
to_list
(
value
):
if
value
is
None
:
return
value
if
isinstance
(
value
,
(
list
,
tuple
)):
return
list
(
value
)
return
[
value
]
class
Loss
(
object
):
"""
Base class for loss, encapsulates loss logic and APIs
Usage:
custom_loss = CustomLoss()
loss = custom_loss(inputs, labels)
"""
def
__init__
(
self
,
average
=
True
):
super
(
Loss
,
self
).
__init__
()
self
.
average
=
average
def
forward
(
self
,
outputs
,
labels
):
raise
NotImplementedError
()
def
__call__
(
self
,
outputs
,
labels
=
None
):
labels
=
to_list
(
labels
)
if
in_dygraph_mode
()
and
labels
:
labels
=
[
to_variable
(
l
)
for
l
in
labels
]
losses
=
to_list
(
self
.
forward
(
to_list
(
outputs
),
labels
))
if
self
.
average
:
losses
=
[
fluid
.
layers
.
reduce_mean
(
l
)
for
l
in
losses
]
else
:
losses
=
[
fluid
.
layers
.
reduce_sum
(
l
)
for
l
in
losses
]
return
losses
class
CrossEntropy
(
Loss
):
"""
Args:
input (list[Variable]): Input tensor, the data type is float32,
float64, int32, int64.
label (list[Variable]): Label tensor, the data type is float32,
float64, int32, int64.
average (bool, optional): Indicate whether to average the loss, Default: True.
Returns:
list[Variable]: The tensor variable storing the cross_entropy_loss of inputs and labels.
"""
def
__init__
(
self
,
average
=
True
):
super
(
CrossEntropy
,
self
).
__init__
()
def
forward
(
self
,
outputs
,
labels
):
return
[
fluid
.
layers
.
cross_entropy
(
o
,
l
)
for
o
,
l
in
zip
(
outputs
,
labels
)
]
class
SoftmaxWithCrossEntropy
(
Loss
):
"""
this op combined softmax and cross entropy.
Args:
input (list[Variable]): Input tensor, the data type is float32,
float64, int32, int64.
label (list[Variable]): Label tensor, the data type is float32,
float64, int32, int64.
average (bool, optional): Indicate whether to average the loss, Default: True.
Returns:
list[Variable]: The tensor variable storing the cross_entropy_loss of inputs and labels.
"""
def
__init__
(
self
,
average
=
True
):
super
(
SoftmaxWithCrossEntropy
,
self
).
__init__
()
def
forward
(
self
,
outputs
,
labels
):
return
[
fluid
.
layers
.
softmax_with_cross_entropy
(
o
,
l
,
return_softmax
=
False
)
for
o
,
l
in
zip
(
outputs
,
labels
)
]
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录