Keras/TensorFlow 交叉熵损失函数中的 from_logits 参数

文章目录

在 Keras 的二元交叉熵损失函数(tf.keras.losses.BinaryCrossentropy,下面简称为 bce)中,有 from_logits 这个参数。在使用时需要分下面 2 种情况:

  1. 如果输入给 bce 的是一个 logit 值(值域范围 [-∞, +∞] ),则应该设置 from_logits=True 。
  2. 如果输入给 bce 的是一个概率值 probability (值域范围 [0, 1] ),则应该设置为 from_logits=False 。

这两种方法的比较如下图所示。

[En]

The comparison of the two methods is shown in the following figure.

Keras/TensorFlow 交叉熵损失函数中的 from_logits 参数
从上面的对比图可以看出,from_logits=True 是一个简便操作,它让使用者省去了转换概率这个步骤。

; 2. 交叉熵的底层操作。

在计算交叉熵时,实际上经历了两个步骤:1.将数值转换为概率值。二.取这个概率值的对数,得到交叉熵。如下图所示。

[En]

When calculating the cross entropy, it actually goes through two steps: 1. Convert a numerical value to a probability value. two。 Take the logarithm of this probability value and get the cross entropy. As shown in the following picture.

Keras/TensorFlow 交叉熵损失函数中的 from_logits 参数
  1. logit 与 sigmoid 互为反函数。

实际上,logit 是一个函数,并且它和 sigmoid 互为反函数。sigmoid 会把 logit 值转换为 [0, 1] 之间的概率值,而 logit 则会把概率值转换为 [-∞, +∞] 之间的 logit 值。
在 Scipy 中,sigmoid 函数也叫做 expit 函数。下图是 sigmoid 和 logit 函数的对比。关于 sigmoid 函数的更多内容,详见 Scipy 链接 → Scipy 的 sigmoid 函数页面链接

Keras/TensorFlow 交叉熵损失函数中的 from_logits 参数
最后,Keras 中的多类别交叉熵损失函数 Categorical Crossentropy,也有 from_logits 这个参数,其使用方法和二元交叉熵损失函数相同。不过因为它处理的是多分类问题,在计算概率时,要用 softmax 函数。

Original: https://blog.csdn.net/drin201312/article/details/123247057
Author: 杭州的平湖秋月
Title: Keras/TensorFlow 交叉熵损失函数中的 from_logits 参数

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/497209/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球