如果我有一个定义三重损失的函数(期望 y_true 和 y_pred 作为输入参数),并且我通过以下方式“引用或调用它”:
model.compile(optimizer="rmsprop", loss=triplet_loss, metrics=[accuracy])
y_pred 如何传递给triplet_loss 函数?
例如,triplet_loss 函数可能是:
def triplet_loss(y_true, y_pred, alpha = 0.2):
"""
Implementation of the triplet loss function
Arguments:
y_true -- true labels, required when you define a loss in Keras,
y_pred -- python list containing three objects:
"""
anchor, positive, negative = y_pred[0], y_pred[1], y_pred[2]
# distance between the anchor and the positive
pos_dist = tf.reduce_sum(tf.square(tf.subtract(anchor,positive)))
# distance between the anchor and the negative
neg_dist = tf.reduce_sum(tf.square(tf.subtract(anchor,negative)))
# compute loss
basic_loss = pos_dist-neg_dist+alpha
loss = tf.maximum(basic_loss,0.0)
return loss
谢谢乔恩