Example #1
0
def create_p_list_mle_lambda_weight(list_size):
  """Creates _LambdaWeight based on Position-Aware ListMLE paper.

  Produces a weight based on the formulation presented in the
  "Position-Aware ListMLE" paper (Lan et al.) and available using
  create_p_list_mle_lambda_weight() factory function above.

  Args:
    list_size: Size of the input list.

  Returns:
    A _LambdaWeight for Position-Aware ListMLE.
  """
  return losses_impl.ListMLELambdaWeight(
      rank_discount_fn=lambda rank: tf.pow(2., list_size - rank) - 1.)
Example #2
0
 def test_list_mle_loss_lambda_weight(self):
     with tf.Graph().as_default():
         scores = [[0., ln(3), ln(2)], [0., ln(2), ln(3)]]
         labels = [[0., 2., 1.], [1., 0., 2.]]
         lw = losses_impl.ListMLELambdaWeight(
             rank_discount_fn=lambda rank: tf.pow(2., 3 - rank) - 1.)
         reduction = tf.compat.v1.losses.Reduction.SUM_BY_NONZERO_WEIGHTS
         with self.cached_session():
             loss_fn = losses_impl.ListMLELoss(name=None, lambda_weight=lw)
             self.assertAlmostEqual(
                 loss_fn.compute(labels, scores, None, reduction).eval(),
                 -((3 * ln(3. / (3 + 2 + 1)) + 1 * ln(2. / (2 + 1)) +
                    0 * ln(1. / 1)) +
                   (3 * ln(3. /
                           (3 + 2 + 1)) + 1 * ln(1. /
                                                 (1 + 2)) + 0 * ln(2. / 2)))
                 / 2,
                 places=5)