Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
a4c06083
P
Paddle
项目概览
机器未来
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
a4c06083
编写于
7月 23, 2018
作者:
B
baiyf
提交者:
GitHub
7月 23, 2018
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Expose rank_loss op Python API (#12132)
* expose rank_loss python api
上级
1e417b93
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
102 addition
and
0 deletion
+102
-0
doc/fluid/api/layers.rst
doc/fluid/api/layers.rst
+8
-0
python/paddle/fluid/layers/nn.py
python/paddle/fluid/layers/nn.py
+72
-0
python/paddle/fluid/tests/unittests/test_layers.py
python/paddle/fluid/tests/unittests/test_layers.py
+22
-0
未找到文件。
doc/fluid/api/layers.rst
浏览文件 @
a4c06083
...
...
@@ -1768,3 +1768,11 @@ reverse
.. autofunction:: paddle.fluid.layers.reverse
:noindex:
.. _api_fluid_layers_rank_loss:
rank_loss
-------
.. autofunction:: paddle.fluid.layers.rank_loss
:noindex:
python/paddle/fluid/layers/nn.py
浏览文件 @
a4c06083
...
...
@@ -110,6 +110,7 @@ __all__ = [
'relu'
,
'log'
,
'crop'
,
'rank_loss'
,
]
...
...
@@ -5282,3 +5283,74 @@ def crop(x, shape=None, offsets=None, name=None):
outputs
=
{
'Out'
:
out
},
attrs
=
None
if
len
(
attrs
)
==
0
else
attrs
)
return
out
def
rank_loss
(
label
,
left
,
right
,
name
=
None
):
"""
**Rank loss layer for RankNet**
RankNet(http://icml.cc/2015/wp-content/uploads/2015/06/icml_ranking.pdf)
is a pairwise ranking model with a training sample consisting of a pair
of documents, A and B. Label P indicates whether A is ranked higher than B
or not:
P = {0, 1} or {0, 0.5, 1}, where 0.5 means that there is no information
about the rank of the input pair.
Rank loss layer takes three inputs: left (o_i), right (o_j) and
label (P_{i,j}). The inputs respectively represent RankNet's output scores
for documents A and B and the value of label P. The following equation
computes rank loss C_{i,j} from the inputs:
$$
C_{i,j} = -
\t
ilde{P_{ij}} * o_{i,j} + \log(1 + e^{o_{i,j}})
\\
o_{i,j} = o_i - o_j
\\
\t
ilde{P_{i,j}} = \left \{0, 0.5, 1
\r
ight \} \ or \ \left \{0, 1
\r
ight \}
$$
Rank loss layer takes batch inputs with size batch_size (batch_size >= 1).
Args:
label (Variable): Indicats whether A ranked higher than B or not.
left (Variable): RankNet's output score for doc A.
right (Variable): RankNet's output score for doc B.
name(str|None): A name for this layer(optional). If set None, the layer
will be named automatically.
Returns:
list: The value of rank loss.
Raises:
ValueError: Any of label, left, and right is not a variable.
Examples:
.. code-block:: python
label = fluid.layers.data(name="label", shape=[4, 1], dtype="float32")
left = fluid.layers.data(name="left", shape=[4, 1], dtype="float32")
right = fluid.layers.data(name="right", shape=[4, 1], dtype="float32")
out = fluid.layers.rank_loss(label, left, right)
"""
helper
=
LayerHelper
(
'rank_loss'
,
**
locals
())
if
not
(
isinstance
(
label
,
Variable
)):
raise
ValueError
(
"The label should be a Variable"
)
if
not
(
isinstance
(
left
,
Variable
)):
raise
ValueError
(
"The left should be a Variable"
)
if
not
(
isinstance
(
right
,
Variable
)):
raise
ValueError
(
"The right should be a Variable"
)
out
=
helper
.
create_tmp_variable
(
"float32"
)
helper
.
append_op
(
type
=
'rank_loss'
,
inputs
=
{
"Label"
:
label
,
"Left"
:
left
,
"Right"
:
right
},
outputs
=
{
'Out'
:
out
})
return
out
python/paddle/fluid/tests/unittests/test_layers.py
浏览文件 @
a4c06083
...
...
@@ -443,6 +443,28 @@ class TestBook(unittest.TestCase):
self
.
assertIsNotNone
(
ids
)
print
(
str
(
program
))
def
test_rank_loss
(
self
):
program
=
Program
()
with
program_guard
(
program
):
label
=
layers
.
data
(
name
=
'label'
,
append_batch_size
=
False
,
shape
=
[
16
,
1
],
dtype
=
"float32"
)
left
=
layers
.
data
(
name
=
'left'
,
append_batch_size
=
False
,
shape
=
[
16
,
1
],
dtype
=
"float32"
)
right
=
layers
.
data
(
name
=
'right'
,
append_batch_size
=
False
,
shape
=
[
16
,
1
],
dtype
=
"float32"
)
out
=
layers
.
rank_loss
(
label
,
left
,
right
,
name
=
"rank_loss"
)
self
.
assertIsNotNone
(
out
)
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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录