Skip to content

Commit a4c0608

Browse files
author
baiyf
authored
Expose rank_loss op Python API (#12132)
* expose rank_loss python api
1 parent 1e417b9 commit a4c0608

File tree

3 files changed

+102
-0
lines changed

3 files changed

+102
-0
lines changed

doc/fluid/api/layers.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1768,3 +1768,11 @@ reverse
17681768
.. autofunction:: paddle.fluid.layers.reverse
17691769
:noindex:
17701770

1771+
.. _api_fluid_layers_rank_loss:
1772+
1773+
rank_loss
1774+
-------
1775+
1776+
.. autofunction:: paddle.fluid.layers.rank_loss
1777+
:noindex:
1778+

python/paddle/fluid/layers/nn.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@
110110
'relu',
111111
'log',
112112
'crop',
113+
'rank_loss',
113114
]
114115

115116

@@ -5282,3 +5283,74 @@ def crop(x, shape=None, offsets=None, name=None):
52825283
outputs={'Out': out},
52835284
attrs=None if len(attrs) == 0 else attrs)
52845285
return out
5286+
5287+
5288+
def rank_loss(label, left, right, name=None):
5289+
"""
5290+
**Rank loss layer for RankNet**
5291+
5292+
RankNet(http://icml.cc/2015/wp-content/uploads/2015/06/icml_ranking.pdf)
5293+
is a pairwise ranking model with a training sample consisting of a pair
5294+
of documents, A and B. Label P indicates whether A is ranked higher than B
5295+
or not:
5296+
5297+
P = {0, 1} or {0, 0.5, 1}, where 0.5 means that there is no information
5298+
about the rank of the input pair.
5299+
5300+
Rank loss layer takes three inputs: left (o_i), right (o_j) and
5301+
label (P_{i,j}). The inputs respectively represent RankNet's output scores
5302+
for documents A and B and the value of label P. The following equation
5303+
computes rank loss C_{i,j} from the inputs:
5304+
5305+
$$
5306+
C_{i,j} = -\tilde{P_{ij}} * o_{i,j} + \log(1 + e^{o_{i,j}}) \\
5307+
o_{i,j} = o_i - o_j \\
5308+
\tilde{P_{i,j}} = \left \{0, 0.5, 1 \right \} \ or \ \left \{0, 1 \right \}
5309+
$$
5310+
5311+
Rank loss layer takes batch inputs with size batch_size (batch_size >= 1).
5312+
5313+
Args:
5314+
label (Variable): Indicats whether A ranked higher than B or not.
5315+
left (Variable): RankNet's output score for doc A.
5316+
right (Variable): RankNet's output score for doc B.
5317+
name(str|None): A name for this layer(optional). If set None, the layer
5318+
will be named automatically.
5319+
5320+
Returns:
5321+
list: The value of rank loss.
5322+
5323+
Raises:
5324+
ValueError: Any of label, left, and right is not a variable.
5325+
5326+
Examples:
5327+
5328+
.. code-block:: python
5329+
5330+
label = fluid.layers.data(name="label", shape=[4, 1], dtype="float32")
5331+
left = fluid.layers.data(name="left", shape=[4, 1], dtype="float32")
5332+
right = fluid.layers.data(name="right", shape=[4, 1], dtype="float32")
5333+
out = fluid.layers.rank_loss(label, left, right)
5334+
5335+
5336+
"""
5337+
helper = LayerHelper('rank_loss', **locals())
5338+
5339+
if not (isinstance(label, Variable)):
5340+
raise ValueError("The label should be a Variable")
5341+
5342+
if not (isinstance(left, Variable)):
5343+
raise ValueError("The left should be a Variable")
5344+
5345+
if not (isinstance(right, Variable)):
5346+
raise ValueError("The right should be a Variable")
5347+
5348+
out = helper.create_tmp_variable("float32")
5349+
5350+
helper.append_op(
5351+
type='rank_loss',
5352+
inputs={"Label": label,
5353+
"Left": left,
5354+
"Right": right},
5355+
outputs={'Out': out})
5356+
return out

python/paddle/fluid/tests/unittests/test_layers.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,28 @@ def test_argsort(self):
443443
self.assertIsNotNone(ids)
444444
print(str(program))
445445

446+
def test_rank_loss(self):
447+
program = Program()
448+
with program_guard(program):
449+
label = layers.data(
450+
name='label',
451+
append_batch_size=False,
452+
shape=[16, 1],
453+
dtype="float32")
454+
left = layers.data(
455+
name='left',
456+
append_batch_size=False,
457+
shape=[16, 1],
458+
dtype="float32")
459+
right = layers.data(
460+
name='right',
461+
append_batch_size=False,
462+
shape=[16, 1],
463+
dtype="float32")
464+
out = layers.rank_loss(label, left, right, name="rank_loss")
465+
self.assertIsNotNone(out)
466+
print(str(program))
467+
446468

447469
if __name__ == '__main__':
448470
unittest.main()

0 commit comments

Comments
 (0)