示例#1
0
    def __init__(self,
                 reduction=tf.losses.Reduction.AUTO,
                 name=None,
                 lambda_weight=None,
                 temperature=1.0,
                 ragged=False):
        """ListMLE loss.

    Args:
      reduction: (Optional) The `tf.keras.losses.Reduction` to use (see
        `tf.keras.losses.Loss`).
      name: (Optional) The name for the op.
      lambda_weight: (Optional) A lambdaweight to apply to the loss. Can be one
        of `tfr.keras.losses.DCGLambdaWeight`,
        `tfr.keras.losses.NDCGLambdaWeight`,
        `tfr.keras.losses.PrecisionLambdaWeight`, or,
        `tfr.keras.losses.ListMLELambdaWeight`.
      temperature: (Optional) The temperature to use for scaling the logits.
      ragged: (Optional) If True, this loss will accept ragged tensors. If
        False, this loss will accept dense tensors.
    """
        super().__init__(reduction, name, lambda_weight, temperature, ragged)
        self._loss = losses_impl.ListMLELoss(
            name='{}_impl'.format(name) if name else None,
            lambda_weight=lambda_weight,
            temperature=temperature,
            ragged=ragged)
示例#2
0
文件: losses.py 项目: chiqunz/ranking
 def __init__(self,
              reduction=tf.losses.Reduction.AUTO,
              name=None,
              lambda_weight=None):
     super(ListMLELoss, self).__init__(reduction, name)
     self._loss = losses_impl.ListMLELoss(name='{}_impl'.format(name),
                                          lambda_weight=lambda_weight)
示例#3
0
 def test_list_mle_loss_tie(self):
     with tf.Graph().as_default():
         tf.compat.v1.set_random_seed(1)
         scores = [[0., ln(2), ln(3)]]
         labels = [[0., 0., 1.]]
         reduction = tf.compat.v1.losses.Reduction.SUM_BY_NONZERO_WEIGHTS
         with self.cached_session():
             loss_fn = losses_impl.ListMLELoss(name=None)
             self.assertAlmostEqual(
                 loss_fn.compute(labels, scores, None, reduction).eval(),
                 -((ln(3. / (3 + 2 + 1)) + ln(2. / (2 + 1)) + ln(1. / 1))),
                 places=5)
示例#4
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)
示例#5
0
def _list_mle_loss(
    labels,
    logits,
    weights=None,
    lambda_weight=None,
    reduction=tf.compat.v1.losses.Reduction.SUM_BY_NONZERO_WEIGHTS,
    name=None):
  """Computes the ListMLE loss [Xia et al.

  2008] for a list.

  Given the labels of graded relevance l_i and the logits s_i, we calculate
  the ListMLE loss for the given list.

  The `lambda_weight` re-weights examples based on l_i and r_i.
  The recommended weighting scheme is 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:
    labels: A `Tensor` of the same shape as `logits` representing graded
      relevance.
    logits: A `Tensor` with shape [batch_size, list_size]. Each value is the
      ranking score of the corresponding item.
    weights: A scalar, a `Tensor` with shape [batch_size, 1] for list-wise
      weights, or a `Tensor` with shape [batch_size, list_size] for item-wise
      weights.
    lambda_weight: A `DCGLambdaWeight` instance.
    reduction: One of `tf.losses.Reduction` except `NONE`. Describes how to
      reduce training loss over batch.
    name: A string used as the name for this loss.

  Returns:
    An op for the ListMLE loss.
  """
  loss = losses_impl.ListMLELoss(name, lambda_weight)
  with tf.compat.v1.name_scope(loss.name, 'list_mle_loss',
                               (labels, logits, weights)):
    return loss.compute(labels, logits, weights, reduction)
示例#6
0
 def test_list_mle_loss(self):
     with tf.Graph().as_default():
         scores = [[0., ln(3), ln(2)], [0., ln(2), ln(3)]]
         labels = [[0., 2., 1.], [1., 0., 2.]]
         weights = [[2.], [1.]]
         reduction = tf.compat.v1.losses.Reduction.SUM_BY_NONZERO_WEIGHTS
         with self.cached_session():
             loss_fn = losses_impl.ListMLELoss(name=None)
             self.assertAlmostEqual(
                 loss_fn.compute(labels, scores, None, reduction).eval(),
                 -((ln(3. / (3 + 2 + 1)) + ln(2. / (2 + 1)) + ln(1. / 1)) +
                   (ln(3. / (3 + 2 + 1)) + ln(1. /
                                              (1 + 2)) + ln(2. / 2))) / 2,
                 places=5)
             self.assertAlmostEqual(
                 loss_fn.compute(labels, scores, weights, reduction).eval(),
                 -(2 * (ln(3. /
                           (3 + 2 + 1)) + ln(2. /
                                             (2 + 1)) + ln(1. / 1)) + 1 *
                   (ln(3. / (3 + 2 + 1)) + ln(1. /
                                              (1 + 2)) + ln(2. / 2))) / 2,
                 places=5)
示例#7
0
def make_loss_metric_fn(loss_key,
                        weights_feature_name=None,
                        lambda_weight=None,
                        name=None):
  """Factory method to create a metric based on a loss.

  Args:
    loss_key: A key in `RankingLossKey`.
    weights_feature_name: A `string` specifying the name of the weights feature
      in `features` dict.
    lambda_weight: A `_LambdaWeight` object.
    name: A `string` used as the name for this metric.

  Returns:
    A metric fn with the following Args:
    * `labels`: A `Tensor` of the same shape as `predictions` representing
    graded relevance.
    * `predictions`: A `Tensor` with shape [batch_size, list_size]. Each value
    is the ranking score of the corresponding example.
    * `features`: A dict of `Tensor`s that contains all features.
  """

  metric_dict = {
      RankingLossKey.PAIRWISE_HINGE_LOSS:
          losses_impl.PairwiseHingeLoss(name, lambda_weight=lambda_weight),
      RankingLossKey.PAIRWISE_LOGISTIC_LOSS:
          losses_impl.PairwiseLogisticLoss(name, lambda_weight=lambda_weight),
      RankingLossKey.PAIRWISE_SOFT_ZERO_ONE_LOSS:
          losses_impl.PairwiseSoftZeroOneLoss(
              name, lambda_weight=lambda_weight),
      RankingLossKey.SOFTMAX_LOSS:
          losses_impl.SoftmaxLoss(name, lambda_weight=lambda_weight),
      RankingLossKey.SIGMOID_CROSS_ENTROPY_LOSS:
          losses_impl.SigmoidCrossEntropyLoss(name),
      RankingLossKey.MEAN_SQUARED_LOSS:
          losses_impl.MeanSquaredLoss(name),
      RankingLossKey.LIST_MLE_LOSS:
          losses_impl.ListMLELoss(name, lambda_weight=lambda_weight),
      RankingLossKey.APPROX_NDCG_LOSS:
          losses_impl.ApproxNDCGLoss(name),
      RankingLossKey.APPROX_MRR_LOSS:
          losses_impl.ApproxMRRLoss(name),
      RankingLossKey.GUMBEL_APPROX_NDCG_LOSS: losses_impl.ApproxNDCGLoss(name),
  }

  def _get_weights(features):
    """Get weights tensor from features and reshape it to 2-D if necessary."""
    weights = None
    if weights_feature_name:
      weights = tf.convert_to_tensor(value=features[weights_feature_name])
      # Convert weights to a 2-D Tensor.
      weights = utils.reshape_to_2d(weights)
    return weights

  def metric_fn(labels, predictions, features):
    """Defines the metric fn."""
    weights = _get_weights(features)
    loss = metric_dict.get(loss_key, None)
    if loss is None:
      raise ValueError('loss_key {} not supported.'.format(loss_key))
    return loss.eval_metric(labels, predictions, weights)

  return metric_fn