code: def organize_valid_indices(is_valid, shuffle=True, seed=None):
      """Organizes indices in such a way that valid items appear first.
  
      Args:
        is_valid: A boolean `Tensor` for entry validity with shape [batch_size,
          list_size].
        shuffle: A boolean indicating whether valid items should be shuffled.
        seed: An int for random seed at the op level. It works together with the
          seed at global graph level together to determine the random number
          generation. See `tf.set_random_seed`.
  
      Returns:
        A tensor of indices with shape [batch_size, list_size, 2]. The returned
        tensor can be used with `tf.gather_nd` and `tf.scatter_nd` to compose a new
        [batch_size, list_size] tensor. The values in the last dimension are the
        indices for an element in the input tensor.
      """
      with tf.compat.v1.name_scope(name="organize_valid_indices"):
          is_valid = tf.convert_to_tensor(value=is_valid)
          is_valid.get_shape().assert_has_rank(2)
          output_shape = tf.shape(input=is_valid)
  
          if shuffle:
              values = tf.random.uniform(output_shape, seed=seed)
          else:
              values = tf.ones_like(is_valid, tf.float32) * tf.reverse(
                  tf.cast(tf.range(output_shape[1]), dtype=tf.float32),
                  [-1],
              )
  
          rand = tf.where(
              is_valid, values, tf.ones(output_shape) * -1e-6
          )
          # shape(indices) = [batch_size, list_size]
          indices = tf.argsort(
              rand, direction="DESCENDING", stable=True
          )
          return _to_nd_indices(indices)
  
file_path: ranking/tensorflow_ranking/python/utils.py
imports: from typing import Callable, Dict, Tuple
  import tensorflow
label_desc: Use image node_id 8 for calling a global function with example usage: organize_valid_indices(is_valid, shuffle, seed) and returns: _to_nd_indices