lambda_rank.py 4.5 KB
Newer Older
D
dong zhihong 已提交
1 2 3 4 5
import os, sys
import gzip
import paddle.v2 as paddle
import numpy as np
import functools
D
dongzhihong 已提交
6
import argparse
D
dong zhihong 已提交
7 8


C
caoying03 已提交
9
def lambda_rank(input_dim):
D
dzhwinter 已提交
10
    """
C
caoying03 已提交
11
    lambda_rank is a Listwise rank model, the input data and label must be sequences.
D
dzhwinter 已提交
12 13 14 15
    https://papers.nips.cc/paper/2971-learning-to-rank-with-nonsmooth-cost-functions.pdf
    parameters :
      input_dim, one document's dense feature vector dimension

C
caoying03 已提交
16 17
    format of the dense_vector_sequence:
    [[f, ...], [f, ...], ...], f is a float or an int number
D
dzhwinter 已提交
18
    """
19 20 21 22 23 24 25 26
    label = paddle.layer.data("label",
                              paddle.data_type.dense_vector_sequence(1))
    data = paddle.layer.data("data",
                             paddle.data_type.dense_vector_sequence(input_dim))

    # hidden layer
    hd1 = paddle.layer.fc(
        input=data,
D
dzhwinter 已提交
27 28 29 30 31 32
        size=128,
        act=paddle.activation.Tanh(),
        param_attr=paddle.attr.Param(initial_std=0.01))

    hd2 = paddle.layer.fc(
        input=hd1,
33 34 35 36
        size=10,
        act=paddle.activation.Tanh(),
        param_attr=paddle.attr.Param(initial_std=0.01))
    output = paddle.layer.fc(
D
dzhwinter 已提交
37
        input=hd2,
38 39 40
        size=1,
        act=paddle.activation.Linear(),
        param_attr=paddle.attr.Param(initial_std=0.01))
D
dzhwinter 已提交
41 42 43 44

    # evaluator
    evaluator = paddle.evaluator.auc(input=output, label=label)
    # cost layer
45 46 47 48
    cost = paddle.layer.lambda_cost(
        input=output, score=label, NDCG_num=6, max_sort_size=-1)
    return cost, output

D
dong zhihong 已提交
49

C
caoying03 已提交
50
def train_lambda_rank(num_passes):
51 52 53 54 55 56 57
    # listwise input sequence
    fill_default_train = functools.partial(
        paddle.dataset.mq2007.train, format="listwise")
    fill_default_test = functools.partial(
        paddle.dataset.mq2007.test, format="listwise")
    train_reader = paddle.batch(
        paddle.reader.shuffle(fill_default_train, buf_size=100), batch_size=32)
D
dong zhihong 已提交
58
    test_reader = paddle.batch(fill_default_test, batch_size=32)
59

D
dzhwinter 已提交
60
    # mq2007 input_dim = 46, dense format
61
    input_dim = 46
C
caoying03 已提交
62
    cost, output = lambda_rank(input_dim)
63 64 65 66 67 68
    parameters = paddle.parameters.create(cost)

    trainer = paddle.trainer.SGD(
        cost=cost,
        parameters=parameters,
        update_equation=paddle.optimizer.Adam(learning_rate=1e-4))
D
dong zhihong 已提交
69

70 71 72 73 74 75 76 77
    #  Define end batch and end pass event handler
    def event_handler(event):
        if isinstance(event, paddle.event.EndIteration):
            print "Pass %d Batch %d Cost %.9f" % (event.pass_id, event.batch_id,
                                                  event.cost)
        if isinstance(event, paddle.event.EndPass):
            result = trainer.test(reader=test_reader, feeding=feeding)
            print "\nTest with Pass %d, %s" % (event.pass_id, result.metrics)
C
caoying03 已提交
78
            with gzip.open("lambda_rank_params_%d.tar.gz" % (event.pass_id),
79 80 81 82 83 84 85 86 87
                           "w") as f:
                parameters.to_tar(f)

    feeding = {"label": 0, "data": 1}
    trainer.train(
        reader=train_reader,
        event_handler=event_handler,
        feeding=feeding,
        num_passes=num_passes)
D
dong zhihong 已提交
88 89


C
caoying03 已提交
90
def lambda_rank_infer(pass_id):
91
    """
C
caoying03 已提交
92
  lambda_rank model inference interface
93 94 95 96 97
  parameters:
    pass_id : inference model in pass_id
  """
    print "Begin to Infer..."
    input_dim = 46
C
caoying03 已提交
98
    output = lambda_rank(input_dim)
99
    parameters = paddle.parameters.Parameters.from_tar(
C
caoying03 已提交
100
        gzip.open("lambda_rank_params_%d.tar.gz" % (pass_id - 1)))
101 102 103

    infer_query_id = None
    infer_data = []
D
dzhwinter 已提交
104
    infer_data_num = 1
105 106 107 108 109 110
    fill_default_test = functools.partial(
        paddle.dataset.mq2007.test, format="listwise")
    for label, querylist in fill_default_test():
        infer_data.append(querylist)
        if len(infer_data) == infer_data_num:
            break
D
dzhwinter 已提交
111 112 113

    # predict score of infer_data document. Re-sort the document base on predict score
    # in descending order. then we build the ranking documents
114 115 116
    predicitons = paddle.infer(
        output_layer=output, parameters=parameters, input=infer_data)
    for i, score in enumerate(predicitons):
D
dzhwinter 已提交
117
        print i, score
118

D
dong zhihong 已提交
119 120

if __name__ == '__main__':
D
dongzhihong 已提交
121 122 123 124 125 126 127
    parser = argparse.ArgumentParser(description='LambdaRank demo')
    parser.add_argument("--run_type", type=str, help="run type is train|infer")
    parser.add_argument(
        "--num_passes",
        type=int,
        help="num of passes in train| infer pass number of model")
    args = parser.parse_args()
C
caoying03 已提交
128
    paddle.init(use_gpu=False, trainer_count=1)
D
dongzhihong 已提交
129 130 131
    if args.run_type == "train":
        train_lambda_rank(args.num_passes)
    elif args.run_type == "infer":
D
dongzhihong 已提交
132
        lambda_rank_infer(pass_id=args.num_passes - 1)