code: def _get_first_or_last_n_item_selectable(
      input_ids, axis, all_selectable, num_to_select, reverse=False
  ):
      """Selects the first or last N selectable items.
  
      reverse=True means select last. Otherwise select the first N items.
  
      Args:
        input_ids: a `RaggedTensor`.
        axis: axis to apply selection on.
        all_selectable: a `RaggedTensor` with dtype of bool and with shape
          `input_ids.shape[:axis]`. Used to identify all selectable ids that could
          be selectable.
        num_to_select: An int which is the number of ids to select.
        reverse: boolean when False means select the first N selectable ids.
          Otherwise `reverse=True` means select the last N selectable ids.
  
      Returns:
        a `RaggedTensor` with dtype of bool and with shape `input_ids.shape[:axis]`.
        Its values are True for the first or last N selectable items.
      """
      axis = array_ops.get_positive_axis(
          axis, input_ids.ragged_rank + input_ids.flat_values.shape.rank
      )
      # Create a positions RT and mask out positions that are not selectable
      positions_flat = math_ops.range(
          array_ops.size(input_ids.flat_values)
      )
      positions = input_ids.with_flat_values(positions_flat)
      selectable_positions = ragged_array_ops.boolean_mask(
          positions, all_selectable
      )
  
      # merge to the desired axis
      selectable_positions = (
          selectable_positions.merge_dims(1, axis)
          if axis > 1
          else selectable_positions
      )
  
      # Get a selection mask based off of how many items are desired for selection
      merged_axis = axis - (axis - 1)
      selection_mask = _get_selection_mask(
          selectable_positions, num_to_select, merged_axis, reverse
      )
      # Mask out positions that were not selected.
      selected_positions = ragged_array_ops.boolean_mask(
          selectable_positions, selection_mask
      )
  
      # Now that we have all the positions which were chosen, we recreate a mask
      # (matching the original input's shape) where the value is True if it was
      # selected. We do this by creating a "all false" RT and scattering true
      # values to the positions chosen for selection.
      all_true = selected_positions.with_flat_values(
          array_ops.ones_like(selected_positions.flat_values)
      )
      all_false = array_ops.zeros(
          array_ops.shape(input_ids.flat_values), dtypes.int32
      )
      results_flat = array_ops.tensor_scatter_update(
          all_false,
          array_ops.expand_dims(selected_positions.flat_values, -1),
          all_true.flat_values,
      )
      results = input_ids.with_flat_values(results_flat)
      results = math_ops.cast(results, dtypes.bool)
  
      # Reduce until input.shape[:axis]
      for _ in range(input_ids.shape.ndims - axis - 1):
          results = math_ops.reduce_all(results, -1)
      return results
  
imports: from tensorflow.python.framework import dtypes
  from tensorflow.python.framework import ops
  from tensorflow.python.framework import tensor
  from tensorflow.python.ops import array_ops
  from tensorflow.python.ops import array_ops_stack
  from tensorflow.python.ops import gen_array_ops
  from tensorflow.python.ops import map_fn
  from tensorflow.python.ops import math_ops
  from tensorflow.python.ops import random_ops
  from tensorflow.python.ops.ragged import ragged_array_ops
  from tensorflow.python.ops.ragged import ragged_functional_ops
  from tensorflow.python.ops.ragged import ragged_math_ops
  from tensorflow.python.ops.ragged import ragged_tensor
label_desc: Use image node_id 3 for calling a global function with example usage: _get_first_or_last_n_item_selectable(input_ids, axis, all_selectable, num_to_select, reverse) and returns: results