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.