27 / 90

第 27 日:情報理論:エントロピー・交差エントロピーと言語モデル損失

驚異度(サプライザル)をビットとナットで測り、エントロピーと交差エントロピーを手計算し、次トークン負の対数尤度とパープレキシティへの接続を、数値安定性の対策とともに学ぶ。

数学とアルゴリズム基礎 480 分(8 時間の集中学習)
01 120 分

対数・サプライザル・情報量の単位

予測と観測の証跡を記録した後にのみ補助を利用できます。

前提条件

Day 26(確率変数と標本抽出)を完了すること。行き詰まった場合は記載の回復リンク(Day 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$ の約 10 倍になる理由を説明せよ。

独立習得基準

表が両単位で検証済みの値に一致し、$\log(0)$ の失敗と具体的な対策(クリッピング/正の下限、または p が 1 に近い場合に限る log1p)に加えて、明示した p=0 方針(下限・平滑化・宣言した無限損失)を 1 つ述べられること。

公式ソースリソースと権利証跡

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 | オリジナル出所URL ↗

該当セクション・タイムスタンプ: 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 | オリジナル出所URL ↗

該当セクション・タイムスタンプ: 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)$ を計算し、期待サプライザル、すなわち理想的な平均符号長として解釈する。

手順

エントロピーはラベルや結果の数値ではなく確率の値だけに依存する。必須作業:下の 2 つのエントロピーを自力で計算して比較し、単位を明示せよ。

演習と予測記録

(a) $p=[0.5,0.25,0.125,0.125]$ の $H(X)$ を求めよ。(b) 公平な 6 面サイコロの $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 | オリジナル出所URL ↗

該当セクション・タイムスタンプ: 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 | オリジナル出所URL ↗

該当セクション・タイムスタンプ: 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) 言語モデルが次の 3 個の真トークンに $q=0.8, 0.5, 0.25$ を割り当てた:平均 NLL とパープレキシティを求めよ。(d) モデルがある観測トークンに $q=0$ を割り当てるとコーパス損失はどうなるか?

独立習得基準

交差エントロピーと両方向の KL を手計算し、トークンごとの $-\ln q$ が NLL に等しいことを説明し、パープレキシティを指数化した平均として与え、確率ゼロの失敗と対策を 1 つ挙げられること。

公式ソースリソースと権利証跡

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

Elements of Information Theory

著者・提供機関: Thomas M. Cover, Joy A. Thomas | オリジナル出所URL ↗

該当セクション・タイムスタンプ: 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 | オリジナル出所URL ↗

該当セクション・タイムスタンプ: 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・参照資料を使わない。予測を記録し、4 問すべてを計算してから見直し、誤りは回復リンクをたどる。 最初の無支援の試行は変更せず保存する。その後の誤りの見直しと支援つき回復は別段階である。概念の見落としは見直しや再挑戦だけでは修復済みとはならず、答えを事前に見せない真に新しい同等の無支援チェックに合格する必要がある。ここで用意された再チェックは回復学習後のセルフチェックであり、新しい未見の証拠ではない。再チェック(回復後のみ・無支援・ヒントなし): 開始前に解答・参照を閉じ、この試行中はチェッカーを読まず、実行しない。無支援・ヒントなしで解答する。前の回復時に学んだ公式を復習することは概念の復習であり、2回目の数値解答を見せることではない。(a) $p=[0.55,0.20,0.15,0.10]$ の $H(p)$ を求めよ。(b) 4 型に一様な $q$ に対し $H(p,q)$ と $D_{KL}(p\|q)$ を求めよ。(c) $q=[0.25,0.5,0.2]$ と採点されたトークン列のトークンごとの NLL・平均 NLL・パープレキシティを求めよ。(d) さらに観測トークン 1 つが $q=0$ と採点されたら平均 NLL はどうなり、どの対策がそれを防ぐか。 解答後、2回目の無支援提出を変更せず保存してからチェッカーを開いて実行する。チェッカーの数値フィードバックは、回復学習後のセルフチェックとして (a)、(b)、(c) のみに直接用いる。ただし、その試行中にチェッカーを読まず実行もせず、先に提出を変更せず保存していた場合に限り、各項目の合否だけを返す。(d)は、試行後に、解答欄にある別個の事後評価ルーブリックを使って手動で評価する。初回の設問/参照内容ですでに明示されたゼロ確率の概念を繰り返すため、これは手動の復習・再説明であり、新しい未見の評価ではない。(d)を誤った場合、答えを事前に示さない真に新しい同等の (d) 課題が提示され、それに無支援で合格するまで独立には再検証されない。ここにはその新しい (d) 課題は用意されていない。その後、独立な恒等式を直接照合の代わりではなく追加の検算として用いる($H(p,q)=H(p)+D_{KL}(p\|q)$、$D_{KL}\ge0$;パープレキシティはトークン確率の幾何平均の逆数)。フィードバック後の再挑戦は回復学習であり、新しい無支援の初回試行とはみなさない。時間管理、保存、無支援での実施、意図的なチェッカーの早期実行はすべてポリシー上の要件であり、UI に隠れた強制機能はない。(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) 4 型に一様な $q$ に対し $H(p,q)$ と $D_{KL}(p\|q)$ を求めよ。(c) $q=[0.5,0.4,0.1]$ と採点されたトークン列のトークンごとの NLL・平均 NLL・パープレキシティを求めよ。(d) 観測トークンに $q=0$ が 1 つあるだけでコーパス損失が無限大になる理由と対策を 1 つ挙げよ。

独立習得基準

初回の無支援で求めた数値が、明記された絶対許容誤差 0.00005(小数第4位の半単位)以内で対応する参照値と一致し、観測トークンに $q=0$ が一つあるだけでコーパス損失が無限大になる理由と具体的な対策を説明できること。その試行を変更せず保存し、誤りの見直しと支援つき回復を別段階に分ける。(a)、(b)、(c) の保存済み無支援の回復学習後セルフチェックは、その試行中にチェッカーを読まず実行せず、先に提出を保存した場合に限り、直接的な数値証拠として用いる。各対応する参照値との一致は、明記された絶対許容誤差 0.00005(小数第4位の半単位)以内であること。用意された (d) は初回の設問/参照内容にすでにあるゼロ確率の概念を繰り返すため、新しい未見の証拠ではなく、手動の復習・再説明である。(d) を誤った場合、答えを事前に示さない真に新しい同等の (d) 課題が提示され、それに無支援で合格するまで独立には再検証されない。ここにはその課題は用意されていない。誤りは回復リンクにつなげ、フィードバック後の再挑戦は回復学習であって新しい無支援の初回試行ではない。時間管理、保存、無支援での実施、意図的なチェッカー早期実行はポリシー上の要件に限られ、UI に隠れた強制機能はない。

公式ソースリソースと権利証跡

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 | オリジナル出所URL ↗

該当セクション・タイムスタンプ: 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 | オリジナル出所URL ↗

該当セクション・タイムスタンプ: 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.