def margin_based_loss(labels, embeddings, beta_in=1.0, margin=0.2, nu=0.0, high_var_threshold=0.5, nonzero_loss_threshold=1.4, neg_multiplier=1):
    '\n    Computes the margin base loss.\n    # References\n        - [sampling matters in deep embedding learning]\n          (https://arxiv.org/abs/1706.07567)\n\n    Args:\n        labels: 1-D. tf.int32 `Tensor` with shape [batch_size] of multi-class integer labels.\n        embeddings: 2-D float `Tensor` of embedding vectors. Embeddings should be l2 normalized.\n        beta_in: float,int or 1-D, float `Tensor` with shape [labels_size] of multi-class boundary parameters.\n        margin: Float, margin term in the loss function.\n        nu: float. Regularization parameter for beta.\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        margin_based_Loss: tf.float32 scalar\n    '
    (a_indices, anchors, positives, negatives) = dist_weighted_sampling(labels, embeddings, high_var_threshold=high_var_threshold, nonzero_loss_threshold=nonzero_loss_threshold, neg_multiplier=neg_multiplier)
    if isinstance(beta_in, (float, int)):
        beta = beta_in
        beta_reg_loss = 0.0
    elif isinstance(beta_in, tf.Tensor):
        assert (tf.shape(beta_in).shape == 1)
        k = (tf.size(a_indices) / tf.size(labels))
        k = tf.cast(k, tf.int32)
        beta = tf.reshape(beta_in, ((- 1), 1))
        beta = tf.tile(beta, [1, k])
        beta = tf.reshape(beta, ((- 1),))
        beta_reg_loss = (tf.reduce_sum(beta) * nu)
    else:
        raise ValueError('`beta_in` must be one of [float, int, tf.Tensor].')
    d_ap = tf.sqrt((tf.reduce_sum(tf.square((positives - anchors)), axis=1) + 1e-16))
    d_an = tf.sqrt((tf.reduce_sum(tf.square((negatives - anchors)), axis=1) + 1e-16))
    pos_loss = tf.maximum(((margin + d_ap) - beta), 0)
    neg_loss = tf.maximum(((margin + beta) - d_an), 0)
    pair_cnt = tf.cast(tf.size(a_indices), tf.float32)
    loss = (((tf.reduce_sum(pos_loss) + tf.reduce_sum(neg_loss)) + beta_reg_loss) / pair_cnt)
    return loss
Computes the margin base loss.
# References
    - [sampling matters in deep embedding learning]
      (https://arxiv.org/abs/1706.07567)

Args:
    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.
    beta_in: float,int or 1-D, float `Tensor` with shape [labels_size] of multi-class boundary parameters.
    margin: Float, margin term in the loss function.
    nu: float. Regularization parameter for beta.
    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:
    margin_based_Loss: tf.float32 scalar
def margin_based_loss(labels, embeddings, beta_in=1.0, margin=0.2, nu=0.0, high_var_threshold=0.5, nonzero_loss_threshold=1.4, neg_multiplier=1):
    '\n    Computes the margin base loss.\n    # References\n        - [sampling matters in deep embedding learning]\n          (https://arxiv.org/abs/1706.07567)\n\n    Args:\n        labels: 1-D. tf.int32 `Tensor` with shape [batch_size] of multi-class integer labels.\n        embeddings: 2-D float `Tensor` of embedding vectors. Embeddings should be l2 normalized.\n        beta_in: float,int or 1-D, float `Tensor` with shape [labels_size] of multi-class boundary parameters.\n        margin: Float, margin term in the loss function.\n        nu: float. Regularization parameter for beta.\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        margin_based_Loss: tf.float32 scalar\n    '
    (a_indices, anchors, positives, negatives) = dist_weighted_sampling(labels, embeddings, high_var_threshold=high_var_threshold, nonzero_loss_threshold=nonzero_loss_threshold, neg_multiplier=neg_multiplier)
    if isinstance(beta_in, (float, int)):
        beta = beta_in
        beta_reg_loss = 0.0
    elif isinstance(beta_in, tf.Tensor):
        assert (tf.shape(beta_in).shape == 1)
        k = (tf.size(a_indices) / tf.size(labels))
        k = tf.cast(k, tf.int32)
        beta = tf.reshape(beta_in, ((- 1), 1))
        beta = tf.tile(beta, [1, k])
        beta = tf.reshape(beta, ((- 1),))
        beta_reg_loss = (tf.reduce_sum(beta) * nu)
    else:
        raise ValueError('`beta_in` must be one of [float, int, tf.Tensor].')
    d_ap = tf.sqrt((tf.reduce_sum(tf.square((positives - anchors)), axis=1) + 1e-16))
    d_an = tf.sqrt((tf.reduce_sum(tf.square((negatives - anchors)), axis=1) + 1e-16))
    pos_loss = tf.maximum(((margin + d_ap) - beta), 0)
    neg_loss = tf.maximum(((margin + beta) - d_an), 0)
    pair_cnt = tf.cast(tf.size(a_indices), tf.float32)
    loss = (((tf.reduce_sum(pos_loss) + tf.reduce_sum(neg_loss)) + beta_reg_loss) / pair_cnt)
    return loss