code: def test_builder_to_backend_stress(
      self, use_cpu_only, backend, n_sample, n_class
  ):
      output_name = "random_categorical"
      logits = np.random.rand(2, n_class)
      probs = [self.softmax(logits[0]), self.softmax(logits[1])]
  
      # Test logits input
      input_placeholders = {"x": mb.placeholder(shape=(2, n_class))}
      input_values = {"x": logits}
  
      def build(x):
          return [
              mb.random_categorical(
                  x=x, size=n_sample, mode="logits", name=output_name
              )
          ]
  
      if _IS_MACOS:
          prediction = get_core_ml_prediction(
              build, input_placeholders, input_values, backend=backend
          )
  
          ref0 = np.random.multinomial(n_sample, probs[0])
          ref1 = np.random.multinomial(n_sample, probs[1])
  
          pred0 = prediction[output_name].reshape(2, n_sample)[0]
          pred1 = prediction[output_name].reshape(2, n_sample)[1]
  
          # convert to bincount and validate probabilities
          pred0 = np.bincount(
              np.array(pred0).astype(np.int), minlength=n_class
          )
          pred1 = np.bincount(
              np.array(pred1).astype(np.int), minlength=n_class
          )
  
          assert np.allclose(
              np.true_divide(pred0, n_sample), probs[0], atol=1e-2
          )
          assert np.allclose(
              np.true_divide(pred0, n_sample),
              np.true_divide(ref0, n_sample),
              atol=1e-2,
          )
  
          assert np.allclose(
              np.true_divide(pred1, n_sample), probs[1], atol=1e-2
          )
          assert np.allclose(
              np.true_divide(pred1, n_sample),
              np.true_divide(ref1, n_sample),
              atol=1e-2,
          )
  
      # Test probs input
      input_placeholders = {"x": mb.placeholder(shape=(2, n_class))}
      input_values = {"x": np.array(probs)}
  
      def build(x):
          return [
              mb.random_categorical(
                  x=x, size=n_sample, mode="probs", name=output_name
              )
          ]
  
      if _IS_MACOS:
          prediction = get_core_ml_prediction(
              build, input_placeholders, input_values, backend=backend
          )
  
          pred0 = prediction[output_name].reshape(2, n_sample)[0]
          pred1 = prediction[output_name].reshape(2, n_sample)[1]
  
          # convert to bincount and validate probabilities
          pred0 = np.bincount(
              np.array(pred0).astype(np.int), minlength=n_class
          )
          pred1 = np.bincount(
              np.array(pred1).astype(np.int), minlength=n_class
          )
  
          assert np.allclose(
              np.true_divide(pred0, n_sample), probs[0], atol=1e-2
          )
          assert np.allclose(
              np.true_divide(pred0, n_sample),
              np.true_divide(ref0, n_sample),
              atol=1e-2,
          )
  
          assert np.allclose(
              np.true_divide(pred1, n_sample), probs[1], atol=1e-2
          )
          assert np.allclose(
              np.true_divide(pred1, n_sample),
              np.true_divide(ref1, n_sample),
              atol=1e-2,
          )
  
file_path: turicreate/src/external/coremltools_wrap/coremltools/coremltools/converters/mil/mil/ops/tests/test_random.py
imports: from coremltools.converters.mil import testing_reqs
  from coremltools.converters.mil.testing_reqs import *
  from coremltools.converters.mil.testing_utils import get_core_ml_prediction
  from coremltools._deps import _IS_MACOS
  from .testing_utils import UNK_SYM, run_compare_builder
label_desc: Use image node_id 3 for calling the TestRandomCategorical obj's underlying member method code with example usage: obj.test_builder_to_backend_stress(use_cpu_only, backend, n_sample, n_class) and returns: list, list