41 / 90

第 41 天:神經語言模型的前向傳播

用手算把 Makemore 式 MLP 從頭走到尾:嵌入查表、上下文串接、tanh 隱藏層、輸出 logit、數值穩定的 softmax。每個活動都記錄預測、觀察、複習與復原。

模型與訓練 480 分鐘主動學習時間(8 小時專注學習)
01 120 分鐘

神經語言模型流水線:從索引到預激活

Agent 輔助工程檢查

前置要求

第 40 天的嵌入查表與上下文串接;第 34-36 天的 MLP 機制。

學習目標

走完 索引 → C 的列 → 串接 x → 隱藏層預激活 的完整流水線,並手算輸入向量與預激活向量。

操作指引

必做任務:沿用第 40 天的表 C 與上下文 ('a','b'),故 x = [0.2, -0.1, -0.4, 0.5]。今天權重固定:W1 = [[0.5,-0.2,0.1],[0.3,0.4,-0.3],[-0.1,0.2,0.6],[0.2,-0.5,0.4]],b1 = [0.1,-0.1,0.0]。計算各隱藏層預激活 pre_j = sum_i x_i * W1[i][j] + b1[j]。先記錄你的預測值再查看推演。選做任務:預測哪個輸入座標對 pre_1 影響最大。

自主實作與記錄

1. 用上面的 x 與 W1 計算 pre_0、pre_1、pre_2,寫出每一個乘積。
2. 預測是否有某個預激活恰好為 0。
3. 按順序說出從原始符號到 pre 的流水線各階段。

獨立掌握標準

手算預激活寫出每個乘積並與 [0.31, -0.51, 0.01] 一致,學習者能不看筆記按順序敘述流水線各階段。

權威來源資源與權利證明

linking MIT License (repository) / Standard YouTube License (video, subtitle rights unknown) Video subtitles have unknown distribution rights and are not imported

makemore: Character-level language models on names.txt

作者 / 機構: Andrej Karpathy | 原始出處連結 ↗

對應章節 / 時間戳: Segment wiring the MLP forward pass: C[X] -> hidden tanh layer -> output logits

驗證版本與日期: Repository state verified 2026-10-30; lecture published 2022

入選理由:

Standard beginner walkthrough of the Bengio-style MLP language model; the MIT-licensed reference implementation pairs with a stepwise derivation that matches the Days 40-42 worked numbers exactly.

來源前置條件:

Python basics, matrix multiplication, and the Day 37-39 tokenization and n-gram context

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

All forward-pass arithmetic, shapes, parameter counts, and loss values are reproduced as original hand-worked examples in the lesson text; no video, subtitle, or transcript is required.

對比候選資源與選型分析 (3)
Yoshua Bengio et al.: A Neural Probabilistic Language Model (JMLR 2003) ↗ compared_selected_co_primary [JMLR open access (author-retained copyright)]

The original architecture paper; selected jointly as the theoretical citation while Makemore supplies the stepwise implementation walkthrough.

Stanford CS336: Language Modeling from Scratch ↗ compared_deferred [No license declared (the repository has no LICENSE file, so all rights are reserved by default)]

Strong PyTorch rigor but assumes fluent tensor programming; deferred to the Days 43-60 pretraining blocks rather than the first embedding contact.

3Blue1Brown: Neural Networks series ↗ compared_alternative [3Blue1Brown copyright (all rights reserved); site FAQ permits only short clips under stated conditions]

Strong visual intuition for network mechanics but covers image classification rather than embedding tables and next-token language modeling; retained as optional supplementary viewing.

linking JMLR open access (author-retained copyright)

A Neural Probabilistic Language Model

作者 / 機構: Yoshua Bengio, Rejean Ducharme, Pascal Vincent, Christian Jauvin | 原始出處連結 ↗

對應章節 / 時間戳: Section 2 (the mapping g: context feature vectors to next-word probabilities)

驗證版本與日期: JMLR Volume 3 (2003); verified 2026-10-30

入選理由:

The original paper introducing jointly learned word feature vectors and the neural probabilistic language model; grounds the curriculum's embedding and MLP derivations in the primary literature.

來源前置條件:

Probability basics and linear algebra

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

Every worked derivation in the lessons is self-contained and offline-complete; the paper is further reading only.

對比候選資源與選型分析 (3)
Andrej Karpathy: Let's build makemore (MLP) ↗ compared_selected_co_primary [MIT (repository code) / Standard YouTube License (video)]

Selected jointly: Makemore reproduces this architecture at character scale with MIT-licensed code; the paper supplies the original formalism and the n-gram comparison framing.

Strong textbook coverage of n-grams through neural LMs; kept as secondary reading because the primary derivation follows the original paper's notation.

Foundational for embedding geometry but trains on context-prediction objectives rather than the full next-token probabilistic model studied this week; revisited in later representation blocks.

02 120 分鐘

手算隱藏層:tanh 與為何需要非線性

獨立掌握檢查

前置要求

完成上一活動;如受阻請使用列出的復原連結。

學習目標

逐元素施加 tanh,精確算出隱向量,並論證兩層線性映射之間為何必須有非線性。

操作指引

必做任務:h = tanh(pre) 逐元素計算。使用 tanh(0.31) = 0.300437、tanh(-0.51) = -0.469945、tanh(0.01) = 0.010000,並用計算器或 Python 的 math.tanh 驗算至少一個。然後回答坍縮問題:若激活函數換成恆等映射而非 tanh,兩個線性映射會退化成什麼?選做任務:計算 tanh(5.0) 與 tanh(-5.0) 以觀察飽和。

自主實作與記錄

1. 寫出隱向量 h。
2. tanh 會飽和:tanh(5.0) 取四位小數是多少?飽和對非常大的預激活意味著什麼?
3. 證明坍縮:恆等激活時 h = x*W1、z = h*W2,請把 z 只用 x 表示。

獨立掌握標準

h 精確到小數點後 6 位,給出飽和觀察,並能用一行證明堆疊的線性映射坍縮成單個線性映射。

權威來源資源與權利證明

linking MIT License (repository) / Standard YouTube License (video, subtitle rights unknown) Video subtitles have unknown distribution rights and are not imported

makemore: Character-level language models on names.txt

作者 / 機構: Andrej Karpathy | 原始出處連結 ↗

對應章節 / 時間戳: Hidden layer construction segment: h = tanh(X W1 + b1) with shape annotations

驗證版本與日期: Repository state verified 2026-10-30; lecture published 2022

入選理由:

Standard beginner walkthrough of the Bengio-style MLP language model; the MIT-licensed reference implementation pairs with a stepwise derivation that matches the Days 40-42 worked numbers exactly.

來源前置條件:

Python basics, matrix multiplication, and the Day 37-39 tokenization and n-gram context

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

All forward-pass arithmetic, shapes, parameter counts, and loss values are reproduced as original hand-worked examples in the lesson text; no video, subtitle, or transcript is required.

對比候選資源與選型分析 (3)
Yoshua Bengio et al.: A Neural Probabilistic Language Model (JMLR 2003) ↗ compared_selected_co_primary [JMLR open access (author-retained copyright)]

The original architecture paper; selected jointly as the theoretical citation while Makemore supplies the stepwise implementation walkthrough.

Stanford CS336: Language Modeling from Scratch ↗ compared_deferred [No license declared (the repository has no LICENSE file, so all rights are reserved by default)]

Strong PyTorch rigor but assumes fluent tensor programming; deferred to the Days 43-60 pretraining blocks rather than the first embedding contact.

3Blue1Brown: Neural Networks series ↗ compared_alternative [3Blue1Brown copyright (all rights reserved); site FAQ permits only short clips under stated conditions]

Strong visual intuition for network mechanics but covers image classification rather than embedding tables and next-token language modeling; retained as optional supplementary viewing.

linking JMLR open access (author-retained copyright)

A Neural Probabilistic Language Model

作者 / 機構: Yoshua Bengio, Rejean Ducharme, Pascal Vincent, Christian Jauvin | 原始出處連結 ↗

對應章節 / 時間戳: Sections 2-3 (hidden layer with tanh units and free parameters)

驗證版本與日期: JMLR Volume 3 (2003); verified 2026-10-30

入選理由:

The original paper introducing jointly learned word feature vectors and the neural probabilistic language model; grounds the curriculum's embedding and MLP derivations in the primary literature.

來源前置條件:

Probability basics and linear algebra

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

Every worked derivation in the lessons is self-contained and offline-complete; the paper is further reading only.

對比候選資源與選型分析 (3)
Andrej Karpathy: Let's build makemore (MLP) ↗ compared_selected_co_primary [MIT (repository code) / Standard YouTube License (video)]

Selected jointly: Makemore reproduces this architecture at character scale with MIT-licensed code; the paper supplies the original formalism and the n-gram comparison framing.

Strong textbook coverage of n-grams through neural LMs; kept as secondary reading because the primary derivation follows the original paper's notation.

Foundational for embedding geometry but trains on context-prediction objectives rather than the full next-token probabilistic model studied this week; revisited in later representation blocks.

03 120 分鐘

logit、數值穩定的 softmax 與輸出分布

Agent 輔助工程檢查

前置要求

完成上一活動;如受阻請使用列出的復原連結。

學習目標

計算 logit z = h*W2 + b2,執行減最大值 softmax,得到下一符號分布,並用反例解釋穩定性技巧。

操作指引

必做任務:W2 = [[0.2,-0.1,0.3,0.1],[0.4,0.2,-0.2,0.0],[-0.3,0.1,0.5,-0.4]],b2 = [0.05,-0.05,0.02,0.0]。用昨天的 h 計算 z = h*W2 + b2。然後做 softmax:減去 m = max(z),取指數,正規化。反例:logit [1000,999,998] 時,樸素 exp(1000) 在 float64 中溢位;平移版本給出 [0.665241, 0.244728, 0.090031]。選做任務:證明對任意常數 c 都有 softmax(z - c) = softmax(z)。

自主實作與記錄

1. 計算四個 logit z。
2. 取 m = max(z),計算 exp(z_j - m)、正規化之和與 p。
3. 驗證 p 之和為 1。
4. 用一句話說明為什麼減去最大值在數學上不改變結果。

獨立掌握標準

z 與 p 精確到小數點後 6 位,完成和為 1 的驗證,並對減最大值的不變性給出正確論證。

權威來源資源與權利證明

linking MIT License (repository) / Standard YouTube License (video, subtitle rights unknown) Video subtitles have unknown distribution rights and are not imported

makemore: Character-level language models on names.txt

作者 / 機構: Andrej Karpathy | 原始出處連結 ↗

對應章節 / 時間戳: Output layer and loss segment: logits = h W2 + b2, softmax probabilities, cross-entropy loss

驗證版本與日期: Repository state verified 2026-10-30; lecture published 2022

入選理由:

Standard beginner walkthrough of the Bengio-style MLP language model; the MIT-licensed reference implementation pairs with a stepwise derivation that matches the Days 40-42 worked numbers exactly.

來源前置條件:

Python basics, matrix multiplication, and the Day 37-39 tokenization and n-gram context

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

All forward-pass arithmetic, shapes, parameter counts, and loss values are reproduced as original hand-worked examples in the lesson text; no video, subtitle, or transcript is required.

對比候選資源與選型分析 (3)
Yoshua Bengio et al.: A Neural Probabilistic Language Model (JMLR 2003) ↗ compared_selected_co_primary [JMLR open access (author-retained copyright)]

The original architecture paper; selected jointly as the theoretical citation while Makemore supplies the stepwise implementation walkthrough.

Stanford CS336: Language Modeling from Scratch ↗ compared_deferred [No license declared (the repository has no LICENSE file, so all rights are reserved by default)]

Strong PyTorch rigor but assumes fluent tensor programming; deferred to the Days 43-60 pretraining blocks rather than the first embedding contact.

3Blue1Brown: Neural Networks series ↗ compared_alternative [3Blue1Brown copyright (all rights reserved); site FAQ permits only short clips under stated conditions]

Strong visual intuition for network mechanics but covers image classification rather than embedding tables and next-token language modeling; retained as optional supplementary viewing.

linking JMLR open access (author-retained copyright)

A Neural Probabilistic Language Model

作者 / 機構: Yoshua Bengio, Rejean Ducharme, Pascal Vincent, Christian Jauvin | 原始出處連結 ↗

對應章節 / 時間戳: Section 2 (softmax output guarantees positive probabilities summing to one)

驗證版本與日期: JMLR Volume 3 (2003); verified 2026-10-30

入選理由:

The original paper introducing jointly learned word feature vectors and the neural probabilistic language model; grounds the curriculum's embedding and MLP derivations in the primary literature.

來源前置條件:

Probability basics and linear algebra

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

Every worked derivation in the lessons is self-contained and offline-complete; the paper is further reading only.

對比候選資源與選型分析 (3)
Andrej Karpathy: Let's build makemore (MLP) ↗ compared_selected_co_primary [MIT (repository code) / Standard YouTube License (video)]

Selected jointly: Makemore reproduces this architecture at character scale with MIT-licensed code; the paper supplies the original formalism and the n-gram comparison framing.

Strong textbook coverage of n-grams through neural LMs; kept as secondary reading because the primary derivation follows the original paper's notation.

Foundational for embedding geometry but trains on context-prediction objectives rather than the full next-token probabilistic model studied this week; revisited in later representation blocks.

04 120 分鐘

獨立掌握檢查:在保留上下文上的完整前向傳播

獨立掌握檢查

前置要求

完成活動 41-01 至 41-03。

學習目標

對新上下文 ('b','a') 獨立復現整條前向傳播,並誠實指出預測符號。

操作指引

必做任務:闔上書本。對上下文 ('b','a') = 索引 [2,1],用同一套 C、W1、b1、W2、b2 計算完整前向傳播,並在打開參考解答前預測下一符號。選做任務:若真實下一符號為 'a'(索引 1),寫出其交叉熵損失——第 42 天會用到。

自主實作與記錄

對上下文 ('b','a'):
1. 串接輸入 x。
2. 預激活與隱向量 h。
3. logit z。
4. 機率 p 與 argmax 符號。

客觀知識測驗:前向傳播與 softmax

請回答以下診斷題目檢驗掌握情況。提交前請誠實聲明你的輔助條件。

聲明輔助條件

說明:無 AI 輔助屬於誠實聲明條件,非平台監控或強制檢測手段。

1.

數值穩定的 softmax 為什麼在取指數前從每個 logit 中減去 max(z)?

2.

兩個堆疊的線性層之間沒有非線性激活時,它等價於:

3.

給定符號 ['.','a','b','c'] 上的 p = [0.290410, 0.237875, 0.220627, 0.251087],argmax 預測是:

作答應證與歷史記錄

    獨立掌握標準

    獨立復現 x、h、z、p 並與參考值精確到小數點後 6 位一致,指出 argmax '.' 是索引 0,並對未訓練預測給出誠實評論。

    權威來源資源與權利證明

    linking MIT License (repository) / Standard YouTube License (video, subtitle rights unknown) Video subtitles have unknown distribution rights and are not imported

    makemore: Character-level language models on names.txt

    作者 / 機構: Andrej Karpathy | 原始出處連結 ↗

    對應章節 / 時間戳: End-to-end forward pass segment evaluated on a single minibatch

    驗證版本與日期: Repository state verified 2026-10-30; lecture published 2022

    入選理由:

    Standard beginner walkthrough of the Bengio-style MLP language model; the MIT-licensed reference implementation pairs with a stepwise derivation that matches the Days 40-42 worked numbers exactly.

    來源前置條件:

    Python basics, matrix multiplication, and the Day 37-39 tokenization and n-gram context

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

    All forward-pass arithmetic, shapes, parameter counts, and loss values are reproduced as original hand-worked examples in the lesson text; no video, subtitle, or transcript is required.

    對比候選資源與選型分析 (3)
    Yoshua Bengio et al.: A Neural Probabilistic Language Model (JMLR 2003) ↗ compared_selected_co_primary [JMLR open access (author-retained copyright)]

    The original architecture paper; selected jointly as the theoretical citation while Makemore supplies the stepwise implementation walkthrough.

    Stanford CS336: Language Modeling from Scratch ↗ compared_deferred [No license declared (the repository has no LICENSE file, so all rights are reserved by default)]

    Strong PyTorch rigor but assumes fluent tensor programming; deferred to the Days 43-60 pretraining blocks rather than the first embedding contact.

    3Blue1Brown: Neural Networks series ↗ compared_alternative [3Blue1Brown copyright (all rights reserved); site FAQ permits only short clips under stated conditions]

    Strong visual intuition for network mechanics but covers image classification rather than embedding tables and next-token language modeling; retained as optional supplementary viewing.

    linking JMLR open access (author-retained copyright)

    A Neural Probabilistic Language Model

    作者 / 機構: Yoshua Bengio, Rejean Ducharme, Pascal Vincent, Christian Jauvin | 原始出處連結 ↗

    對應章節 / 時間戳: Sections 1-2 (architecture overview)

    驗證版本與日期: JMLR Volume 3 (2003); verified 2026-10-30

    入選理由:

    The original paper introducing jointly learned word feature vectors and the neural probabilistic language model; grounds the curriculum's embedding and MLP derivations in the primary literature.

    來源前置條件:

    Probability basics and linear algebra

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

    Every worked derivation in the lessons is self-contained and offline-complete; the paper is further reading only.

    對比候選資源與選型分析 (3)
    Andrej Karpathy: Let's build makemore (MLP) ↗ compared_selected_co_primary [MIT (repository code) / Standard YouTube License (video)]

    Selected jointly: Makemore reproduces this architecture at character scale with MIT-licensed code; the paper supplies the original formalism and the n-gram comparison framing.

    Strong textbook coverage of n-grams through neural LMs; kept as secondary reading because the primary derivation follows the original paper's notation.

    Foundational for embedding geometry but trains on context-prediction objectives rather than the full next-token probabilistic model studied this week; revisited in later representation blocks.