import tensorflow as tf


    Returns:

    A boolean tensor with the same shape as input (indicator) tensor
    """
    indices = tf.where(indicator)
    indices = tf.random.shuffle(indices)
    indices = tf.reshape(indices, [-1])

    num_samples = tf.minimum(tf.size(indices), num_samples)
    selected_indices = tf.slice(indices, [0], tf.reshape(num_samples, [1]))

    selected_indicator = ops.indices_to_dense_vector(selected_indices, tf.shape(indicator)[0])

    return tf.equal(selected_indicator, 1)


def sample_balanced_positive_negative(indicator, sample_size, labels, positive_fraction=0.5):
    """Subsamples minibatches to a desired balance of positives and negatives.

    Arguments:

    - *indicator*: boolean tensor of shape [N] whose True entries can be sampled.
    - *sample_size*: desired batch size. If None, keeps all positive samples and
