Last active
February 15, 2018 15:52
-
-
Save TheLoneNut/d4e1797ca7ea7c5b82474f64ca7a3fc8 to your computer and use it in GitHub Desktop.
Siamese Network
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
def triplet_loss(y_true, y_pred, alpha = ALPHA): | |
""" | |
Implementation of the triplet loss function | |
Arguments: | |
y_true -- true labels, required when you define a loss in Keras, you don't need it in this function. | |
y_pred -- python list containing three objects: | |
anchor -- the encodings for the anchor data | |
positive -- the encodings for the positive data (similar to anchor) | |
negative -- the encodings for the negative data (different from anchor) | |
Returns: | |
loss -- real number, value of the loss | |
""" | |
anchor = y_pred[:,0:3] | |
positive = y_pred[:,3:6] | |
negative = y_pred[:,6:9] | |
# distance between the anchor and the positive | |
pos_dist = K.sum(K.square(anchor-positive),axis=1) | |
# distance between the anchor and the negative | |
neg_dist = K.sum(K.square(anchor-negative),axis=1) | |
# compute loss | |
basic_loss = pos_dist-neg_dist+alpha | |
loss = K.maximum(basic_loss,0.0) | |
return loss |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment