Skip to content

Instantly share code, notes, and snippets.

@wenfahu
Last active August 6, 2018 10:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wenfahu/6ae4dc61ac7d0004e2a3b543eabb1509 to your computer and use it in GitHub Desktop.
Save wenfahu/6ae4dc61ac7d0004e2a3b543eabb1509 to your computer and use it in GitHub Desktop.
soft margin smooth hinge
def triplet_loss_soft(anchor, positive, negative, m=1):
"""Calculate the triplet loss according to the FaceNet paper
Args:
anchor: the embeddings for the anchor images.
positive: the embeddings for the positive images.
negative: the embeddings for the negative images.
Returns:
the triplet loss according to the FaceNet paper as a float tensor.
"""
with tf.variable_scope('triplet_loss'):
pos_dist = tf.reduce_sum(tf.square(tf.subtract(anchor, positive)), 1)
neg_dist = tf.reduce_sum(tf.square(tf.subtract(anchor, negative)), 1)
# basic_loss = tf.add(tf.subtract(pos_dist,neg_dist), m * tf.nn.softplus(pos_dist))
diff = tf.subtract(pos_dist, neg_dist)
margin = m * tf.nn.softplus(pos_dist)
loss = tf.relu(tf.add(diff, margin))
quad = tf.minimum(loss, m)
linear = m * ( loss - quad)
total_loss = tf.square(quad) + 2 * margin * linear
total_loss = tf.reduce_mean(total_loss)
return total_loss
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment