我正在尝试复制lossless tripler loss,但使用的是“ K”。语法,就像下面我的三元组损失一样:
我的代码
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)
在我的代码中。是否有“ tf”的“翻译”。样式为“ K”。这些线的样式?
谢谢。
答案 0 :(得分:1)
这是您可以采取的方法:
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