code: def _pairwise_loss(
      labels, scores, weights, loss_form, rank_discount_form=None
  ):
      """Returns the pairwise loss given the loss form.
  
      Args:
        labels: A list of graded relevance.
        scores: A list of item ranking scores.
        weights: A list of item weights.
        loss_form: A string representing the form of loss.
        rank_discount_form: A string representing the form of rank discount.
  
      Returns:
        A tuple of (sum of loss, sum of weights, count of nonzero weights).
      """
      scores, labels, weights = zip(
          *sorted(zip(scores, labels, weights), reverse=True)
      )
  
      def _rank_discount(rank_discount_form, rank):
          discount = {
              "LINEAR": 1.0 / rank,
              "LOG": 1.0 / math.log(1.0 + rank),
          }
          return discount[rank_discount_form]
  
      def _lambda_weight(label_diff, i, j):
          delta = math.fabs(label_diff)
          if rank_discount_form is not None:
              delta *= math.fabs(
                  _rank_discount(rank_discount_form, i + 1)
                  - _rank_discount(rank_discount_form, j + 1)
              )
          else:
              delta = 1.0 if delta > 0 else 0
          return delta
  
      def _loss(score_diff, label_diff, delta):
          if label_diff <= 0:
              return 0.0
          loss_table = {
              ranking_losses.RankingLossKey.PAIRWISE_HINGE_LOSS: max(
                  0, 1 - score_diff
              )
              * delta,
              ranking_losses.RankingLossKey.PAIRWISE_LOGISTIC_LOSS: math.log(
                  1.0 + math.exp(-score_diff)
              )
              * delta,
              ranking_losses.RankingLossKey.PAIRWISE_SOFT_ZERO_ONE_LOSS: 1.0
              / (1.0 + math.exp(score_diff))
              * delta,
          }
          return loss_table[loss_form]
  
      loss = 0.0
      weight = 0.0
      count = 0.0
      for i in range(len(labels)):
          for j in range(len(labels)):
              if labels[i] > labels[j]:
                  delta = _lambda_weight(labels[i] - labels[j], i, j)
                  part_loss = _loss(
                      scores[i] - scores[j],
                      labels[i] - labels[j],
                      delta,
                  )
                  if weights[i] > 0:
                      loss += part_loss * weights[i]
                      weight += delta * weights[i]
                      if weight > 0:
                          count += 1.0
      return loss, weight, count
  
imports: import math
  import tensorflow
  from tensorflow_ranking.python import losses
  from tensorflow_ranking.python import losses_impl
label_desc: Use image node_id 2 for calling a global function with example usage: _pairwise_loss(labels, scores, weights, loss_form, rank_discount_form) and returns: loss,  weight,  count, discount, delta, loss_table, int