如何编译具有 3 个参数的自定义损失的 2 个输出的 keras 模型?

How to compile a keras model that has 2 outputs with a custom loss that takes 3 parameters?

我正在尝试使用自定义损失函数编译具有 2 个输出的模型,但我没有这样做。 有任何想法吗?让我告诉你我做了什么,

这是损失函数:

def contrastive_loss(y_true, y_pred1, y_pred2):
    '''Contrastive loss from Hadsell-et-al.'06
    http://yann.lecun.com/exdb/publis/pdf/hadsell-chopra-lecun-06.pdf
    '''
    euclidean_distance = pairwise_dist(y_pred1, y_pred2)
    loss_contrastive = K.mean((1-y_true) * tf.pow(euclidean_distance, 2) + 
                                  (y_true) * tf.pow(tf.clip_by_value(2.0 - euclidean_distance, 0.0, 2.0), 2))

    return loss_contrastive

我试过这个:

optimizer = Adam(lr = 0.00006)
model.compile(loss=[lambda y_true,y_pred: contrastive_loss(y_true, y_pred[0], y_pred[1])],optimizer=optimizer)

但是我得到这个错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-27-b31099307b2d> in <module>
     15 [lambda y_true,y_pred: Custom_loss(y_true, y_pred, val=0.01)]
     16 
---> 17 model.compile(loss=[lambda y_true,y_pred: contrastive_loss(y_true, y_pred[0], y_pred[1])],optimizer=optimizer)
     18 
     19 print("Starting training process!")

C:\ProgramData\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py in symbolic_fn_wrapper(*args, **kwargs)
     73         if _SYMBOLIC_SCOPE.value:
     74             with get_graph().as_default():
---> 75                 return func(*args, **kwargs)
     76         else:
     77             return func(*args, **kwargs)

C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\training.py in compile(self, optimizer, loss, metrics, loss_weights, sample_weight_mode, weighted_metrics, target_tensors, **kwargs)
    117         # Prepare list of loss functions, same size as model outputs.
    118         self.loss_functions = training_utils.prepare_loss_functions(
--> 119             self.loss, self.output_names)
    120 
    121         self._feed_outputs = []

C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\training_utils.py in prepare_loss_functions(loss, output_names)
    825             raise ValueError('When passing a list as loss, it should have one entry '
    826                              'per model outputs. The model has {} outputs, but you '
--> 827                              'passed loss={}'.format(len(output_names), loss))
    828         loss_functions = [get_loss_function(l) for l in loss]
    829     else:

ValueError: When passing a list as loss, it should have one entry per model outputs. The model has 2 outputs, but you passed loss=[<function <lambda> at 0x0000000041EFCB88>]

如何解决?

如果两个 pred 具有相同的形状,将它们连接到一个输出中:

final_output = Lambda(lambda x: tf.stack(x, axis=0))([output1, output2])

在你输的时候,你把它们拆开:

def contrastive_loss(y_true, y_pred):
    y_pred1 = y_pred[0]
    y_pred2 = y_pred[1]

    '''Contrastive loss from Hadsell-et-al.'06
    http://yann.lecun.com/exdb/publis/pdf/hadsell-chopra-lecun-06.pdf
    '''
    euclidean_distance = pairwise_dist(y_pred1, y_pred2)
    loss_contrastive = K.mean((1-y_true) * tf.pow(euclidean_distance, 2) + 
                                (y_true) * tf.pow(tf.clip_by_value(
                                                     2.0 - euclidean_distance, 0.0, 2.0), 
                              2))

    return loss_contrastive

如果两个 pred 的形状不同,请转到此处:Keras: Custom loss function with training data not directly related to model