python - Keras 合并 VS 连接,无法更新我的代码

标签 python python-3.x tensorflow keras

我有一个 CNN 的 Keras 功能模型。我正在尝试实现一个三元组损失函数。我发现了一些关于谁可以使用“合并”来执行此操作的帖子,该功能现已弃用,但我无法使用“连接”,因为我正在使用合并。

原始代码如下所示:

def triplet_loss(x):
    anchor, positive, negative = x
    pos_dist = tf.reduce_sum(tf.square(tf.subtract(anchor, positive)), 1)
    neg_dist = tf.reduce_sum(tf.square(tf.subtract(anchor, negative)), 1)

    basic_loss = tf.add(tf.subtract(pos_dist, neg_dist), 0.05)
    loss = tf.reduce_mean(tf.maximum(basic_loss, 0.0), 0)
    return loss



def build_model(img_x, img_y):
    input_shape = Input(shape=(img_x, img_y, 3))
    c0 = Conv2D(32, kernel_size=(3, 3), strides=(1, 1), activation='relu') (input_shape)
    m0 = MaxPooling2D(pool_size=(2, 2), strides=(2, 2)) (c0)
    f = Flatten()(m0)
    d1 = Dense(4024, activation='relu')(f)
    d2 = Dense(512, activation='sigmoid')(d1)

    anchor = Input(shape=(128, 254, 3))
    positive = Input(shape=(128, 254, 3))
    negative = Input(shape=(128, 254, 3))

    reid_model = Model(inputs=[input_shape], outputs=[d2])

    anchor_embed = reid_model(anchor)
    positive_embed = reid_model(positive)
    negative_embed = reid_model(negative)

    loss = merge([anchor_embed, positive_embed, negative_embed],
             mode=triplet_loss, output_shape=(1,))

    model = Model(inputs=[anchor, positive, negative], outputs=loss)
    model.compile(optimizer='Adam', loss='mean_absolute_error')
    return model

我使用 loss = merge([anchor_embed, Positive_embed, negative_embed], mode=triplet_loss, output_shape=(1,)) 作为转换函数 triplet_loss 输出的方法 进入 keras 层输出(如 https://codepad.co/snippet/F1uVDD5N 中的建议)。函数concatenate没有参数“mode”。有什么方法可以调整我的代码以获得损失函数的结果作为 Keras 层输出吗?

最佳答案

我终于找到了一种方法来计算 triplet_loss 函数的值,通过添加 lambda 层来保持代码的原始架构。

def triplet_loss(x):
    anchor, positive, negative = x
    pos_dist = tf.reduce_sum(tf.square(tf.subtract(anchor, positive)), 1)
    neg_dist = tf.reduce_sum(tf.square(tf.subtract(anchor, negative)), 1)

    basic_loss = tf.add(tf.subtract(pos_dist, neg_dist), 0.05)
    loss = tf.reduce_mean(tf.maximum(basic_loss, 0.0), 0)
    return loss

def build_model(img_x, img_y):
    input_shape = Input(shape=(img_x, img_y, 3))
    c0 = Conv2D(32, kernel_size=(3, 3), strides=(1, 1), activation='relu') 
(input_shape)
    m0 = MaxPooling2D(pool_size=(2, 2), strides=(2, 2)) (c0)
    f = Flatten()(m0)
    d1 = Dense(4024, activation='relu')(f)
    d2 = Dense(512, activation='sigmoid')(d1)

    anchor = Input(shape=(128, 254, 3))
    positive = Input(shape=(128, 254, 3))
    negative = Input(shape=(128, 254, 3))

    reid_model = Model(inputs=[input_shape], outputs=[d2])

    anchor_embed = reid_model(anchor)
    positive_embed = reid_model(positive)
    negative_embed = reid_model(negative)

    merged_output = concatenate([anchor_embed, positive_embed, 
negative_embed])
    loss = Lambda(triplet_loss, (1,))(merged_output)

    model = Model(inputs=[anchor, positive, negative], outputs=loss)
    model.compile(optimizer='Adam', loss='mse',
                  metrics=["mae"])
    return model

关于python - Keras 合并 VS 连接,无法更新我的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52712301/

相关文章:

python - 将数据框字典写入excel文件会产生错误

python - 证书验证失败 : self signed certificate in certificate chain (_ssl. c:1129)

python - 将数组添加到Python中的数据框

python - 我该如何阻止它消失?

python - 创建形状为 (None, 1) 的有序整数的张量

javascript - 在 HTML 按钮上单击运行 python 文件 (Django)

python - 详细变换的逆

python - 为什么我不断收到位置参数错误?

machine-learning - TensorFlow 中 tf.estimator.Estimator 和 tf.contrib.learn.Estimator 有什么区别

python - 我可以获得指向 tensorflow 中张量部分的指针吗?