code: def test_make_loss_fn_with_a_string_of_keys_and_weights(self):
      scores = [[0.2, 0.5, 0.3], [0.2, 0.3, 0.5]]
      labels = [[0.0, 0.0, 1.0], [0.0, 0.0, 1.0]]
      weights = [[2.0], [1.0]]
      weights_1d = [2.0, 1.0]
      weights_3d = [[[2.0], [1.0], [0.0]], [[0.0], [1.0], [2.0]]]
      weights_feature_name = "weights"
      weights_1d_feature_name = "weights_1d"
      weights_3d_feature_name = "weights_3d"
      features = {
          weights_feature_name: weights,
          weights_1d_feature_name: weights_1d,
          weights_3d_feature_name: weights_3d,
      }
      pairwise_hinge_loss = ranking_losses._pairwise_hinge_loss(
          labels, scores
      ).numpy()
      pairwise_hinge_loss_weighted = (
          ranking_losses._pairwise_hinge_loss(
              labels, scores, weights=weights
          ).numpy()
      )
      pairwise_hinge_loss_itemwise_weighted = (
          ranking_losses._pairwise_hinge_loss(
              labels, scores, weights=tf.squeeze(weights_3d)
          ).numpy()
      )
      mean_squared_loss = ranking_losses._mean_squared_loss(
          labels, scores
      ).numpy()
      mean_squared_loss_weighted = ranking_losses._mean_squared_loss(
          labels, scores, weights=weights
      ).numpy()
      mean_squared_loss_itemwise_weighted = (
          ranking_losses._mean_squared_loss(
              labels, scores, weights=tf.squeeze(weights_3d)
          ).numpy()
      )
  
      loss_keys = "pairwise_hinge_loss:1.0,mean_squared_loss:1.0"
      loss_fn_simple = ranking_losses.make_loss_fn(loss_keys)
      self.assertAlmostEqual(
          loss_fn_simple(labels, scores, features).numpy(),
          pairwise_hinge_loss + mean_squared_loss,
          places=5,
      )
  
      # With 2-d list-wise weighted examples.
      loss_fn_weighted_example = ranking_losses.make_loss_fn(
          loss_keys, weights_feature_name=weights_feature_name
      )
      self.assertAlmostEqual(
          loss_fn_weighted_example(labels, scores, features).numpy(),
          pairwise_hinge_loss_weighted + mean_squared_loss_weighted,
          places=5,
      )
  
      # With 1-d list-wise weighted examples.
      loss_fn_weighted_example = ranking_losses.make_loss_fn(
          loss_keys, weights_feature_name=weights_1d_feature_name
      )
      self.assertAlmostEqual(
          loss_fn_weighted_example(labels, scores, features).numpy(),
          pairwise_hinge_loss_weighted + mean_squared_loss_weighted,
          places=5,
      )
  
      # With 3-d item-wise weighted examples.
      loss_fn_weighted_example = ranking_losses.make_loss_fn(
          loss_keys, weights_feature_name=weights_3d_feature_name
      )
      self.assertAlmostEqual(
          loss_fn_weighted_example(labels, scores, features).numpy(),
          pairwise_hinge_loss_itemwise_weighted
          + mean_squared_loss_itemwise_weighted,
          places=5,
      )
  
      # Test loss reduction method.
      # Two reduction methods should return different loss values.
      loss_fn_1 = ranking_losses.make_loss_fn(
          loss_keys, reduction=tf.compat.v1.losses.Reduction.SUM
      )
      loss_fn_2 = ranking_losses.make_loss_fn(
          loss_keys, reduction=tf.compat.v1.losses.Reduction.MEAN
      )
      self.assertNotAlmostEqual(
          loss_fn_1(labels, scores, features).numpy(),
          loss_fn_2(labels, scores, features).numpy(),
      )
  
      # With both weighted loss and weighted examples.
      loss_keys_with_weights = (
          "pairwise_hinge_loss:3.0,mean_squared_loss:2.0"
      )
      weighted_loss_fn_weighted_example = ranking_losses.make_loss_fn(
          loss_keys_with_weights,
          weights_feature_name=weights_feature_name,
      )
      self.assertAlmostEqual(
          weighted_loss_fn_weighted_example(
              labels, scores, features
          ).numpy(),
          pairwise_hinge_loss_weighted * 3.0
          + mean_squared_loss_weighted * 2.0,
          places=5,
      )
  
      with self.assertRaisesRegex(
          ValueError,
          r"`loss_weights` has to be None when weights are encoded in `loss_keys`",
      ):
          ranking_losses.make_loss_fn(loss_keys, [2.0])
  
imports: import math
  import tensorflow
  from tensorflow_ranking.python import losses
  from tensorflow_ranking.python import losses_impl
label_desc: Use image node_id 21 for calling the LossesTest obj's underlying member method code with example usage: obj.test_make_loss_fn_with_a_string_of_keys_and_weights() without return types