Skip to content

Instantly share code, notes, and snippets.

@pranjalg2308
Created May 25, 2020 14:35
Show Gist options
  • Save pranjalg2308/f591533e9a651a8d4d96313746746dc3 to your computer and use it in GitHub Desktop.
Save pranjalg2308/f591533e9a651a8d4d96313746746dc3 to your computer and use it in GitHub Desktop.
# L2 Distance
def triplet_loss(alpha, emb_dim):
def loss(y_true, y_pred):
anc, pos, neg = y_pred[:,:emb_size], y_pred[:,emb_size:2*emb_size], y_pred[:,2*emb_size:]
distance1 = tf.sqrt(tf.reduce_sum(tf.pow(anc - pos, 2), 1, keepdims=True))
distance2 = tf.sqrt(tf.reduce_sum(tf.pow(anc - neg, 2), 1, keepdims=True))
return tf.reduce_mean(tf.maximum(distance1 - distance2 + alpha, 0.))
return loss
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment