Exemplo n.º 1
0
    def __init__(self,
                 name=None,
                 topn=None,
                 dtype=None,
                 ragged=False,
                 **kwargs):
        """Constructor.

    Args:
      name: A string used as the name for this metric.
      topn: A cutoff for how many examples to consider for this metric.
      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(PrecisionIAMetric, self).__init__(name=name,
                                                dtype=dtype,
                                                ragged=ragged,
                                                **kwargs)
        self._topn = topn
        self._metric = metrics_impl.PrecisionIAMetric(name=name,
                                                      topn=topn,
                                                      ragged=ragged)
Exemplo n.º 2
0
 def __init__(self,
              topn=None,
              dtype=None,
              name="precision_ia_metric",
              **kwargs):
   super(PrecisionIAMetric, self).__init__(name=name, dtype=dtype, **kwargs)
   self._topn = topn
   self._metric = metrics_impl.PrecisionIAMetric(name=name, topn=topn)
Exemplo n.º 3
0
 def __init__(self,
              name=None,
              topn=None,
              dtype=None,
              ragged=False,
              **kwargs):
     super(PrecisionIAMetric, self).__init__(name=name,
                                             dtype=dtype,
                                             **kwargs)
     self._topn = topn
     self._metric = metrics_impl.PrecisionIAMetric(name=name,
                                                   topn=topn,
                                                   ragged=ragged)
Exemplo n.º 4
0
def precision_ia(labels, predictions, weights=None, topn=None, name=None):
    """Computes Intent-Aware Precision as weighted average of relevant examples.

  Args:
    labels: A `Tensor` with shape [batch_size, list_size, subtopic_size]. A
      nonzero value means that the example covers the corresponding subtopic.
    predictions: A `Tensor` with shape [batch_size, list_size]. Each value is
      the ranking score of the corresponding example.
    weights: A `Tensor` of the same shape of predictions or [batch_size, 1]. The
      former case is per-example and the latter case is per-list.
    topn: A cutoff for how many examples to consider for this metric.
    name: A string used as the name for this metric.

  Returns:
    A metric for the weighted precision of the batch.
  """
    metric = metrics_impl.PrecisionIAMetric(name, topn)
    with tf.compat.v1.name_scope(metric.name, 'precision_ia',
                                 (labels, predictions, weights)):
        precision_at_k, per_list_weights = metric.compute(
            labels, predictions, weights)
    return tf.compat.v1.metrics.mean(precision_at_k, per_list_weights)