code: def _resample_using_log_points(
      log_probs, sample_shape, log_points, name=None
  ):
      """Resample from `log_probs` using supplied points in interval `[0, 1]`."""
  
      # We divide up the unit interval [0, 1] according to the provided
      # probability distributions using `cumulative_logsumexp`.
      # At the end of each division we place a 'marker'.
      # We use points on the unit interval supplied by caller.
      # We sort the combination of points and markers. The number
      # of points between the markers defining a division gives the number
      # of samples we require in that division.
      # For example, suppose `probs` is `[0.2, 0.3, 0.5]`.
      # We divide up `[0, 1]` using 3 markers:
      #
      #     |     |          |
      # 0.  0.2   0.5        1.0  <- markers
      #
      # Suppose we are given four points: [0.1, 0.25, 0.9, 0.75]
      # After sorting the combination we get:
      #
      # 0.1  0.25     0.75 0.9    <- points
      #  *  | *   |    *    *|
      # 0.   0.2 0.5         1.0  <- markers
      #
      # We have one sample in the first category, one in the second and
      # two in the last.
      #
      # All of these computations are carried out in batched form.
  
      with tf.name_scope(name or "resample_using_log_points") as name:
          points_shape = ps.shape(log_points)
          batch_shape, [num_markers] = ps.split(
              ps.shape(log_probs), num_or_size_splits=[-1, 1]
          )
  
          # `working_shape` specifies the total number of events
          # we will be generating.
          working_shape = ps.concat([sample_shape, batch_shape], axis=0)
          # `markers_shape` is the shape of the markers we temporarily insert.
          markers_shape = ps.concat(
              [working_shape, [num_markers]], axis=0
          )
  
          markers = ps.concat(
              [
                  tf.ones(markers_shape, dtype=tf.int32),
                  tf.zeros(points_shape, dtype=tf.int32),
              ],
              axis=-1,
          )
          log_marker_positions = tf.broadcast_to(
              tf.math.cumulative_logsumexp(log_probs, axis=-1),
              markers_shape,
          )
          log_markers_and_points = ps.concat(
              [log_marker_positions, log_points], axis=-1
          )
          # Stable sort is used to ensure that no points get sorted between
          # markers that have zero distance between them. This ensures that
          # there will never be a sample drawn whose probability is intended
          # to be zero even when a point falls on the edge of the
          # corresponding zero-width bucket.
          indices = tf.argsort(
              log_markers_and_points, axis=-1, stable=True
          )
          sorted_markers = tf.gather_nd(
              markers,
              indices[..., tf.newaxis],
              batch_dims=(
                  ps.rank_from_shape(sample_shape)
                  + ps.rank_from_shape(batch_shape)
              ),
          )
          markers_and_samples = ps.cast(
              tf.cumsum(sorted_markers, axis=-1), dtype=tf.int32
          )
          markers_and_samples = tf.math.minimum(
              markers_and_samples, num_markers - np.int32(1)
          )
  
          # Collect up samples, omitting markers.
          samples_mask = tf.equal(sorted_markers, 0)
  
          # The following block of code is equivalent to
          # `samples = markers_and_samples[samples_mask]` however boolean mask
          # indices are not supported by XLA.
          # Instead we use `argsort` to pick out the top `num_samples`
          # elements of `markers_and_samples` when sorted using `samples_mask`
          # as key.
          num_samples = points_shape[-1]
          sample_locations = tf.argsort(
              ps.cast(samples_mask, dtype=tf.int32),
              direction="DESCENDING",
              stable=True,
          )
          samples = tf.gather_nd(
              markers_and_samples,
              sample_locations[..., :num_samples, tf.newaxis],
              batch_dims=(
                  ps.rank_from_shape(sample_shape)
                  + ps.rank_from_shape(batch_shape)
              ),
          )
  
          return tf.reshape(samples, points_shape)
  
file_path: probability/tensorflow_probability/python/experimental/mcmc/weighted_resampling.py
imports: import numpy
  import tensorflow.compat.v2
  from tensorflow_probability.python.distributions import exponential
  from tensorflow_probability.python.distributions import uniform
  from tensorflow_probability.python.internal import distribution_util
  from tensorflow_probability.python.internal import prefer_static
  from tensorflow_probability.python.math.gradient import value_and_gradient
  from tensorflow_probability.python.mcmc.internal import util
label_desc: Use image node_id 2 for calling a global function with example usage: _resample_using_log_points(log_probs, sample_shape, log_points, name) and returns: tf