-
Notifications
You must be signed in to change notification settings - Fork 1
/
triple_loss.py
32 lines (25 loc) · 1.43 KB
/
triple_loss.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import tensorflow as tf
def triplet_loss(y_true, y_pred, alpha = 0.2):
"""
Implementation of the triplet loss as defined by formula (3)
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 images, of shape (None, 128)
positive -- the encodings for the positive images, of shape (None, 128)
negative -- the encodings for the negative images, of shape (None, 128)
Returns:
loss -- real number, value of the loss
"""
anchor, positive, negative = y_pred[0], y_pred[1], y_pred[2]
# Step 1: Compute the (encoding) distance between the anchor and the positive
pos_dist = tf.reduce_sum( tf.square(tf.subtract(anchor,positive)),-1)
#pos_dist = tf.reduce_sum( tf.square(tf.subtract(anchor,positive)))
# Step 2: Compute the (encoding) distance between the anchor and the negative
neg_dist = tf.reduce_sum( tf.square(tf.subtract(anchor,negative)),-1)
#neg_dist = tf.reduce_sum( tf.square(tf.subtract(anchor,negative)))
# Step 3: subtract the two previous distances and add alpha.
basic_loss = tf.add(tf.subtract(pos_dist,neg_dist),alpha)
# Step 4: Take the maximum of basic_loss and 0.0. Sum over the training examples.
loss =tf.reduce_sum( tf.maximum(basic_loss,0.0))
return loss