Compilation keras如何通过model.compile将y_pred传递给丢失的对象/函数

Compilation keras如何通过model.compile将y_pred传递给丢失的对象/函数,compilation,keras,triplet,Compilation,Keras,Triplet,如果我有一个定义三重损失的函数(期望y_true和y_pred作为输入参数),并且我通过以下方式“引用或调用它”: model.compile(optimizer="rmsprop", loss=triplet_loss, metrics=[accuracy]) y_pred是如何传递到triplet_loss函数的 例如,三重态损耗函数可以是: def triplet_loss(y_true, y_pred, alpha = 0.2): """ Implementation

如果我有一个定义三重损失的函数(期望y_true和y_pred作为输入参数),并且我通过以下方式“引用或调用它”:

model.compile(optimizer="rmsprop", loss=triplet_loss, metrics=[accuracy]) 
y_pred是如何传递到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

谢谢Jon,我浏览了一下keras的源代码。在
Model()
类中:

首先,他们稍微修改函数以考虑权重:

self.loss_functions = loss_functions
weighted_losses = [_weighted_masked_objective(fn) for fn in loss_functions]
稍后在训练期间,他们将输出(预测)映射到目标(标签),并调用损失函数以获得输出损失。这里y_true和y_pred被传递到函数中

y_true = self.targets[i]
y_pred = self.outputs[i]
weighted_loss = weighted_losses[i]
sample_weight = sample_weights[i]
mask = masks[i]
loss_weight = loss_weights_list[i]
with K.name_scope(self.output_names[i] + '_loss'):
    output_loss = weighted_loss(y_true, y_pred,
                                sample_weight, mask)

谢谢你,我感谢你迅速而清晰的回答