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(四位小数半个单位)内与相应参考值一致;说明为何一个 $q=0$ 的被观察词元会使语料损失为无穷,并给出一个具体修法。原样保留该次尝试,并将错题复盘与协助式补救分开。(a)、(b)、(c) 的已保存、无协助的补救后自检,只有在该次作答期间既未阅读也未运行检查器、并先保存提交时,才可作为直接数值证据;各项必须在所述绝对容差 0.00005(四位小数半个单位)内与相应参考值一致。这里提供的 (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.