lambda_rank.py 1.4 KB
Newer Older
W
wangmeng28 已提交
1 2 3 4
"""
LambdaRank is a listwise rank model.
https://papers.nips.cc/paper/2971-learning-to-rank-with-nonsmooth-cost-functions.pdf
"""
C
caoying03 已提交
5
import paddle.v2 as paddle
D
dong zhihong 已提交
6 7


W
wangmeng28 已提交
8
def lambda_rank(input_dim, is_infer=False):
D
dzhwinter 已提交
9
    """
W
wangmeng28 已提交
10
    The input data and label for LambdaRank must be sequences.
C
caoying03 已提交
11

D
dzhwinter 已提交
12 13 14
    parameters :
      input_dim, one document's dense feature vector dimension

P
peterzhang2029 已提交
15
    The format of the dense_vector_sequence is as follows:
C
caoying03 已提交
16
    [[f, ...], [f, ...], ...], f is a float or an int number
D
dzhwinter 已提交
17
    """
18 19 20
    data = paddle.layer.data("data",
                             paddle.data_type.dense_vector_sequence(input_dim))

C
caoying03 已提交
21
    # Define the hidden layer.
C
caoying03 已提交
22 23 24 25 26
    hd1 = paddle.layer.fc(
        input=data,
        size=128,
        act=paddle.activation.Tanh(),
        param_attr=paddle.attr.Param(initial_std=0.01))
C
caoying03 已提交
27

C
caoying03 已提交
28 29 30 31 32 33 34 35 36 37
    hd2 = paddle.layer.fc(
        input=hd1,
        size=10,
        act=paddle.activation.Tanh(),
        param_attr=paddle.attr.Param(initial_std=0.01))
    output = paddle.layer.fc(
        input=hd2,
        size=1,
        act=paddle.activation.Linear(),
        param_attr=paddle.attr.Param(initial_std=0.01))
D
dzhwinter 已提交
38

P
peterzhang2029 已提交
39
    if not is_infer:
W
wangmeng28 已提交
40 41 42
        label = paddle.layer.data("label",
                                  paddle.data_type.dense_vector_sequence(1))

P
peterzhang2029 已提交
43 44
        cost = paddle.layer.lambda_cost(
            input=output, score=label, NDCG_num=6, max_sort_size=-1)
W
wangmeng28 已提交
45
        return cost
C
caoying03 已提交
46
    else:
W
wangmeng28 已提交
47
        return output