【问题标题】:Understanding the Keras MNIST Siamese Network and adapting it for triples了解 Keras MNIST 连体网络并将其调整为三元组
【发布时间】:2020-08-03 04:03:46
【问题描述】:

我目前正在使用 Keras 在 Python 中调整 this siamese 网络。但是,我目前不明白损失是如何工作的(不是函数本身,而是哪些参数在哪里传递)

好的,现在一步一步我认为这是如何工作的:

distance = Lambda(euclidean_distance,
              output_shape=eucl_dist_output_shape)([processed_a, processed_b])

这是两个单独网络的输出组合在一起的行,自定义层应用以下功能:

def euclidean_distance(vects):
    x, y = vects
    sum_square = K.sum(K.square(x - y), axis=1, keepdims=True)
    return K.sqrt(K.maximum(sum_square, K.epsilon()))


def eucl_dist_output_shape(shapes):
    shape1, shape2 = shapes
    return (shape1[0], 1)

因此,当该层的输入为 (128, 128) 时,输出将为 (128, 1)。在最后一步中,损失计算如下:

def contrastive_loss(y_true, y_pred):
    '''Contrastive loss from Hadsell-et-al.'06
    http://yann.lecun.com/exdb/publis/pdf/hadsell-chopra-lecun-06.pdf
    '''
    margin = 1
    square_pred = K.square(y_pred)
    margin_square = K.square(K.maximum(margin - y_pred, 0))
    return K.mean(y_true * square_pred + (1 - y_true) * margin_square)

在这里,预测的 128D 向量与 128D 地面实况向量进行比较。

现在我将 Lambda 层更改为:

distance = Lambda(euclidean_distance,
                  output_shape=eucl_dist_output_shape)([processed_a, processed_b, processed_c])

所以我现在有三个网络,具有以下适应功能(应该将三个输出组合为一个输出,形状为 (128, 3)):

def euclidean_distance(vects):
    return vects


def eucl_dist_output_shape(shapes):
    shape1, shape2, shape3 = shapes
    return (shape1, shape2, shape3)

然后是新的损失函数:

def loss_desc_triplet(vects, margin=5):
    """Triplet loss.
    """
    d1, d2, d3 = vects
    d_pos = K.sqrt(K.sum(K.square(d1 - d2), axis=1))
    pair_dist_1_to_3 = K.sqrt(K.sum(K.square(d1 - d3), axis=1))
    d_neg = pair_dist_1_to_3

    return Activation.relu(d_pos - d_neg + margin)

但现在我得到了这个错误:

文件 "DeepLearningWithAugmentationWithTriplets.py", 第 233 行,在 output_shape=eucl_dist_output_shape)([已处理的_a,已处理的_b,已处理的_c])

文件 “lib/python3.7/site-packages/keras/engine/base_layer.py”, 第 497 行,在 调用 参数=user_kwargs)

文件 “lib/python3.7/site-packages/keras/engine/base_layer.py”, 第 565 行,在 _add_inbound_node output_tensors[i]._keras_shape = output_shapes[i]

IndexError: 列表索引超出范围

但我不确定是什么原因造成的。

【问题讨论】:

    标签: python keras deep-learning


    【解决方案1】:

    我通过连接输出解决了这个问题:

    merged_vector = concatenate([processed_a, processed_b, processed_c], axis=-1, name='merged_layer')
    

    然后在我的损失函数中分解向量:

    d1 = y_pred[:,0:128]
    d2 = y_pred[:,128:256]
    d3 = y_pred[:,256:384]
    

    不过,我不确定这是否是最佳解决方案。

    【讨论】:

      猜你喜欢
      • 2017-05-28
      • 1970-01-01
      • 2020-07-16
      • 1970-01-01
      • 2019-07-14
      • 1970-01-01
      • 1970-01-01
      • 2018-03-21
      • 1970-01-01
      相关资源
      最近更新 更多