code: def test_multi_label_dot_product_loss__sample_candidates_with_constant_number_of_labels(
      monkeypatch: MonkeyPatch,
  ):
      num_candidates = 2
      num_features = 4
      batch_size = 3
      layer = MultiLabelDotProductLoss(
          num_candidates, scale_loss=False, similarity_type=INNER
      )
  
      # Seven random vectors for inputs and labels
      i0, i1, i2, l0, l1, l2, l3 = np.round(
          np.random.uniform(-100, 100, size=[7, num_features])
      ).tolist()
  
      # Each example in the batch has one input
      batch_inputs_embed = tf.constant(
          [[i0], [i1], [i2]], dtype=tf.float32
      )
      # Each input can have multiple labels (here its always the same number of labels,
      # but it doesn't have to be)
      batch_labels_embed = tf.constant(
          [[l0, l1], [l2, l3], [l3, l0]], dtype=tf.float32
      )
      # We assign the corresponding indices
      batch_labels_ids = tf.constant(
          [[[0], [1]], [[2], [3]], [[3], [0]]], dtype=tf.float32
      )
      # List all the labels and ids in play
      all_labels_embed = tf.constant([l0, l1, l2, l3], dtype=tf.float32)
      all_labels_ids = tf.constant(
          [[0], [1], [2], [3]], dtype=tf.float32
      )
  
      # Inside `layer._sample_candidates` random indices will be generated for the
      # candidates. We mock them to have a deterministic output.
      mock_indices = [0, 2, 0, 1, 0, 3]
  
      def mock_random_indices(*args, **kwargs) -> tf.Tensor:
          return tf.reshape(
              tf.constant(mock_indices), [batch_size, num_candidates]
          )
  
      monkeypatch.setattr(
          layers_utils, "random_indices", mock_random_indices
      )
  
      # Now run the function we want to test
      (
          pos_inputs_embed,
          pos_labels_embed,
          candidate_labels_embed,
          pos_neg_labels,
      ) = layer._sample_candidates(
          batch_inputs_embed,
          batch_labels_embed,
          batch_labels_ids,
          all_labels_embed,
          all_labels_ids,
      )
      # The inputs just stay the inputs, up to an extra dimension
      assert np.all(
          pos_inputs_embed.numpy()
          == tf.expand_dims(batch_inputs_embed, axis=-2).numpy()
      )
      # All positive labels of each batch are in `pos_labels_embed`
      assert np.all(
          pos_labels_embed.numpy()
          == np.array([[[l0, l1]], [[l2, l3]], [[l3, l0]]])
      )
      # The candidate label embeddings are picked according to the `mock_indices` above.
      # E.g. a 2 coming from `mock_indices` means that `all_labels_embed[2]` is picked,
      # i.e. `l2`.
      assert np.all(
          candidate_labels_embed.numpy()
          == np.array([[[l0, l2]], [[l0, l1]], [[l0, l3]]])
      )
      # The `pos_neg_labels` contains `1`s wherever the vector in `candidate_labels_embed`
      # of example `i` is actually in the possible lables of example `i`
      assert np.all(
          pos_neg_labels.numpy()
          == np.array(
              [
                  [
                      1,
                      0,
                  ],  # l0 is an actual positive example in `batch_labels_embed[0]`,
                  # whereas l2 is not
                  [
                      0,
                      0,
                  ],  # Neither l0 nor l3 are positive examples in `batch_labels_embed[1]`
                  [
                      1,
                      1,
                  ],  # l0 and l3 are both positive examples in `batch_labels_embed[2]`
              ]
          )
      )
  
imports: from typing import Text, List, Tuple, Union, Optional
  import pytest
  from _pytest.monkeypatch import MonkeyPatch
  import numpy
  import tensorflow
  from rasa.utils.tensorflow.layers import DotProductLoss, MultiLabelDotProductLoss, RandomlyConnectedDense, DenseForSparse
  from rasa.utils.tensorflow.constants import INNER, SOFTMAX, LABEL, LABEL_PAD_ID
  import rasa.utils.tensorflow.layers_utils
  from rasa.shared.nlu.constants import TEXT, INTENT, ACTION_NAME, ACTION_TEXT, FEATURE_TYPE_SENTENCE, FEATURE_TYPE_SEQUENCE
  from rasa.core.constants import DIALOGUE
label_desc: Use image node_id 6 for calling a global function with example usage: test_multi_label_dot_product_loss__sample_candidates_with_constant_number_of_labels(monkeypatch) and returns: tf