gpt4 book ai didi

python - Keras:binary_crossentropy 和 categorical_crossentropy 混淆

转载 作者:太空狗 更新时间:2023-10-29 21:45:12 26 4
gpt4 key购买 nike

在使用 TensorFlow 一段时间后,我阅读了一些 Keras 教程并实现了一些示例。我找到了几个使用 keras.losses.binary_crossentropy 作为损失函数的卷积自动编码器的教程。

我认为 binary_crossentropy 应该是一个多类损失函数并且很可能会使用二进制标签,但实际上 Keras(TF Python 后端)调用 tf.nn.sigmoid_cross_entropy_with_logits,它实际上用于具有互斥的多个独立类的分类任务。

另一方面,我对 categorical_crossentropy 的期望是用于多类分类,其中目标类相互依赖,但不一定是一个-热编码。

但是,Keras 文档指出:

(...) when using the categorical_crossentropy loss, your targets should be in categorical format (e.g. if you have 10 classes, the target for each sample should be a 10-dimensional vector that is all-zeros expect for a 1 at the index corresponding to the class of the sample).

如果我没记错的话,这只是单热编码分类任务的特例,但潜在的交叉熵损失也适用于概率分布(“多类”、依赖标签)?

此外,Keras 使用 tf.nn.softmax_cross_entropy_with_logits(TF python 后端)来实现,它本身 states :

NOTE: While the classes are mutually exclusive, their probabilities need not be. All that is required is that each row of labels is a valid probability distribution. If they are not, the computation of the gradient will be incorrect.

如果我错了请纠正我,但在我看来 Keras 文档 - 至少 - 不是很“详细”?!

那么,Keras 对损失函数的命名背后的想法是什么?文档是否正确?如果二元交叉熵真的依赖于二元标签,它应该适用于自动编码器,对吧?!同样,分类交叉熵:如果文档正确,应该只适用于单热编码标签?!

最佳答案

您通过定义这些损失中的每一个适用的区域是正确的:

  • binary_crossentropy (和引擎盖下的 tf.nn.sigmoid_cross_entropy_with_logits)用于二进制多标签分类(标签是独立的)。
  • categorical_crossentropy (和引擎盖下的 tf.nn.softmax_cross_entropy_with_logits)用于多类分类(类是独占的)。

另请参阅 this question 中的详分割析.

我不确定你指的是什么教程,所以无法评论 binary_crossentropy 是自编码器的好选择还是坏选择。

至于命名,绝对正确合理。还是您认为 sigmoidsoftmax 名称听起来更好?

因此,您的问题中唯一令人困惑的是 categorical_crossentropy 文档。请注意,所陈述的一切都是正确的:损失支持单热表示。在 tensorflow 后端的情况下,此函数确实适用于标签的任何概率分布(除了单热向量)并且它可以包含在文档中,但这并不看起来对我很挑剔。此外,需要检查其他后端是否支持soft classes,theano和CNTK。请记住,keras 力图做到极简主义并以最流行的用例为目标,因此我可以理解这里的逻辑。

关于python - Keras:binary_crossentropy 和 categorical_crossentropy 混淆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47877083/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com