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.