code: def test_make_alpha_discounted_cumulative_gain_fn(self):
      with tf.Graph().as_default():
          scores = [[1.0, 3.0, 2.0], [1.0, 2.0, 3.0]]
          # Note that scores are ranked in descending order.
          # ranks = [[3, 1, 2], [3, 2, 1]]
          labels = [
              [[0.0, 0.0], [0.0, 1.0], [0.0, 1.0]],
              [[0.0, 0.0], [1.0, 0.0], [1.0, 1.0]],
          ]
          # cum_labels = [[[0., 2.], [0., 0.], [0., 1.]],
          #               [[2., 1.], [1., 1.], [0., 0.]]]
          weights = [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]
          weights_3d = [[[1.0], [2.0], [3.0]], [[4.0], [5.0], [6.0]]]
          list_weights = [1.0, 0.0]
          list_weights_2d = [[1.0], [0.0]]
          weights_feature_name = "weights"
          weights_invalid_feature_name = "weights_invalid"
          weights_3d_feature_name = "weights_3d"
          list_weights_name = "list_weights"
          list_weights_2d_name = "list_weights_2d"
          features = {
              weights_feature_name: [weights[0]],
              weights_invalid_feature_name: weights[0],
              weights_3d_feature_name: [weights_3d[0]],
              list_weights_name: list_weights,
              list_weights_2d_name: list_weights_2d,
          }
          m = metrics_lib.make_ranking_metric_fn(
              metrics_lib.RankingMetricKey.ALPHA_DCG
          )
  
          expected_alphadcg = (
              _alpha_dcg([0.0, 1.0], [0.0, 0.0], 1)
              + _alpha_dcg([0.0, 1.0], [0.0, 1.0], 2)
              + _alpha_dcg([0.0, 0.0], [0.0, 2.0], 3)
          )
          self._check_metrics(
              [
                  (
                      m([labels[0]], [scores[0]], features),
                      expected_alphadcg,
                  ),
              ]
          )
          expected_alphadcg_1 = (
              _alpha_dcg([0.0, 1.0], [0.0, 0.0], 1)
              + _alpha_dcg([0.0, 1.0], [0.0, 1.0], 2)
              + _alpha_dcg([0.0, 0.0], [0.0, 2.0], 3)
          )
          expected_alphadcg_2 = (
              _alpha_dcg([1.0, 1.0], [0.0, 0.0], 1)
              + _alpha_dcg([1.0, 0.0], [1.0, 1.0], 2)
              + _alpha_dcg([0.0, 0.0], [2.0, 1.0], 3)
          )
          expected_alphadcg = (
              expected_alphadcg_1 + expected_alphadcg_2
          ) / 2.0
          self._check_metrics(
              [
                  (m(labels, scores, features), expected_alphadcg),
              ]
          )
  
          # With item-wise weights.
          m_top = metrics_lib.make_ranking_metric_fn(
              metrics_lib.RankingMetricKey.ALPHA_DCG,
              weights_feature_name=weights_feature_name,
              topn=1,
          )
          m_weight = metrics_lib.make_ranking_metric_fn(
              metrics_lib.RankingMetricKey.ALPHA_DCG,
              weights_feature_name=weights_feature_name,
          )
          m_weights_3d = metrics_lib.make_ranking_metric_fn(
              metrics_lib.RankingMetricKey.ALPHA_DCG,
              weights_feature_name=weights_3d_feature_name,
          )
          self._check_metrics(
              [
                  (
                      m_top([labels[0]], [scores[0]], features),
                      _alpha_dcg([0.0, 1.0], [0.0, 0.0], 1, 2.0) / 2.5,
                  ),
                  (
                      m_weight([labels[0]], [scores[0]], features),
                      (
                          _alpha_dcg([0.0, 1.0], [0.0, 0.0], 1, 2.0)
                          + _alpha_dcg([0.0, 1.0], [0.0, 1.0], 2, 3.0)
                          + _alpha_dcg([0.0, 0.0], [0.0, 2.0], 3, 1.0)
                      )
                      / 2.5,
                  ),
                  (
                      m_weights_3d([labels[0]], [scores[0]], features),
                      (
                          _alpha_dcg([0.0, 1.0], [0.0, 0.0], 1, 2.0)
                          + _alpha_dcg([0.0, 1.0], [0.0, 1.0], 2, 3.0)
                          + _alpha_dcg([0.0, 0.0], [0.0, 2.0], 3, 1.0)
                      )
                      / 2.5,
                  ),
              ]
          )
          with self.assertRaises(ValueError):
              m_weight_invalid = metrics_lib.make_ranking_metric_fn(
                  metrics_lib.RankingMetricKey.ALPHA_DCG,
                  weights_feature_name=weights_invalid_feature_name,
              )
              m_weight_invalid([labels[0]], [scores[0]], features)
  
          # With list-wise weights.
          m_list_weight = metrics_lib.make_ranking_metric_fn(
              metrics_lib.RankingMetricKey.ALPHA_DCG,
              weights_feature_name=list_weights_name,
          )
          m_list_weight_2d = metrics_lib.make_ranking_metric_fn(
              metrics_lib.RankingMetricKey.ALPHA_DCG,
              weights_feature_name=list_weights_2d_name,
          )
          self._check_metrics(
              [
                  (
                      m_list_weight(labels, scores, features),
                      (
                          _alpha_dcg([0.0, 1.0], [0.0, 0.0], 1, 1.0)
                          + _alpha_dcg([0.0, 1.0], [0.0, 1.0], 2, 1.0)
                          + _alpha_dcg([0.0, 0.0], [0.0, 2.0], 3, 1.0)
                      ),
                  ),
                  (
                      m_list_weight_2d(labels, scores, features),
                      (
                          _alpha_dcg([0.0, 1.0], [0.0, 0.0], 1, 1.0)
                          + _alpha_dcg([0.0, 1.0], [0.0, 1.0], 2, 1.0)
                          + _alpha_dcg([0.0, 0.0], [0.0, 2.0], 3, 1.0)
                      ),
                  ),
              ]
          )
  
          # Test different gain and discount functions.
          alpha = 0.2
          rank_discount_fn = lambda rank: 1.0 / rank
  
          mod_alpha_dcg_fn = functools.partial(
              _alpha_dcg, alpha=alpha, rank_discount_fn=rank_discount_fn
          )
  
          m_mod = metrics_lib.make_ranking_metric_fn(
              metrics_lib.RankingMetricKey.ALPHA_DCG,
              rank_discount_fn=rank_discount_fn,
              alpha=alpha,
          )
  
          expected_modified_alphadcg_1 = (
              mod_alpha_dcg_fn([0.0, 1.0], [0.0, 0.0], 1)
              + mod_alpha_dcg_fn([0.0, 1.0], [0.0, 1.0], 2)
              + mod_alpha_dcg_fn([0.0, 0.0], [0.0, 2.0], 3)
          )
          self._check_metrics(
              [
                  (
                      m_mod([labels[0]], [scores[0]], features),
                      expected_modified_alphadcg_1,
                  ),
              ]
          )
  
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  import functools
  import math
  import tensorflow
  from tensorflow_ranking.python import metrics
label_desc: Use image node_id 14 for calling the MetricsTest obj's underlying member method code with example usage: obj.test_make_alpha_discounted_cumulative_gain_fn() without return types