class_docstr: Tests the metric computation related utilities.
class_docstr_tok: ["Tests","the","metric","computation","related","utilities","."]
docstr: Tests the topk accuracy computation.
docstr_tok: ["Tests","the","topk","accuracy","computation","."]
code: def test_weighted_topk_correctly_classified(self):
      """Tests the topk accuracy computation."""
      batch_size = 512
      num_of_classes = 100
      logits = jnp.array(
          np.random.normal(size=(batch_size, num_of_classes)),
          dtype=jnp.float32,
      )
      labels = jnp.array(
          np.random.randint(num_of_classes, size=(batch_size,))
      )
  
      one_hot_targets = common_utils.onehot(labels, logits.shape[-1])
      classification_accuracy = (
          model_utils.weighted_correctly_classified(
              logits, one_hot_targets
          )
      )
      top_one_accuracy = model_utils.weighted_topk_correctly_classified(
          logits, one_hot_targets, k=1
      )
      self.assertSequenceAlmostEqual(
          classification_accuracy.flatten(), top_one_accuracy.flatten()
      )
  
      top_n_accuracy = model_utils.weighted_topk_correctly_classified(
          logits, one_hot_targets, k=num_of_classes
      )
      self.assertEqual(jnp.mean(top_n_accuracy), 1)
  
      # computes using numpy
      top_5_accuracy = model_utils.weighted_topk_correctly_classified(
          logits, one_hot_targets, k=5
      )
      top5_pred = np.argsort(
          np.reshape(logits, [-1, num_of_classes]), axis=1
      )[:, -5:]
      y_true = np.array(labels)
      top5_pred = np.reshape(top5_pred, [-1, 5])
      y_true = np.reshape(y_true, [-1])
      np_top_accuracy = np.array(
          [y_true[i] in top5_pred[i, :] for i in range(len(y_true))]
      )
      self.assertSequenceAlmostEqual(
          top_5_accuracy.flatten(), np_top_accuracy.flatten()
      )
  
imports: import itertools
  from absl.testing import absltest
  from absl.testing import parameterized
  from flax.training import common_utils
  import jax
  import jax.numpy
  import numpy
  from scenic.model_lib.base_models import model_utils
label_desc: Use image node_id 2 for calling the MetricTest obj's underlying member method code with example usage: obj.test_weighted_topk_correctly_classified() without return types