Keras стиль без потерь тройной потере - PullRequest
0 голосов
/ 06 января 2020

Я пытаюсь повторить потерю без потерь в триплете , но использую "К." Синтаксис, как в моей тройной потере ниже:

Мой код

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

Код из статьи

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

Как я понимаю, все, что мне нужно сделать, это вставить

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

в мой код. Есть ли «перевод» с «тф». стиль "к" стиль для этих линий?

Спасибо.

1 Ответ

1 голос
/ 06 января 2020

Вот способ, которым вы можете сделать:

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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...