【问题标题】:ValueError: Unknown loss function:triplet_lossValueError:未知损失函数:triplet_loss
【发布时间】:2019-08-30 02:31:34
【问题描述】:

尝试将模型转换为 tflite 或 .pb 文件时出现以下错误:

ValueError:未知损失函数:triplet_loss

我检查了 StackOverflow 和 GitHub 中发布的不同解决方案,但没有一个适用于我的代码。

#calculates triplet loss
def triplet_loss(y_true, y_pred, alpha = 0.2):

    anchor, positive, negative = y_pred[0], y_pred[1], y_pred[2]

    # triplet loss formula 
    pos_dist = tf.reduce_sum( tf.square(tf.subtract(anchor, positive)) )
    neg_dist = tf.reduce_sum( tf.square(tf.subtract(anchor, negative)) )
    basic_loss = pos_dist - neg_dist + alpha
    loss = tf.maximum(basic_loss, 0.0)
    return loss

# load the model    
model = load_model('facenet_model/model.h5', custom_objects={'triplet_loss': triplet_loss})

我相信我必须对三元组损失函数进行一些更改以解决值错误。

【问题讨论】:

    标签: python-3.x tensorflow


    【解决方案1】:

    前两步使用 tf.reduce_sum() 的轴参数等于 -1。这可能会解决您的问题。

    pos_dist = tf.reduce_sum(tf.square(tf.subtract(anchor,positive)),axis=-1)
    neg_dist = tf.reduce_sum(tf.square(tf.subtract(anchor,negative)),axis=-1)
    basic_loss = pos_dist - neg_dist + alpha
    loss = tf.reduce_sum(tf.maximum(basic_loss,0.0))
    

    【讨论】:

      猜你喜欢
      • 2020-01-18
      • 2019-03-10
      • 1970-01-01
      • 2020-12-24
      • 2022-06-13
      • 2019-09-10
      • 2020-02-18
      • 2021-02-26
      • 2022-11-24
      相关资源
      最近更新 更多