【问题标题】:No gradients provided for any variable for custom segmentation loss function没有为自定义分段损失函数的任何变量提供梯度
【发布时间】:2019-05-09 01:09:50
【问题描述】:

我创建了一个分段函数。 y是mnist标签,y_是softmax预测结果,pen_lesspen_more是两个惩罚参数。

loss = tf.reduce_sum(tf.where(
    tf.greater(tf.to_float(tf.argmax(y, 1)), tf.to_float(tf.argmax(y_, 1))),
    tf.pow(pen_less, tf.to_float(tf.argmax(y, 1)) - tf.to_float(tf.argmax(y_, 1))),
    tf.pow(pen_more, tf.to_float(tf.argmax(y, 1)) - tf.to_float(tf.argmax(y_, 1)))))

【问题讨论】:

    标签: tensorflow machine-learning loss-function gradient


    【解决方案1】:

    编辑:如果您传递所有三个参数,tf.where 是可区分的。我假设您的问题在于 argmaxes:

    import tensorflow as tf
    
    x = tf.Variable([0, 1, 2])
    tf.gradients(tf.argmax(x), x)
    

    输出:

    LookupError: 没有为操作“ArgMax”定义梯度(操作类型:ArgMax)

    如果你想要一个可微的损失函数,你需要避免 argmax 操作或者找到一个聪明的方法来得到一个合适的伪梯度。

    【讨论】:

    • 我之前也以为是这个问题,但是有些函数比如:loss = tf.reduce_sum(tf.where(tf.greater(y, y_), (y - y_)*loss_more, (y_-y)*loss_less)) 可以工作。我不知道为什么。
    • 编辑了我的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-08
    • 1970-01-01
    • 2020-04-05
    • 2021-01-08
    • 2022-12-04
    • 1970-01-01
    相关资源
    最近更新 更多