Пример #1
0
  def test_ndcg(self):
    with tf.Graph().as_default():
      labels = [[1., 4., 1., 0.], [4., 2., 0., 3.], [0., 0., 0., 0.]]
      ranks = [[1, 2, 3, 4], [1, 3, 4, 2], [1, 2, 3, 4]]
      perm_mat = [[[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]],
                  [[1, 0, 0, 0], [0, 0, 0, 1], [0, 1, 0, 0], [0, 0, 1, 0]],
                  [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]]
      ndcg_ = [[0.679685], [0.95176], [0.]]
      ndcg_rank = [[0.679685], [1.], [0.]]
      ndcg_perm = [[0.679685], [1.], [0.]]

      with tf.compat.v1.Session() as sess:
        ndcg = sess.run(utils.ndcg(labels))
        self.assertAllClose(ndcg, ndcg_)
        ndcg = sess.run(utils.ndcg(labels, ranks))
        self.assertAllClose(ndcg, ndcg_rank)
        ndcg = sess.run(utils.ndcg(labels, perm_mat=perm_mat))
        self.assertAllClose(ndcg, ndcg_perm)
Пример #2
0
    def compute_unreduced_loss(self, labels, logits):
        """See `_RankingLoss`."""
        alpha = self._params.get('alpha', 10.0)
        is_valid = utils.is_label_valid(labels)
        labels = tf.compat.v1.where(is_valid, labels, tf.zeros_like(labels))
        logits = tf.compat.v1.where(
            is_valid, logits, -1e3 * tf.ones_like(logits) +
            tf.reduce_min(input_tensor=logits, axis=-1, keepdims=True))

        label_sum = tf.reduce_sum(input_tensor=labels, axis=1, keepdims=True)
        nonzero_mask = tf.greater(tf.reshape(label_sum, [-1]), 0.0)
        labels = tf.compat.v1.where(nonzero_mask, labels,
                                    _EPSILON * tf.ones_like(labels))
        ranks = utils.approx_ranks(logits, alpha=alpha)

        return -utils.ndcg(labels, ranks), tf.reshape(
            tf.cast(nonzero_mask, dtype=tf.float32), [-1, 1])