對數、驚異度與資訊單位
僅在記錄預測與觀察證據後才允許輔助。
前置要求
完成第 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.