【发布时间】:2023-04-03 16:10:01
【问题描述】:
from fr_utils import *
from inception_blocks_v2 import *
def triplet_loss(y_true, y_pred, alpha=0.3):
"""
Implementation of the triplet loss as defined by formula (3)
Arguments:
y_pred -- python list containing three objects:
anchor -- the encodings for the anchor images, of shape (None, 128)
positive -- the encodings for the positive images, of shape (None, 128)
negative -- the encodings for the negative images, of shape (None, 128)
Returns:
loss -- real number, value of the loss
"""
anchor, positive, negative = y_pred[0], y_pred[1], y_pred[2]
# Step 1: Compute the (encoding) distance between the anchor and the positive, you will need to sum over axis=-1
pos_dist = tf.reduce_sum(tf.square(tf.subtract(anchor, positive)), axis=-1)
# Step 2: Compute the (encoding) distance between the anchor and the negative, you will need to sum over axis=-1
neg_dist = tf.reduce_sum(tf.square(tf.subtract(anchor, negative)), axis=-1)
# Step 3: subtract the two previous distances and add alpha.
basic_loss = tf.add(tf.subtract(pos_dist, neg_dist), alpha)
# Step 4: Take the maximum of basic_loss and 0.0. Sum over the training examples.
loss = tf.reduce_sum(tf.maximum(basic_loss, 0.0))
return loss
def main():
FRmodel = faceRecoModel(input_shape=(3, 96, 96))
FRmodel.compile(optimizer='adam', loss=triplet_loss, metrics=['accuracy'])
FRmodel.save('face-rec_Google.h5')
print_summary(model)
main()
这段代码显示的错误如下
得到输入形状:%s' % (input_shape))
ValueError:Concatenate 层需要具有匹配形状的输入,但 concat 轴除外。得到输入形状:[(None, 128, 12, 192), (None, 32, 12, 192), (None, 32, 12, 102), (None, 64, 12, 192)]
我尝试在互联网上查找错误但没有找到解决方案
【问题讨论】:
-
这意味着你试图相互连接的张量必须具有相同的形状,除了最后一个轴。例如,第一个张量的形状为
(None, 128, 12, 192),第二个张量的形状为(None, 32, 12, 192)。所以这两个张量中的第二个轴不相等:128 != 32。所有张量的第三个轴都是 12,所以这很好。但是它们中的第二个轴也必须相等,但它们是 128、32、32、64。 -
新错误:二维输入的缩减维度 3 无效。对于 'lambda_2/l2_normalize/Sum' (op: 'Sum'),输入形状:[?,128], [] 和计算输入张量:input[1] = .
-
如果没有看到完整的模型代码,就很难判断出了什么问题。此外,这似乎是另一个问题,这意味着您需要提出一个新问题。