Created
May 25, 2020 14:35
-
-
Save pranjalg2308/f591533e9a651a8d4d96313746746dc3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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