技術指南

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 的相對熵方向遵循相對於 qk 的 pk。

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,給出負對數項,而完全散度服從非負性。

使用以 2 為底的對數會產生什麼單位?

以位為基礎的兩個度量資訊;天然原木產生自然原木。