Exemplo n.º 1
0
  def __init__(self,
               topn=None,
               alpha=0.5,
               rank_discount_fn=_DEFAULT_RANK_DISCOUNT_FN,
               seed=None,
               dtype=None,
               name="alpha_dcg_metric",
               **kwargs):
    """Construct the ranking metric class for alpha-DCG.

    Args:
      topn: A cutoff for how many examples to consider for this metric.
      alpha: A float between 0 and 1, parameter used in definition of alpha-DCG.
        Introduced as an assessor error in judging whether a document is
        covering a subtopic of the query.
      rank_discount_fn: A function of rank discounts. Default is set to
        discount = 1 / log2(rank+1).
      seed: The ops-level random seed used in shuffle ties in `sort_by_scores`.
      dtype: Data type of the metric output. See `tf.keras.metrics.Metric`.
      name: A string used as the name for this metric.
      **kwargs: Other keyward arguments used in `tf.keras.metrics.Metric`.
    """
    super(AlphaDCGMetric, self).__init__(name=name, dtype=dtype, **kwargs)
    self._topn = topn
    self._alpha = alpha
    self._rank_discount_fn = rank_discount_fn
    self._seed = seed
    self._metric = metrics_impl.AlphaDCGMetric(
        name=name,
        topn=topn,
        alpha=alpha,
        rank_discount_fn=rank_discount_fn,
        seed=seed)
Exemplo n.º 2
0
def alpha_discounted_cumulative_gain(
        labels,
        predictions,
        weights=None,
        topn=None,
        name=None,
        rank_discount_fn=_DEFAULT_RANK_DISCOUNT_FN,
        alpha=0.5,
        seed=None):
    """Computes alpha discounted cumulative gain (alpha-DCG).

  Args:
    labels: A `Tensor` with shape [batch_size, list_size, subtopic_size]. Each
      value represents graded relevance to a subtopic: 1 for relevent subtopic,
      0 for irrelevant, and -1 for paddings. When the actual subtopic number
      of a query is smaller than the `subtopic_size`, `labels` will be padded
      to `subtopic_size` with -1, similar to the paddings used for queries
      with doc number less then list_size.
    predictions: A `Tensor` with shape [batch_size, list_size]. Each value is
      the ranking score of the corresponding example.
    weights: A `Tensor` of shape [batch_size, list_size] or [batch_size, 1].
      They are per-example and per-list, respectively.
    topn: A cutoff for how many examples to consider for this metric.
    name: A string used as the name for this metric.
    rank_discount_fn: A function of rank discounts. Default is set to
      discount = 1 / log2(rank+1).
    alpha: A float between 0 and 1. Originally introduced as an assessor error
      in judging whether a document is covering a subtopic of the query. It
      can also be interpreted as the inverse number of documents covering the
      same subtopic reader needs to get and confirm the subtopic information
      of a query.
    seed: The ops-level random seed used in shuffle ties in `sort_by_scores`.

  Returns:
    A metric for the weighted alpha discounted cumulative gain of the batch.
  """
    metric = metrics_impl.AlphaDCGMetric(name,
                                         topn,
                                         alpha=alpha,
                                         rank_discount_fn=rank_discount_fn,
                                         seed=seed)
    with tf.compat.v1.name_scope(name, 'alpha_discounted_cumulative_gain',
                                 (labels, predictions, weights)):
        # TODO: Add mask argument for metric.compute() call
        alpha_dcg, per_list_weights = metric.compute(labels, predictions,
                                                     weights)
    return tf.compat.v1.metrics.mean(alpha_dcg, per_list_weights)
Exemplo n.º 3
0
    def __init__(self,
                 name="alpha_dcg_metric",
                 topn=None,
                 alpha=0.5,
                 rank_discount_fn=None,
                 seed=None,
                 dtype=None,
                 ragged=False,
                 **kwargs):
        """Construct the ranking metric class for alpha-DCG.

    Args:
      name: A string used as the name for this metric.
      topn: A cutoff for how many examples to consider for this metric.
      alpha: A float between 0 and 1, parameter used in definition of alpha-DCG.
        Introduced as an assessor error in judging whether a document is
        covering a subtopic of the query.
      rank_discount_fn: A function of rank discounts. Default is set to
        `1 / log2(rank+1)`. The `rank_discount_fn` should be keras serializable.
        Please see the `log2_inverse` above as an example when defining user
        customized functions.
      seed: The ops-level random seed used in shuffle ties in `sort_by_scores`.
      dtype: Data type of the metric output. See `tf.keras.metrics.Metric`.
      ragged: A bool indicating whether the supplied tensors are ragged. If
        True y_true, y_pred and sample_weight (if providing per-example weights)
        need to be ragged tensors with compatible shapes.
      **kwargs: Other keyward arguments used in `tf.keras.metrics.Metric`.
    """
        super(AlphaDCGMetric, self).__init__(name=name,
                                             dtype=dtype,
                                             ragged=ragged,
                                             **kwargs)
        self._topn = topn
        self._alpha = alpha
        self._rank_discount_fn = rank_discount_fn or utils.log2_inverse
        self._seed = seed
        self._metric = metrics_impl.AlphaDCGMetric(
            name=name,
            topn=topn,
            alpha=alpha,
            rank_discount_fn=self._rank_discount_fn,
            seed=seed,
            ragged=ragged)