技术指南

KL散度

Kullback-Leibler (KL) 散度衡量使用一种概率分布代表另一种概率分布的预期额外信息成本。

听听这个指南AI叙述 · ~3 min

在本页3 分钟阅读
  1. 概述
  2. 深入探讨
  3. 现实世界的实施
  4. KL 散度的未来
  5. 付诸实践
  6. 不断探索
  7. 常见问题

概述

它是非负的但不对称,交换参考分布和比较分布通常会改变结果,并可能改变哪种建模选择更优。

深入探讨

For discrete distributions p and q over the same outcomes, KL divergence is D_KL(p||q) = sum over outcomes p(x) log(p(x)/q(x)). It compares q with a reference p, weighting each log ratio by how often the outcome occurs under p. In information theory, it can be interpreted as expected extra coding cost when a code is optimized for q but the true distribution is p. The log base sets the unit: natural logarithms yield nats and base two yields bits. KL divergence is nonnegative and equals zero when the distributions match on outcomes with positive probability under p, subject to support considerations. It is asymmetric: D_KL(p||q) is generally not D_KL(q||p). This makes it unsuitable as a conventional geometric distance. The direction matters in applications such as variational inference, where one distribution is treated as a target and another as an approximation. In the fair-versus-biased coin example, p=(0.5,0.5) and q=(0.9,0.1). The bit-valued contributions are 0.5 log2(0.5/0.9), about -0.424, and 0.5 log2(0.5/0.1), about 1.161. Their sum is about 0.737 bits. Negative individual terms are allowed even though the total divergence is nonnegative. Reversing p and q produces a different value because the weights also change. Support mismatches matter. If p assigns positive probability to an outcome where q assigns zero, the corresponding forward divergence is infinite. If both assign zero, that outcome contributes zero by convention in the discrete sum. For continuous distributions, KL is written as an integral of the log density ratio under P; finiteness requires P to be absolutely continuous with respect to Q. SciPy's entropy function computes Shannon entropy with one distribution and relative entropy when q is provided; check input normalization and direction. KL is useful for comparing distributions, but it does not by itself say whether a model is useful for a downstream decision.

现实世界的实施

一个公平硬币的分布 p=(0.5,0.5) 与 q=(0.9,0.1) 进行比较。以比特为单位,D_KL(p||q)=0.5 log2(0.5/0.9)+0.5 log2(0.5/0.1),约为 0.737 比特。

语言模型的训练目标是最小化目标令牌分布到其预测分布的 KL。如果目标事件的预测概率为零,前向 KL 代价会趋于无穷,需要仔细平滑或模型支持。

研究者比较 D_KL(p||q) 与 D_KL(q||p) 时得到不同数值。该不对称性意味着该度量不是普通距离,也不满足一般的三角不等式。

分析师使用 scipy.stats.entropy(pk, qk) 并确认哪个参数代表目标分布,因为该 API 的相对熵方向是相对于 pk 而不是 qk。

KL 散度的未来

当报告说明参考分布、对数底数以及零概率事件的处理方式时,分布比较会更易解释。在模型训练中,团队应将 KL 方向与期望行为对应:覆盖所有目标模式不同于将近似集中在主导模式附近。应评估下游决策而不仅是散度值,因为低分布不匹配本身并不保证实际效用。未来的流水线可以加入支持检查和对平滑的敏感性,使数值边缘情况可见,而不是悄悄改变概率。

付诸实践

为什么这很重要

  • 成本与预算. 多年来,架构决策决定着性能和运营成本。
  • 更清晰的判决. 技术教育帮助团队选择正确的堆栈,而不仅仅是最新的堆栈。
  • 质量控制. 更好的工程选择可以减少生产中的可靠性事故。

留意

  • 优化一项基准测试可以隐藏更广泛的系统弱点。
  • 基础设施和维护成本常常被低估。
  • 随着系统变得更加复杂,安全性和可观察性差距可能会扩大。

后续步骤

  1. 在实施之前定义延迟、质量和成本目标。
  2. 在实际负载和数据条件下进行基准测试。
  3. 仪器监控错误、漂移和用户影响。
  4. 在扩展之前准备回滚和事件响应路径。

不断探索

免费通讯

获取每日AI简报

每个工作日早晨提供三条经过验证的AI故事,使用简明英文撰写。永久免费,无广告。

每个工作日发送一封邮件。一键退订。我们永不出售或分享您的地址。

测试一下自己

参加 KL 散度测验

每个答案即时反馈,课程通过后可获得可验证ID的可分享证书。

开始测验

支持免费AI教育。AI Understanding是一家501(c)(3)非营利组织——无广告,无付费墙,永不收费。 捐款

常见问题

什么是 KL 散度?

Kullback‑Leibler(KL)散度衡量使用一个概率分布来表示另一个分布时所产生的额外信息成本。它是非负的但不对称,交换参考分布和比较分布通常会改变结果,并可能改变哪种建模选择更优。

在 D_KL(p||q) 中,哪个分布为对数比率项加权?

求和由 p(x) 加权,即该方向的参考分布。

为什么 KL 散度不是普通的对称距离?

当交换分布时,加权方式和比率会改变。

当 p(x)>0 且 q(x)=0 时,对应的前向 KL 贡献会怎样?

对数比率的分子为正而分母为零,导致散度无穷大。

在公平硬币与偏置硬币的例子中,为什么某一项可以为负而整体 KL 仍保持非负?

某些 q 的概率大于 p,导致负的对数项,但整体散度仍满足非负性。

使用以二为底的对数得到的单位是什么?

以二为底的对数测量信息量为比特;自然对数产生纳特。