40 / 90

第 40 天:可學習的嵌入表與分散式表示

以可學習的嵌入矩陣 C 取代稀疏的 one-hot 指示向量。證明查表等於選取行的恆等關係,把列讀作特徵空間中的點,並在前向傳播之前預測張量形狀與參數數量。每個活動都記錄預測、觀察、複習與復原。

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

從 one-hot 向量到嵌入查表

Agent 輔助工程檢查

前置要求

第 39 天的斷詞與 n-gram 統計;數學模組中的矩陣乘法基礎。

學習目標

推導為什麼嵌入查表等價於 one-hot 向量乘以嵌入矩陣,並對真實配置預測表的形狀與參數數量。

操作指引

必做任務:使用小詞彙表 V = {'.', 'a', 'b', 'c'}(索引 0-3)與嵌入表 C,其各列分別為 '.'=[0.1,0.3]、'a'=[0.2,-0.1]、'b'=[-0.4,0.5]、'c'=[0.0,0.2],形狀 (V,d) = (4,2)。證明 one-hot 向量 e_2 = [0,0,1,0] 與 C 相乘會選出第 2 列,得到 [-0.4, 0.5]。解釋為什麼程式以整數索引查表 C[i],而不是物化一個 V 維 one-hot 向量。選做任務:說明若同一個字元由兩張不同的表嵌入,會有什麼變化。

自主實作與記錄

1. 用上表計算 e_3 * C,並說明選中了哪一列。
2. 對姓名詞彙表 V=27、d=10,給出 C 的形狀與參數數量。
3. 用一句話說明為什麼 C[i] 比先構造 e_i 再做乘法更省。

獨立掌握標準

學習者能寫出恆等式 e_i * C = C[i,:],正確算出兩個範例查表結果,並在無提示下推出 (27,10) 表的 270 個參數。

權威來源資源與權利證明

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 | 原始出處連結 ↗

對應章節 / 時間戳: makemore MLP lecture segment: indexing the embedding table C with one-hot vectors

驗證版本與日期: 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 (feature vectors and the joint probability model)

驗證版本與日期: 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 分鐘

分散式表示:列即學習出的點

獨立掌握檢查

前置要求

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

學習目標

把嵌入列解釋為學習出的特徵空間中的座標,計算內積與餘弦相似度,並解釋為什麼未訓練的列不攜帶語義。

操作指引

必做任務:把 C 的每一列當作 R^2 中的點。對兩對列分別計算 dot(u,v) = sum_i u_i v_i 與 cos(u,v) = dot(u,v)/(||u||*||v||)。然後用自己的話解釋:要讓「相似」的字元最終彼此靠近,訓練必須改變什麼——表的初值是隨機的,所以初始距離沒有意義。選做任務:在紙上畫出四個點並標出最近的一對。

自主實作與記錄

使用列 '.'=[0.1,0.3]、'a'=[0.2,-0.1]、'b'=[-0.4,0.5]、'c'=[0.0,0.2]:
1. 計算 dot('.', 'c') 與 cos('.', 'c')。
2. 計算 cos('a', 'b')。
3. 這張表中哪一對最近?在訓練之前你會信任這些相似度嗎?為什麼?

獨立掌握標準

獨立計算兩個餘弦相似度且誤差在 ±0.01 以內,並能正確說明隨機嵌入在訓練前不編碼任何語義。

權威來源資源與權利證明

linking JMLR open access (author-retained copyright)

A Neural Probabilistic Language Model

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

對應章節 / 時間戳: Section 1 (distributed representation fights the curse of dimensionality)

驗證版本與日期: 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.

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 | 原始出處連結 ↗

對應章節 / 時間戳: Embedding table inspection segment; 2-D embedding visualization of learned character rows

驗證版本與日期: 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.

03 120 分鐘

上下文串接與參數台帳

Agent 輔助工程檢查

前置要求

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

學習目標

按上下文順序串接各位置的嵌入列來組裝模型輸入,預測其維度,並為微型模型與姓名規模配置維護完整參數台帳。

操作指引

必做任務:block_size = 2、上下文 ('a','b') = 索引 [1,2] 時,網路輸入為 x = concat(C[1], C[2]) = [0.2, -0.1, -0.4, 0.5],維度 block_size * d = 4。為上下文 ('b','.') 構建同樣的向量。然後寫出微型模型(隱藏層大小 3)與姓名規模配置 V=27、d=10、block=3、hidden=100 的參數台帳。選做任務:解釋為什麼串接中的位置順序不能打亂。

自主實作與記錄

1. 寫出上下文 ('b','.') 的 x。
2. 微型台帳:C (4x2)、W1 (4x3)、b1 (3)、W2 (3x4)、b2 (4)——總參數數?
3. 姓名台帳:C (27x10)、W1 (30x100)、b1 (100)、W2 (100x27)、b2 (27)——總參數數?

獨立掌握標準

給出位置順序正確的串接向量,並推導出(而非背出)兩份台帳:39 與 6097。

權威來源資源與權利證明

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 building X from context indices and flattening C[X] into the input layer

驗證版本與日期: 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 (concatenated feature vectors x = (C(w_{t-n+1}), ..., C(w_{t-1})))

驗證版本與日期: 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 分鐘

獨立掌握檢查:查表、形狀與參數計數

獨立掌握檢查

前置要求

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

學習目標

在聲明的無 AI 條件下,檢驗對嵌入查表、分散式幾何、上下文串接與參數核算的獨立掌握程度。

操作指引

必做任務:闔上書本,在打開參考解答或知識測驗之前,把推導過程記錄在草稿工作區。推導本身不得求助 AI 助手或計算器(事後可以驗算)。選做任務:把隱藏層改為 200,重做台帳並比較。

自主實作與記錄

新任務使用 V=8 個符號、d=4、block_size=3、hidden=64。
1. C 的形狀與參數數。
2. 串接輸入 x 的維度。
3. C、W1、b1、W2、b2 的總參數數。
4. 一句話:訓練為什麼會推動嵌入列移動?

客觀知識測驗:嵌入與查找表

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

聲明輔助條件

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

1.

把 V=27 個符號的詞彙表嵌入 d=10 維時,嵌入表 C 含有多少可訓練參數?

2.

one-hot 向量 e_i 與嵌入矩陣 C 相乘得到:

3.

block_size = 3、嵌入維度 d = 10 時,串接上下文向量 x 的維度是多少?

作答應證與歷史記錄

    獨立掌握標準

    獨立推導出全部四個答案:32 個參數、維度 12、合計 1384,以及關於梯度驅動列移動的正確一句話解釋。

    權威來源資源與權利證明

    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 | 原始出處連結 ↗

    對應章節 / 時間戳: Embedding table construction and parameter summary segments

    驗證版本與日期: 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 (model overview and notation)

    驗證版本與日期: 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.