简体   繁体   English

Keras中的自定义损失输出

[英]Output of custom loss in Keras

I know there are many questions treating custom loss functions in Keras but I've been unable to answer this even after 3 hours of googling. 我知道在Keras中处理自定义损失函数有很多问题,但是即使经过3个小时的搜寻,我也无法回答。

Here is a very simplified example of my problem. 这是我的问题的非常简化的示例。 I realize this example is pointless but I provide it for simplicity, I obviously need to implement something more complicated. 我意识到这个例子是没有意义的,但是为了简单起见,我提供了它,显然我需要实现更复杂的东西。

from keras.backend import binary_crossentropy
from keras.backend import mean
def custom_loss(y_true, y_pred):

    zeros = tf.zeros_like(y_true)
    index_of_zeros = tf.where(tf.equal(zeros, y_true))
    ones = tf.ones_like(y_true)
    index_of_ones = tf.where(tf.equal(ones, y_true))

    zero = tf.gather(y_pred, index_of_zeros)
    one = tf.gather(y_pred, index_of_ones)

    loss_0 = binary_crossentropy(tf.zeros_like(zero), zero)
    loss_1 = binary_crossentropy(tf.ones_like(one), one)

    return mean(tf.concat([loss_0, loss_1], axis=0))

I do not understand why training the network with the above loss function on a two class dataset does not yield the same result as training with the built in binary-crossentropy loss function. 我不明白为什么在两类数据集上使用上述损失函数训练网络不会产生与使用内置binary-crossentropy损失函数进行训练的结果相同的结果。 Thank you! 谢谢!

EDIT : I edited the code snippet to include the mean as per comments below. 编辑 :我编辑了代码片段,以包括以下每个注释的平均值。 I still get the same behavior however. 我仍然得到相同的行为。

I finally figured it out. 我终于弄明白了。 The tf.where function behaves very differently when the shape is "unknown". 当形状为“未知”时, tf.where函数的行为会非常不同。 To fix the snippet above simply insert the following lines right after the function is declared: 要修复上面的代码段,只需在函数声明后立即插入以下几行:

y_pred = tf.reshape(y_pred, [-1])
y_true = tf.reshape(y_true, [-1])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM