-
Notifications
You must be signed in to change notification settings - Fork 1
/
lambdarank.py
74 lines (61 loc) · 2.53 KB
/
lambdarank.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env python3
"""
Gradients for the LambdaRank implementation.
.. moduleauthor:: Nishant Sharma
"""
import os
import tensorflow as tf
from tensorflow.python.framework import ops
from tensorflow.python.ops import array_ops
from tensorflow.python.ops import sparse_ops
if os.name == "posix":
libLambdaRankPath = os.path.dirname(__file__) + "/liblambdarank.so"
elif os.name == "nt":
libLambdaRankPath = os.path.dirname(__file__) + "/liblambdarank.dll"
else:
libLambdaRankPath = None
if libLambdaRankPath and os.path.exists(libLambdaRankPath):
lambdarank_module = tf.load_op_library(libLambdaRankPath)
@ops.RegisterGradient("LambdaRank")
def LambdaRankGradient(op,
grad_ranknet_cost,
grad_lambdarank_cost,
grad_discrete_metric,
grad_lambdas):
"""
The gradient for `lambdarank` using the operation implemented in C++.
:param op: `lambdarank` `Operation` that we are differentiating, which we can use
to find the inputs and outputs of the original op.
:param grad: gradient with respect to the output of the `lambdarank` op.
:return: gradients with respect to the input of `lambdarank`.
PLEASE NOTE: Although, the OP has other outputs, the differentiation works only
w.r.t. lambdarank_score.
"""
lambdas = op.outputs[3]
grad_wrt_ypred = tf.multiply(grad_lambdarank_cost, lambdas)
return [None, None, grad_wrt_ypred]
def lambdarank_tf(qids, y, y_pred, max_k):
"""
Run Tensorflow version of LambdaRank on passed inputs.
"""
n_samples = len(y)
_qids = tf.placeholder(tf.int32, shape = (n_samples,))
_y = tf.placeholder(tf.int32, shape = (n_samples,))
_y_pred = tf.placeholder(tf.float64, shape = (n_samples,))
# Build feed_dict and feed it to TF session to obtain OP output.
feed_dict = {_qids: qids, _y:y, _y_pred:y_pred}
tf_value_nodes = lambdarank_module.lambda_rank(_qids, _y, _y_pred, max_k)
# Obtain TF OP output.
with tf.Session('') as sess:
tf_values = sess.run(tf_value_nodes, feed_dict=feed_dict)
return tf_values
class LambdaRankLoss(object):
def __init__(self, max_k=20):
self.max_k = max_k
def __call__(self, y, y_pred):
_qid = tf.to_int32(y[...,1])
_y = tf.to_int32(y[...,0])
_y_pred = y_pred[...,0]
T="float64" if _y_pred.dtype == tf.float64 else "float32"
lambdaRankNode = lambdarank_module.lambda_rank(_qid, _y, _y_pred, self.max_k, T)
return lambdaRankNode.lambdarank_cost