Steve Brown
Steve Brown

Reputation: 449

Keras style lossless triplet loss

I am trying to replicate the lossless tripler loss, but using the "K." syntax, like in my triplet loss below:

My code

def triplet_loss_01(y_true, y_pred, alpha = 0.2):

  total_lenght = y_pred.shape.as_list()[-1]
  print("triplet_loss.total_lenght: ", total_lenght)

  anchor = y_pred[:,0:int(total_lenght*1/3)]
  positive = y_pred[:,int(total_lenght*1/3):int(total_lenght*2/3)]
  negative = y_pred[:,int(total_lenght*2/3):int(total_lenght*3/3)]

  pos_dist = K.sum(K.square(anchor-positive),axis=1)

  neg_dist = K.sum(K.square(anchor-negative),axis=1)

  basic_loss = pos_dist-neg_dist+alpha
  loss = K.maximum(basic_loss,0.0)

  return loss

Code from the article

def lossless_triplet_loss(y_true, y_pred, N = 3, beta=N, epsilon=1e-8):

    anchor = tf.convert_to_tensor(y_pred[:,0:N])
    positive = tf.convert_to_tensor(y_pred[:,N:N*2]) 
    negative = tf.convert_to_tensor(y_pred[:,N*2:N*3])

    # distance between the anchor and the positive
    pos_dist = tf.reduce_sum(tf.square(tf.subtract(anchor,positive)),1)
    # distance between the anchor and the negative
    neg_dist = tf.reduce_sum(tf.square(tf.subtract(anchor,negative)),1)

    #Non Linear Values  

    # -ln(-x/N+1)
    pos_dist = -tf.log(-tf.divide((pos_dist),beta)+1+epsilon)
    neg_dist = -tf.log(-tf.divide((N-neg_dist),beta)+1+epsilon)

    # compute loss
    loss = neg_dist + pos_dist

    return loss

As I unterstand, all I have to do is to insert

pos_dist = -tf.log(-tf.divide((pos_dist),beta)+1+epsilon)
neg_dist = -tf.log(-tf.divide((N-neg_dist),beta)+1+epsilon)

in my code. Is there a "translation" from "tf." style to "K." style for these lines?

Thank you.

Upvotes: 1

Views: 459

Answers (1)

YOLO
YOLO

Reputation: 21709

Here's a way you can do:

VECTOR_SIZE = 10 # set it to value based on your model

def lossless_triplet_loss(y_true, y_pred, N = VECTOR_SIZE, beta=VECTOR_SIZE, epsilon=1e-8):

    anchor = y_pred[:,0:N]
    positive = y_pred[:,N:2*N]
    negative = y_pred[:,2*N:]

    # 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)

    # some magic
    pos_dist = -K.log(-((pos_dist) / beta)+1+epsilon)
    neg_dist = -K.log(-((N-neg_dist) / beta)+1+epsilon)

    loss = neg_dist + pos_dist

    return loss

Upvotes: 2

Related Questions