27 / 90

第 27 天:資訊理論:熵、交叉熵與語言模型損失

以位元和奈特度量驚異度(surprisal),手算熵與交叉熵,並把交叉熵與下一詞元負對數概似及困惑度聯繫起來,同時給出數值穩定性防護。

數學與演算法基礎 480 分鐘(8 小時專注學習)
01 120 分鐘

對數、驚異度與資訊單位

僅在記錄預測與觀察證據後才允許輔助。

前置要求

完成第 26 天(隨機變數與採樣);如遇阻塞,使用所列恢復連結(第 21 天)。

學習目標

把 $-\log p$ 解釋為結果的驚異度(資訊量),在位元($\log_2$)與奈特($\ln$)之間換算,並安全處理 $\log(0)$。

操作指引

機率為 $p$ 的結果的香農資訊量是 $-\log_2 p$ 位元(或 $-\ln p$ 奈特);1 位元 $= \ln 2 \approx 0.6931$ 奈特。越稀有的結果資訊量越大。就極限而言,$p \to 0^+$ 時 $\log p \to -\infty$(擴充實數約定寫作 $\log(0) = -\infty$)——這是極限約定而非 API 保證:本環境的 Python 標準函式庫中 math.log(0) 與 math.log1p(-1) 都會拋出 ValueError,其他數值函式庫的行為可能不同。因此實作會把機率裁剪到 $p \leftarrow \max(p,\varepsilon)$,或僅對接近 1 的引數使用 math.log1p(x) = ln(1+x),以 log1p(p-1) 精確計算 ln p;p=0 仍需單獨策略(裁剪到正下限、平滑化,或明確接受無窮損失)——只提 log1p 並不構成零機率防護。

自主實作與記錄

為 $p\in\{0.5,0.25,0.125,0.8,0.1\}$ 製作位元和奈特的驚異度表。然後把 2 位元換算為奈特,並解釋為何 $-\ln(0.1)\approx2.3026$ 約為 $-\ln(0.8)\approx0.2231$ 的十倍。

獨立掌握標準

表格在兩種單位下都與已驗證數值一致,並能說出 $\log(0)$ 失效與一條具體防護(裁剪/正下限,或僅限 p 接近 1 時的 log1p)加一條明示的 p=0 策略(下限、平滑化或聲明的無窮損失)。

權威來源資源與權利證明

linking Cambridge University Press copyright; free online reading authorized No text extraction or redistribution permitted

Information Theory, Inference, and Learning Algorithms

作者 / 機構: David J. C. MacKay | 原始出處連結 ↗

對應章節 / 時間戳: Chapter 2: Probability, Entropy, and Inference (Shannon information content)

驗證版本與日期: Cambridge University Press 2003, authorized free PDF (Verified: 2026-10-30)

入選理由:

Freely available textbook introducing surprisal -log p with worked numerical exercises.

來源前置條件:

Probability foundations (Day 25-26)

安全回退方案(離線彈性):

All required definitions, tables, and worked answers are reproduced locally in this lesson.

對比候選資源與選型分析 (3)
Cover & Thomas, Elements of Information Theory ↗ compared_alternative [Wiley copyright]

The standard rigorous reference; paywalled, so it is cited alongside MacKay only via linking rather than as the sole learner-facing text.

James V. Stone, Information Theory: A Tutorial Introduction ↗ compared_alternative [Open Access / CC BY-NC-SA 4.0]

Gentler introduction freely readable on arXiv; less depth on coding and inference connections needed for the language-model-loss bridge.

Shannon, A Mathematical Theory of Communication (1948) ↗ compared_alternative [Bell System Technical Journal copyright]

Historical primary source; valuable citation but dated notation makes it supplementary rather than primary for learners.

linking PSF License Version 2

Python 3 math: Mathematical functions

作者 / 機構: Python Software Foundation | 原始出處連結 ↗

對應章節 / 時間戳: math.log, math.log2, math.log1p (log bases and near-zero precision)

驗證版本與日期: Python 3.12 documentation (Verified: 2026-10-30)

入選理由:

Normative reference for the log-base and log1p calls used in the surprisal table.

來源前置條件:

Basic Python syntax

安全回退方案(離線彈性):

Use the installed interpreter's help(math) and the included deterministic commands.

對比候選資源與選型分析 (2)
NumPy log and ufunc documentation ↗ compared_deferred [BSD 3-Clause]

Vectorized logs arrive with the tensor track; scalar math functions keep Day 27 dependency-free.

decimal — Decimal fixed point and floating point arithmetic ↗ compared_alternative [PSF License Version 2]

Decimal-precision reference rather than binary float math; math remains the correct module for the operators and constants these days use.

02 120 分鐘

分佈的熵

獨立掌握檢查(無 AI 輔助)。

前置要求

完成上一項活動;如遇阻塞,使用所列恢復連結。

學習目標

計算香農熵 $H(X)=-\sum_x p(x)\log_2 p(x)$,並把它解釋為期望驚異度,即理想的平均編碼長度。

操作指引

熵只依賴機率值,不依賴標籤或結果的數值。必做:獨立算出下面兩個熵並加以比較,註明單位。

自主實作與記錄

(a) 求 $p=[0.5,0.25,0.125,0.125]$ 的 $H(X)$。(b) 求公平六面骰的 $H$。(c) 哪個分佈更不確定?熵依賴結果取值 $\{0,1,2,3\}$ 還是只依賴 $p$?

獨立掌握標準

兩個熵都以正確單位獨立算出,並陳述熵是僅由機率向量決定的性質。

權威來源資源與權利證明

linking Cambridge University Press copyright; free online reading authorized No text extraction or redistribution permitted

Information Theory, Inference, and Learning Algorithms

作者 / 機構: David J. C. MacKay | 原始出處連結 ↗

對應章節 / 時間戳: Chapter 2: Shannon information content and entropy; Chapter 4 (source coding theorem)

驗證版本與日期: Cambridge University Press 2003, authorized free PDF (Verified: 2026-10-30)

入選理由:

Authorized-free textbook derivations of entropy as expected surprisal and ideal code length.

來源前置條件:

Probability foundations (Day 25-26)

安全回退方案(離線彈性):

All required definitions, tables, and worked answers are reproduced locally in this lesson.

對比候選資源與選型分析 (3)
Cover & Thomas, Elements of Information Theory ↗ compared_alternative [Wiley copyright]

The standard rigorous reference; paywalled, so it is cited alongside MacKay only via linking rather than as the sole learner-facing text.

James V. Stone, Information Theory: A Tutorial Introduction ↗ compared_alternative [Open Access / CC BY-NC-SA 4.0]

Gentler introduction freely readable on arXiv; less depth on coding and inference connections needed for the language-model-loss bridge.

Shannon, A Mathematical Theory of Communication (1948) ↗ compared_alternative [Bell System Technical Journal copyright]

Historical primary source; valuable citation but dated notation makes it supplementary rather than primary for learners.

linking Wiley copyright / Academic linking only No text extraction or distribution permitted

Elements of Information Theory

作者 / 機構: Thomas M. Cover, Joy A. Thomas | 原始出處連結 ↗

對應章節 / 時間戳: Chapter 2: Entropy, Relative Entropy, and Mutual Information

驗證版本與日期: 2nd Edition, Wiley 2006 (Verified: 2026-10-30)

入選理由:

Detailed mathematical reference for entropy properties cited alongside the free MacKay text.

來源前置條件:

Probability foundations and logarithms

安全回退方案(離線彈性):

Self-contained derivations in activity instructions and offline guide.

對比候選資源與選型分析 (2)
David MacKay, Information Theory, Inference, and Learning Algorithms ↗ compared_alternative [Cambridge University Press copyright; free online reading authorized]

Co-selected: equally rigorous and freely readable online, so it carries the primary learner-facing role.

Dive into Deep Learning: Information Theory appendix ↗ compared_alternative [CC BY-SA 4.0 (book text) / modified MIT (sample code)]

Code-integrated deep-learning framing; useful supplementary reading but less mathematically complete for entropy proofs.

03 120 分鐘

交叉熵、KL 散度與語言模型損失

僅在記錄預測與觀察證據後才允許輔助。

前置要求

完成上一項活動;如遇阻塞,使用所列恢復連結。

學習目標

計算交叉熵 $H(p,q)=-\sum_x p(x)\log_2 q(x)$ 與 KL 散度 $D_{KL}(p\|q)=H(p,q)-H(p)$;把它們接到下一詞元 NLL 損失與困惑度上,包括零機率失效與 KL 非對稱性。

操作指引

真分佈為 $p$、模型為 $q$ 時,期望編碼代價是 $H(p,q)$;超出 $H(p)$ 的部分是 $D_{KL}(p\|q)\ge0$。語言模型訓練最小化的正是它:每個被觀察詞元的損失為 $-\ln q(\text{詞元})$,困惑度為 $e^{\text{平均 NLL}}$。執行程式前先手算下列各量。

自主實作與記錄

取 $p=[0.5,0.25,0.125,0.125]$:(a) 對均勻 $q=[0.25,0.25,0.25,0.25]$ 求 $H(p,q)$ 與 $D_{KL}(p\|q)$;(b) 對 $q=[0.4,0.3,0.15,0.15]$ 求 $D_{KL}(p\|q)$ 與 $D_{KL}(q\|p)$——相等嗎?(c) 語言模型給接下來三個真實詞元賦 $q=0.8, 0.5, 0.25$:求平均 NLL 與困惑度。(d) 若模型給某個被觀察詞元賦 $q=0$,語料損失會怎樣?

獨立掌握標準

手算交叉熵與兩個方向的 KL;解釋逐詞元 $-\ln q$ 即 NLL;把困惑度寫成均值的指數;說出零機率失效與一條防護措施。

權威來源資源與權利證明

linking Wiley copyright / Academic linking only No text extraction or distribution permitted

Elements of Information Theory

作者 / 機構: Thomas M. Cover, Joy A. Thomas | 原始出處連結 ↗

對應章節 / 時間戳: Chapter 2: relative entropy (KL divergence) and its non-negativity

驗證版本與日期: 2nd Edition, Wiley 2006 (Verified: 2026-10-30)

入選理由:

Standard reference for D_KL(p||q) definitions and asymmetry used in this activity.

來源前置條件:

Probability foundations and logarithms

安全回退方案(離線彈性):

Self-contained derivations in activity instructions and offline guide.

對比候選資源與選型分析 (2)
David MacKay, Information Theory, Inference, and Learning Algorithms ↗ compared_alternative [Cambridge University Press copyright; free online reading authorized]

Co-selected: equally rigorous and freely readable online, so it carries the primary learner-facing role.

Dive into Deep Learning: Information Theory appendix ↗ compared_alternative [CC BY-SA 4.0 (book text) / modified MIT (sample code)]

Code-integrated deep-learning framing; useful supplementary reading but less mathematically complete for entropy proofs.

linking Cambridge University Press copyright; free online reading authorized No text extraction or redistribution permitted

Information Theory, Inference, and Learning Algorithms

作者 / 機構: David J. C. MacKay | 原始出處連結 ↗

對應章節 / 時間戳: Chapters 2 and 5: cross-entropy ideas, symbol codes, and noisy-channel models

驗證版本與日期: Cambridge University Press 2003, authorized free PDF (Verified: 2026-10-30)

入選理由:

Authorized-free bridge from coding theory to probabilistic models used for the LM-loss connection.

來源前置條件:

Probability foundations (Day 25-26)

安全回退方案(離線彈性):

All required definitions, derivations, and worked answers are reproduced locally in this lesson.

對比候選資源與選型分析 (3)
Cover & Thomas, Elements of Information Theory ↗ compared_alternative [Wiley copyright]

The standard rigorous reference; paywalled, so it is cited alongside MacKay only via linking rather than as the sole learner-facing text.

James V. Stone, Information Theory: A Tutorial Introduction ↗ compared_alternative [Open Access / CC BY-NC-SA 4.0]

Gentler introduction freely readable on arXiv; less depth on coding and inference connections needed for the language-model-loss bridge.

Shannon, A Mathematical Theory of Communication (1948) ↗ compared_alternative [Bell System Technical Journal copyright]

Historical primary source; valuable citation but dated notation makes it supplementary rather than primary for learners.

04 120 分鐘

獨立掌握檢查:資訊理論損失

獨立掌握檢查(無 AI 輔助)。

前置要求

完成上一項活動;如遇阻塞,使用所列恢復連結。

學習目標

在新分佈上獨立計算熵、交叉熵、KL、NLL 與困惑度,並說明數值防護措施。

操作指引

本檢查期間不使用 AI 與參考資料。先記錄預測,算完全部四題,再複盤並對錯題走恢復連結。 保留首次無協助嘗試且不做改動;此後的錯題複盤和任何協助式補救是單獨階段,遺漏的概念不能僅靠複盤或重做算作補救;必須提供一個答案未事先展示、真正全新的同等無協助檢查並通過。這裡提供的第二次檢查是補救後的自我檢查,不是新的未見證據。 第二次檢查(僅在補救之後;無協助、無提示): 開始前先關閉答案/參考內容;本次作答期間不要閱讀或執行檢查器。獨立完成且不看提示。此前補救時複習已學公式屬於概念複習,並不是展示第二次檢查的數值答案。(a) 求 $p=[0.55,0.20,0.15,0.10]$ 的 $H(p)$。(b) 對四個類型上的均勻 $q$ 求 $H(p,q)$ 與 $D_{KL}(p\|q)$。(c) 對得分 $q=[0.25,0.5,0.2]$ 的詞元求逐詞元 NLL、平均 NLL 與困惑度。(d) 若再有一個被觀察詞元得分 $q=0$,平均 NLL 會怎樣,哪種防護能避免? 完成本次作答後,先原樣保存第二次無協助提交,再開啟或執行檢查器。其數值回饋作為補救後的自我檢查,僅直接用於 (a)、(b)、(c),並且只有在該次作答期間既未閱讀也未執行檢查器、且先保存提交時才可採用;只回報各部分通過/未通過。(d)必須在作答後,依照答案欄中的獨立事後評分標準進行人工評閱。由於它重複了初始題目/參考內容中已出現的零機率概念,這是人工複盤/重新說明,不是新的未見評估。如果 (d) 作答錯誤,在提供一個答案未事先展示且真正全新的同等 (d) 題目並無協助通過之前,它仍未獲獨立重新驗證;這裡不提供這樣的新 (d) 題目。之後再以獨立恆等式作額外交叉檢查,而非取代直接檢查($H(p,q)=H(p)+D_{KL}(p\|q)$ 且 $D_{KL}\ge0$;困惑度等於詞元機率幾何平均的倒數)。回饋後的重做屬於補救,不是新的無協助首次嘗試。計時、保存、無協助行為以及故意提前執行檢查器都只是政策要求;介面沒有隱藏的強制執行。(a) 出錯用 day-27-act-01(驚奇度、對數單位)和 day-27-act-02(熵)補救,(b)-(c) 用 day-27-act-03(交叉熵、KL、NLL、困惑度),(d) 用 day-27-act-01(安全處理 $\log 0$)和 day-27-act-03(零機率失效)。

自主實作與記錄

(a) 求 $p=[0.4,0.3,0.2,0.1]$ 的 $H(p)$。(b) 對四個類型上的均勻 $q$ 求 $H(p,q)$ 與 $D_{KL}(p\|q)$。(c) 對得分 $q=[0.5,0.4,0.1]$ 的詞元求逐詞元 NLL、平均 NLL 與困惑度。(d) 為什麼一個 $q=0$ 的被觀察詞元會使語料損失為無窮,並舉一個修法。

獨立掌握標準

首次無協助作答的數值必須在所述絕對容許誤差 0.00005(小數第4位半個單位)內與相應參考值一致;說明為何一個 $q=0$ 的被觀察詞元會使語料損失為無窮,並提出一個具體修法。原樣保留該次嘗試,並將錯題複盤與協助式補救分開。(a)、(b)、(c) 的已保存、無協助補救後自我檢查,只有在該次作答期間既未閱讀也未執行檢查器,並先保存提交時,才可作為直接數值證據;各項必須在所述絕對容許誤差 0.00005(小數第4位半個單位)內與相應參考值一致。這裡提供的 (d) 重複了初始題目/參考內容中已出現的零機率概念,因此是人工複盤/重新說明,不是新的未見證據。如果 (d) 作答錯誤,在提供一個答案未事先展示且真正全新的同等 (d) 題目並無協助通過之前,它仍未獲獨立重新驗證;這裡不提供這樣的題目。錯題接入恢復連結;回饋後的重做屬於補救,不是新的無協助首次嘗試。計時、保存、無協助行為以及故意提前執行檢查器都只是政策要求;介面沒有隱藏的強制執行。

權威來源資源與權利證明

linking Cambridge University Press copyright; free online reading authorized No text extraction or redistribution permitted

Information Theory, Inference, and Learning Algorithms

作者 / 機構: David J. C. MacKay | 原始出處連結 ↗

對應章節 / 時間戳: Chapters 2 and 4 review: entropy and source coding exercises

驗證版本與日期: Cambridge University Press 2003, authorized free PDF (Verified: 2026-10-30)

入選理由:

Authorized-free exercise coverage aligned with this day's assessment scope.

來源前置條件:

Probability foundations (Day 25-26)

安全回退方案(離線彈性):

The assessment is self-contained with original items and worked reference answers.

對比候選資源與選型分析 (3)
Cover & Thomas, Elements of Information Theory ↗ compared_alternative [Wiley copyright]

The standard rigorous reference; paywalled, so it is cited alongside MacKay only via linking rather than as the sole learner-facing text.

James V. Stone, Information Theory: A Tutorial Introduction ↗ compared_alternative [Open Access / CC BY-NC-SA 4.0]

Gentler introduction freely readable on arXiv; less depth on coding and inference connections needed for the language-model-loss bridge.

Shannon, A Mathematical Theory of Communication (1948) ↗ compared_alternative [Bell System Technical Journal copyright]

Historical primary source; valuable citation but dated notation makes it supplementary rather than primary for learners.

linking Wiley copyright / Academic linking only No text extraction or distribution permitted

Elements of Information Theory

作者 / 機構: Thomas M. Cover, Joy A. Thomas | 原始出處連結 ↗

對應章節 / 時間戳: Chapter 2 review problems: entropy and relative entropy

驗證版本與日期: 2nd Edition, Wiley 2006 (Verified: 2026-10-30)

入選理由:

Reference for entropy and KL identities this assessment checks without assistance.

來源前置條件:

Probability foundations and logarithms

安全回退方案(離線彈性):

The assessment is self-contained with original items and worked reference answers.

對比候選資源與選型分析 (2)
David MacKay, Information Theory, Inference, and Learning Algorithms ↗ compared_alternative [Cambridge University Press copyright; free online reading authorized]

Co-selected: equally rigorous and freely readable online, so it carries the primary learner-facing role.

Dive into Deep Learning: Information Theory appendix ↗ compared_alternative [CC BY-SA 4.0 (book text) / modified MIT (sample code)]

Code-integrated deep-learning framing; useful supplementary reading but less mathematically complete for entropy proofs.