代码之家  ›  专栏  ›  技术社区  ›  abeagomez

Keras merge VS concatenate,无法更新我的代码

  •  1
  • abeagomez  · 技术社区  · 7 年前

    我有一个CNN的Keras功能模型。我想实现一个三重态损耗函数。我发现了一些关于谁使用“merge”来做这件事的帖子,现在已经不推荐了,但是我不能像使用merge那样使用“concatenate”。

    原始代码如下所示:

    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 https://codepad.co/snippet/F1uVDD5N concatenate

    1 回复  |  直到 7 年前
        1
  •  2
  •   abeagomez    7 年前

    我终于找到了计算 triplet_loss

    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