Python Keras合并与连接,可以';不要更新我的代码
我有一个CNN的Keras功能模型。我正在尝试实现一个三重态丢失函数。我找到了一些关于谁使用“合并”来做这件事的帖子,现在不推荐使用“合并”,但我不能像使用“合并”那样使用“连接” 原始代码如下所示:Python Keras合并与连接,可以';不要更新我的代码,python,python-3.x,tensorflow,keras,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.r
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\u embed,positive\u embed,negative\u embed],mode=triplet\u loss,output\u shape=(1,)
将函数triplet\u loss
的输出转换为keras层输出(如中所建议)。函数串联
没有参数“mode”。有没有办法调整我的代码以获得损失函数作为Keras层输出的结果?我终于找到了一种方法来计算三重损失函数的值,通过添加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
调整代码的一种直接方法是:合并输出=连接([anchor\u embed,positive\u embed,negative\u embed])模型=模型(输入=[anchor,positive,negative],输出=合并输出)模型。通过这种方式编译(optimizer='Adam',loss=triplet\u loss,metrics=[triplet\u loss]),如果您想检查并恢复Keras模型,您可能必须这样做,例如:model=load_model('/path/to/model',custom_objects={'triplet_loss':triplet_loss}),让我知道这是否有效!嘿@kvish,我试过了,唯一的问题是“concatenate”函数返回一个张量(“concatenate_1/concat:0”,shape=(?,1536),dtype=float32)
对象,因此,当调用triplet_loss
函数时,我有一个与输入数量相关的错误。您是否知道如何“断开”层以计算损耗。谢谢您是否尝试使用获取相应的张量?@kvish此解决方案的问题是,我仍然需要输出为:model.compile(optimizer='Adam',loss='mean\u absolute\u error',metrics=['accurity'])
如果我更改为model.compile(optimizer='Adam',loss=triplet\u loss,metrics=[triplet\u loss])
如何将损耗设置为“平均绝对误差”?如果您使用的是三重态损耗,那么这就是您正在定义的实际损耗函数,需要将其最小化以评估您的问题。有关三重态损耗的更多详细信息,请参阅本手册。