【问题标题】:How can I implement marginal loss?如何实现边际损失?
【发布时间】:2018-07-17 09:10:01
【问题描述】:

我正在尝试实现论文 [1] 中介绍的边际损失。 到目前为止,这就是我所做的。

def marginal_loss(model1, model2, y, margin, threshold):
    margin_ = 1/(tf.pow(margin,2)-margin)
    tmp = (1. - y)
    euc_dist = tf.sqrt(tf.reduce_sum(tf.pow(model1-model2, 2), 1, keep_dims=True))
    thres_dist = threshold - euc_dist
    mul_val = tf.multiply(tmp, thres_dist)
    sum_ = tf.reduce_sum(mul_val)
    return tf.multiply(margin_, sum_)

但是,经过一些时期后,该值变为 nan。我不确定我犯了什么错误。此外,我使用 1 代替了 epsilon(在论文中描述),因为它的值不清楚。同样,确切的阈值也是未知的。

感谢您的帮助。

[1]https://ibug.doc.ic.ac.uk/media/uploads/documents/deng_marginal_loss_for_cvpr_2017_paper.pdf

【问题讨论】:

    标签: tensorflow deep-learning loss-function


    【解决方案1】:

    这看起来与this other question 提出的问题非常相似。问题可能来自tf.sqrt 的使用,它的坏特性是当您接近零时梯度会趋于无穷大,从而在模型收敛时给您带来不稳定性。

    尝试消除损失中的tf.sqrt,例如通过最小化当前损失的平方。

    或者,您可以依赖现有的内置函数,例如 tf.losses.hinge_loss(但不适用于多维输出)。

    【讨论】:

    • 没有 tf.sqrt,损失在 -ve 中。主要问题不是nan,可能是由于实施错误。它是执行。你能看看我的实现是否正确吗?
    • 这些负值可能是由于阈值;我不确定确切的阈值是多少。我将其设置为 1(导致负值),当我将其更改为 0(这是正常的)时,两种情况下的损失都会减少。但我不确定实施情况。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-06-28
    • 2020-08-06
    • 2018-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-20
    相关资源
    最近更新 更多