Esempio n. 1
0
  def test_approx_ndcg_loss(self):
    with tf.Graph().as_default():
      scores = [[1.4, -2.8, -0.4], [0., 1.8, 10.2], [1., 1.2, -3.2]]
      # ranks= [[1,    3,    2],   [3,  2,   1],    [2,  1,    3]]
      labels = [[0., 2., 1.], [1., 0., 3.], [0., 0., 0.]]
      weights = [[2.], [1.], [1.]]
      example_weights = [[1., 2., 3.], [4., 5., 6.], [7., 8., 9.]]
      norm_wts = [sum([wt * l for wt, l in zip(wts, lbls)]) / sum(lbls)
                  if sum(lbls) else 0
                  for wts, lbls in zip(example_weights, labels)]

      with self.cached_session():
        self.assertAlmostEqual(
            ranking_losses._approx_ndcg_loss(labels, scores).eval(),
            -((1 / (3 / ln(2) + 1 / ln(3))) * (3 / ln(4) + 1 / ln(3)) +
              (1 / (7 / ln(2) + 1 / ln(3))) * (7 / ln(2) + 1 / ln(4))),
            places=5)
        self.assertAlmostEqual(
            ranking_losses._approx_ndcg_loss(labels, scores, weights).eval(),
            -(2 * (1 / (3 / ln(2) + 1 / ln(3))) * (3 / ln(4) + 1 / ln(3)) + 1 *
              (1 / (7 / ln(2) + 1 / ln(3))) * (7 / ln(2) + 1 / ln(4))),
            places=5)
        self.assertAlmostEqual(
            ranking_losses._approx_ndcg_loss(labels, scores,
                                             example_weights).eval(),
            -(norm_wts[0] * (1 / (3 / ln(2) + 1 / ln(3))) *
              (3 / ln(4) + 1 / ln(3)) +
              norm_wts[1] * (1 / (7 / ln(2) + 1 / ln(3))) *
              (7 / ln(2) + 1 / ln(4))),
            places=5)
Esempio n. 2
0
  def test_approx_ndcg_loss(self):
    scores = [[1.4, -2.8, -0.4], [0., 1.8, 10.2], [1., 1.2, -3.2]]
    labels = [[0., 2., 1.], [1., 0., 3.], [0., 0., 0.]]
    weights = [[2.], [1.], [1.]]

    with self.cached_session():
      self.assertAlmostEqual(
          ranking_losses._approx_ndcg_loss(labels, scores).eval(),
          -((1 / (3 / ln(2) + 1 / ln(3))) * (3 / ln(4) + 1 / ln(3)) +
            (1 / (7 / ln(2) + 1 / ln(3))) * (7 / ln(2) + 1 / ln(4))),
          places=5)
      self.assertAlmostEqual(
          ranking_losses._approx_ndcg_loss(labels, scores, weights).eval(),
          -(2 * (1 / (3 / ln(2) + 1 / ln(3))) * (3 / ln(4) + 1 / ln(3)) + 1 *
            (1 / (7 / ln(2) + 1 / ln(3))) * (7 / ln(2) + 1 / ln(4))),
          places=5)