Пример #1
0
  def test_make_loss_metric_fn(self):
    scores = [[1., 3., 2.], [1., 2., 3.]]
    labels = [[0., 0., 1.], [0., 0., 2.]]
    weights = [[2.], [1.]]
    weights_feature_name = 'weights'
    features = {
        weights_feature_name: weights,
    }
    m = ranking_losses.make_loss_metric_fn(
        ranking_losses.RankingLossKey.SOFTMAX_LOSS)
    m_w = ranking_losses.make_loss_metric_fn(
        ranking_losses.RankingLossKey.SOFTMAX_LOSS,
        weights_feature_name=weights_feature_name)
    self._check_metrics([
        (m(labels, scores,
           features), -(math.log(_softmax(scores[0])[2]) +
                        math.log(_softmax(scores[1])[2]) * 2.) / 3.),
        (m_w(labels, scores,
             features), -(math.log(_softmax(scores[0])[2]) * 2. +
                          math.log(_softmax(scores[1])[2]) * 2. * 1.) / 4.),
    ])

    # Value of loss metric is the same as loss with MEAN reduction.
    with self.cached_session():
      loss_fn_mean = ranking_losses.make_loss_fn(
          ranking_losses.RankingLossKey.SOFTMAX_LOSS,
          reduction=tf.compat.v1.losses.Reduction.MEAN)
      loss_mean = loss_fn_mean(labels, scores, features).eval()
    self._check_metrics([
        (m(labels, scores, features), loss_mean),
    ])
Пример #2
0
def _get_loss_metric_pair(key, weight=None):
    """Helper function to construct metric name and function for a loss."""
    name = "".join([
        "metric/",
        "weighted_" if weight else "",
        key,
    ])
    return name, losses.make_loss_metric_fn(key, weights_feature_name=weight)