def dist_weighted_sampling(labels, embeddings, high_var_threshold=0.5, nonzero_loss_threshold=1.4, neg_multiplier=1):
    '\n    Distance weighted sampling.\n    # References\n        - [sampling matters in deep embedding learning]\n          (https://arxiv.org/abs/1706.07567)\n\n    # Arguments:\n        labels: 1-D tf.int32 `Tensor` with shape [batch_size] of\n          multi-class integer labels.\n        embeddings: 2-D float `Tensor` of embedding vectors. Embeddings should\n          be l2 normalized.\n        high_var_threshold: float. cutoff for high gradient variance.\n        nonzero_loss_threshold: float. cutoff for non-zero loss zone.\n        neg_multiplier: int, default=1. the multiplier to enlarger the negative and positive samples.\n    Returns:\n        a_indices: indices of anchors.\n        anchors: sampled anchor embeddings.\n        positives: sampled positive embeddings.\n        negatives: sampled negative embeddings.\n    '
    if (not isinstance(neg_multiplier, int)):
        raise ValueError('`neg_multiplier` must be an integer.')
    n = tf.size(labels)
    if (not isinstance(embeddings, tf.Tensor)):
        embeddings = tf.convert_to_tensor(embeddings)
    d = embeddings.shape[1].value
    distances = pairwise_distance(embeddings, squared=False)
    distances = tf.maximum(distances, high_var_threshold)
    log_weights = (((2 - d) * tf.log((distances + 1e-16))) - ((0.5 * (d - 3)) * tf.log(((1 + 1e-16) - (0.25 * (distances ** 2))))))
    weights = tf.exp((log_weights - tf.reduce_max(log_weights)))
    lshape = tf.shape(labels)
    assert (lshape.shape == 1)
    labels = tf.reshape(labels, [lshape[0], 1])
    adjacency = tf.equal(labels, tf.transpose(labels))
    adjacency_not = tf.logical_not(adjacency)
    mask = tf.cast(adjacency_not, tf.float32)
    adjacency_ex = (tf.cast(adjacency, tf.int32) - tf.diag(tf.ones(n, dtype=tf.int32)))
    m = tf.reduce_sum(adjacency_ex, axis=1)
    if (tf.reduce_min(m) == 0):
        m = tf.diag(tf.cast(tf.equal(m, 0), tf.int32))
        adjacency_ex += m
    k = (tf.maximum(tf.reduce_max(m), 1) * neg_multiplier)
    pos_weights = tf.cast(adjacency_ex, tf.float32)
    weights = ((weights * mask) * tf.cast((distances < nonzero_loss_threshold), tf.float32))
    weights = (weights / (tf.reduce_sum(weights, axis=1, keepdims=True) + 1e-16))
    a_indices = tf.reshape(tf.range(n), ((- 1), 1))
    a_indices = tf.tile(a_indices, [1, k])
    a_indices = tf.reshape(a_indices, ((- 1),))

    def neg_sampling(i):
        s = tf.squeeze(tf.multinomial(tf.log(tf.expand_dims((weights[i] + 1e-16), axis=0)), k, output_dtype=tf.int32), axis=0)
        return s
    n_indices = tf.map_fn(neg_sampling, tf.range(n), dtype=tf.int32)
    n_indices = tf.reshape(n_indices, ((- 1),))

    def pos_sampling(i):
        s = tf.squeeze(tf.multinomial(tf.log(tf.expand_dims((pos_weights[i] + 1e-16), axis=0)), k, output_dtype=tf.int32), axis=0)
        return s
    p_indices = tf.map_fn(pos_sampling, tf.range(n), dtype=tf.int32)
    p_indices = tf.reshape(p_indices, ((- 1),))
    anchors = tf.gather(embeddings, a_indices, name='gather_anchors')
    positives = tf.gather(embeddings, p_indices, name='gather_pos')
    negatives = tf.gather(embeddings, n_indices, name='gather_neg')
    return (a_indices, anchors, positives, negatives)
Distance weighted sampling.
# References
    - [sampling matters in deep embedding learning]
      (https://arxiv.org/abs/1706.07567)

# Arguments:
    labels: 1-D tf.int32 `Tensor` with shape [batch_size] of
      multi-class integer labels.
    embeddings: 2-D float `Tensor` of embedding vectors. Embeddings should
      be l2 normalized.
    high_var_threshold: float. cutoff for high gradient variance.
    nonzero_loss_threshold: float. cutoff for non-zero loss zone.
    neg_multiplier: int, default=1. the multiplier to enlarger the negative and positive samples.
Returns:
    a_indices: indices of anchors.
    anchors: sampled anchor embeddings.
    positives: sampled positive embeddings.
    negatives: sampled negative embeddings.
def dist_weighted_sampling(labels, embeddings, high_var_threshold=0.5, nonzero_loss_threshold=1.4, neg_multiplier=1):
    '\n    Distance weighted sampling.\n    # References\n        - [sampling matters in deep embedding learning]\n          (https://arxiv.org/abs/1706.07567)\n\n    # Arguments:\n        labels: 1-D tf.int32 `Tensor` with shape [batch_size] of\n          multi-class integer labels.\n        embeddings: 2-D float `Tensor` of embedding vectors. Embeddings should\n          be l2 normalized.\n        high_var_threshold: float. cutoff for high gradient variance.\n        nonzero_loss_threshold: float. cutoff for non-zero loss zone.\n        neg_multiplier: int, default=1. the multiplier to enlarger the negative and positive samples.\n    Returns:\n        a_indices: indices of anchors.\n        anchors: sampled anchor embeddings.\n        positives: sampled positive embeddings.\n        negatives: sampled negative embeddings.\n    '
    if (not isinstance(neg_multiplier, int)):
        raise ValueError('`neg_multiplier` must be an integer.')
    n = tf.size(labels)
    if (not isinstance(embeddings, tf.Tensor)):
        embeddings = tf.convert_to_tensor(embeddings)
    d = embeddings.shape[1].value
    distances = pairwise_distance(embeddings, squared=False)
    distances = tf.maximum(distances, high_var_threshold)
    log_weights = (((2 - d) * tf.log((distances + 1e-16))) - ((0.5 * (d - 3)) * tf.log(((1 + 1e-16) - (0.25 * (distances ** 2))))))
    weights = tf.exp((log_weights - tf.reduce_max(log_weights)))
    lshape = tf.shape(labels)
    assert (lshape.shape == 1)
    labels = tf.reshape(labels, [lshape[0], 1])
    adjacency = tf.equal(labels, tf.transpose(labels))
    adjacency_not = tf.logical_not(adjacency)
    mask = tf.cast(adjacency_not, tf.float32)
    adjacency_ex = (tf.cast(adjacency, tf.int32) - tf.diag(tf.ones(n, dtype=tf.int32)))
    m = tf.reduce_sum(adjacency_ex, axis=1)
    if (tf.reduce_min(m) == 0):
        m = tf.diag(tf.cast(tf.equal(m, 0), tf.int32))
        adjacency_ex += m
    k = (tf.maximum(tf.reduce_max(m), 1) * neg_multiplier)
    pos_weights = tf.cast(adjacency_ex, tf.float32)
    weights = ((weights * mask) * tf.cast((distances < nonzero_loss_threshold), tf.float32))
    weights = (weights / (tf.reduce_sum(weights, axis=1, keepdims=True) + 1e-16))
    a_indices = tf.reshape(tf.range(n), ((- 1), 1))
    a_indices = tf.tile(a_indices, [1, k])
    a_indices = tf.reshape(a_indices, ((- 1),))

    def neg_sampling(i):
        s = tf.squeeze(tf.multinomial(tf.log(tf.expand_dims((weights[i] + 1e-16), axis=0)), k, output_dtype=tf.int32), axis=0)
        return s
    n_indices = tf.map_fn(neg_sampling, tf.range(n), dtype=tf.int32)
    n_indices = tf.reshape(n_indices, ((- 1),))

    def pos_sampling(i):
        s = tf.squeeze(tf.multinomial(tf.log(tf.expand_dims((pos_weights[i] + 1e-16), axis=0)), k, output_dtype=tf.int32), axis=0)
        return s
    p_indices = tf.map_fn(pos_sampling, tf.range(n), dtype=tf.int32)
    p_indices = tf.reshape(p_indices, ((- 1),))
    anchors = tf.gather(embeddings, a_indices, name='gather_anchors')
    positives = tf.gather(embeddings, p_indices, name='gather_pos')
    negatives = tf.gather(embeddings, n_indices, name='gather_neg')
    return (a_indices, anchors, positives, negatives)