code: def testSparseMetric(self):
      computation = tf_metric_wrapper.tf_metric_computations(
          [
              tf.keras.metrics.SparseCategoricalCrossentropy(
                  name="sparse_categorical_crossentropy"
              )
          ]
      )[0]
  
      # Simulate a multi-class problem with 3 labels.
      example = {
          "labels": [1],
          "predictions": [0.3, 0.6, 0.1],
          "example_weights": [1.0],
      }
  
      with beam.Pipeline() as pipeline:
          # pylint: disable=no-value-for-parameter
          result = (
              pipeline
              | "Create" >> beam.Create([example])
              | "Process"
              >> beam.Map(metric_util.to_standard_metric_inputs)
              | "AddSlice" >> beam.Map(lambda x: ((), x))
              | "Combine" >> beam.CombinePerKey(computation.combiner)
          )
  
          # pylint: enable=no-value-for-parameter
  
          def check_result(got):
              try:
                  self.assertLen(got, 1)
                  got_slice_key, got_metrics = got[0]
                  self.assertEqual(got_slice_key, ())
                  key = metric_types.MetricKey(
                      name="sparse_categorical_crossentropy"
                  )
                  # 0*log(.3) -1*log(0.6)-0*log(.1) = 0.51
                  self.assertDictElementsAlmostEqual(
                      got_metrics, {key: 0.51083}
                  )
  
              except AssertionError as err:
                  raise util.BeamAssertException(err)
  
          util.assert_that(result, check_result, label="result")
  
imports: from absl.testing import parameterized
  import apache_beam
  from apache_beam.testing import util
  import numpy
  import tensorflow
  from tensorflow_model_analysis.eval_saved_model import testutil
  from tensorflow_model_analysis.metrics import metric_types
  from tensorflow_model_analysis.metrics import metric_util
  from tensorflow_model_analysis.metrics import tf_metric_wrapper
  from tensorflow_model_analysis.proto import config_pb2
label_desc: Use image node_id 2 for calling the NonConfusionMatrixMetricsTest obj's underlying member method code with example usage: obj.testSparseMetric() without return types