对数、惊异度与信息单位
仅在记录预测与观察证据后才允许辅助。
前置要求
完成第 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_2 0.5=1$、$-\log_2 0.25=2$、$-\log_2 0.125=3$;奈特 = 比特 $\times \ln 2$。用 math.log2 与 math.log 验证每个单元格。
比特:$0.5\to1$、$0.25\to2$、$0.125\to3$、$0.8\to0.3219$、$0.1\to3.3219$。奈特:$0.6931, 1.3863, 2.0794, 0.2231, 2.3026$。$2$ 比特 $=1.3863$ 奈特。$p$ 越小惊异度越大,故低概率词元的损失约为高概率词元的十倍——这正是惩罚自信错误预测的不对称性。
表格在两种单位下都与已验证数值一致,并能说出 $\log(0)$ 失效与一条具体防护(裁剪/正下限,或仅限 p 接近 1 时的 log1p)加一条明示的 p=0 策略(下限、平滑化或声明的无穷损失)。
权威源资源与权利证明
Information Theory, Inference, and Learning Algorithms
对应章节 / 时间戳: 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)
The standard rigorous reference; paywalled, so it is cited alongside MacKay only via linking rather than as the sole learner-facing text.
Gentler introduction freely readable on arXiv; less depth on coding and inference connections needed for the language-model-loss bridge.
Historical primary source; valuable citation but dated notation makes it supplementary rather than primary for learners.
Python 3 math: Mathematical functions
对应章节 / 时间戳: 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)
Vectorized logs arrive with the tensor track; scalar math functions keep Day 27 dependency-free.
Decimal-precision reference rather than binary float math; math remains the correct module for the operators and constants these days use.