33 / 90

第 33 天:梯度缺陷诊断分析与无 AI 独立掌握标准评估

诊断真实自动微分实现缺陷、评估多次反向传播与向量输出等边缘情况、理解引擎扩展性边界,并完成无 AI 独立掌握标准评估。

数学与算法基础 480 分钟(8 小时专注学习)
01 120 分钟

缺陷案例实战:精准定位梯度覆盖 Bug

Agent 辅助工程检查

前置要求

第 32 天梯度累加机制与数值梯度检验。

学习目标

通过构造极简复现用例 (MRE),在数值差分比对下精准定位出因覆盖赋值导致隐蔽梯度错误的具体节点与代码缺陷。

操作指引

使用 CLI 运行梯度缺陷诊断排查命令:

python3 projects/autograd/cli.py diagnose --bug-type overwrite-accumulation

观察诊断程序如何在表达式 $f = (a + b) \cdot a$ 中隔离出节点 a,展示其解析梯度 $7.0$ 与数值差分梯度 $10.0$ 之间高达 $30\%$ 的相对误差。

自主演练与记录

1. 写出能够可靠触发梯度覆盖缺陷的极简复现代码 (MRE)。
2. 解释为何类似 $z = x \cdot y$ 这样的单分支测试用例即使在存在该缺陷时依然能够通过 gradcheck。
3. 在 _backward() 中指出修复该缺陷的关键代码修改。

独立掌握标准

学习者能够构造出区分累加与覆盖实现的极简测试用例,并准确解释其数学原理。

权威源资源与权利证明

linking MIT

micrograd: A tiny scalar-valued autograd engine

作者 / 机构: Andrej Karpathy | 原始出处链接 ↗

对应章节 / 时间戳: micrograd/engine.py: class Value (_backward closures and backward() topological sort); README 'Example usage'

验证版本与日期: commit a9a9f94 (Verified: 2026-10-30)

入选理由:

Direct demonstration of how simple variable reuse creates branching DAGs requiring accumulation.

来源前置条件:

Autograd backward pass and multivariable calculus

安全回退方案(离线弹性):

Local diagnostic harness in projects/autograd/diagnostics.py.

对比候选资源与选型分析 (2)
PyTorch autograd C++ engine ↗ compared_alternative [Modified BSD]

Production scale, but too large and complex for tracing by hand in introductory lessons.

Autograd (HIPS) ↗ compared_alternative [MIT]

Vectorized autograd; micrograd's pure scalar model is pedagogical for hand backprop.

02 120 分钟

自动微分边界情况:多次反向传播与向量输出

独立掌握检查

前置要求

活动 33-01 与拓扑逆序执行机制。

学习目标

理解连续调用 `backward()` 时的梯度累加语义,解释为何优化器每轮迭代必须显式清零梯度 (`zero_grad()`),并掌握多输出图的向量雅可比积 (VJP)。

操作指引

运行多输出预设的前向与反向命令:

python3 projects/autograd/cli.py forward-backward --preset multi-output

观察 backward_multiple([y1, y2], grad_outputs=[1.0, 1.0]) 如何同时为多个终端输出节点提供初始种子梯度并完成反向传播。

自主演练与记录

1. 如果在未调用 grad = 0.0 清零的情况下,对同一个计算图连续调用两次 out.backward(),输入叶子节点的梯度数值会变成多少?
2. 为什么主流深度学习框架(如 PyTorch)默认采取梯度累加策略,而不是每次自动清零?
3. 写出多输出向量向输入反向求导时的向量-雅可比积 (VJP) 表达式。

独立掌握标准

学习者能够清晰解释训练循环中清零梯度的必要性,以及梯度累加在大模型分布式训练中的工程价值。

权威源资源与权利证明

linking Modified BSD

PyTorch Documentation: Autograd Mechanics

作者 / 机构: PyTorch Contributors | 原始出处链接 ↗

对应章节 / 时间戳: How autograd encodes the history

验证版本与日期: PyTorch 2.5 (Verified: 2026-10-30)

入选理由:

Clarifies semantics of gradient zeroing before repeated backward passes and vector-Jacobian products.

来源前置条件:

Computational graph basics

安全回退方案(离线弹性):

Stand-alone CLI support in projects/autograd/cli.py.

对比候选资源与选型分析 (2)
TensorFlow tf.GradientTape Documentation ↗ compared_alternative [Apache 2.0]

Alternative tape-based execution; PyTorch dynamic graph semantics are standard in modern LLM research.

JAX autodiff cookbook ↗ compared_alternative [Apache 2.0]

Functional transformations (grad, vjp); PyTorch object-based graph is closer to the beginner micrograd model.

03 120 分钟

来源归属、开源合规与引擎扩展性边界

Agent 辅助工程检查

前置要求

活动 33-01 与 33-02。

学习目标

严格遵守开源许可规范,清晰区分教学型标量自动微分引擎与工业级张量编译计算系统(PyTorch、JAX)的系统边界。

操作指引

阅读 projects/autograd/docs/guide.zh-Hans.md 与 Stanford CS336 作业规范。注意 micrograd 采用 MIT 许可证,要求保留版权与许可声明;讲义课程资源具有独立的版权保护。

自主演练与记录

1. 为什么纯 Python 标量自动微分引擎不能用于预训练一个 70 亿参数 (7B) 的语言模型?
2. 对比 Python 堆对象开销与编译型 C++/CUDA 连续张量内存缓冲区的差异。
3. 说明在引用与复用 Andrej Karpathy 的 micrograd 代码时应遵守的开源合规要求。

独立掌握标准

学习者能够清晰阐明标量教学引擎与张量编译框架在底层数据结构、内存布局与计算效率上的本质区别。

权威源资源与权利证明

linking MIT

CS336 Assignment 1: Basics (Spring 2025 tagged release)

作者 / 机构: Stanford University CS336 Staff | 原始出处链接 ↗

对应章节 / 时间戳: README setup; tests/adapters.py; tests/test_optimizer.py; tests/test_nn_utils.py; tests/test_serialization.py

验证版本与日期: spring2025 tag; README, tests and MIT LICENSE checked 2026-11-01

入选理由:

Lead assignment-driven source for pretraining component practice and tests. nanoGPT remains a compact historical code-reading companion, not the current recommended training curriculum.

来源前置条件:

Software licensing awareness

安全回退方案(离线弹性):

Local documentation in projects/autograd/docs/.

对比候选资源与选型分析 (2)
Karpathy nanoGPT ↗ compared_alternative [MIT]

Simpler code layout, useful complementary reference for minimal PyTorch Transformer.

Hugging Face Transformers Course ↗ compared_alternative [Apache-2.0]

High-level API focus rather than building from raw tensor fundamentals.

04 120 分钟

独立掌握项目标准评估与无 AI 验证

独立掌握检查

前置要求

活动 33-01 至 33-03。

学习目标

对照三项核心独立掌握标准,完成自动微分学习项目的综合自评与无 AI 客观验证。

操作指引

请在无大语言模型、无自动补全的条件下完成下方的诊断测验,并对照三大掌握标准评估自己的项目实现。

自主演练与记录

回顾独立掌握三大评估维度:
1. 白板推导:手写多元微积分链式法则,证明分支 DAG 节点为何必须使用 +=。
2. 白板实现:在空白文本文件中独立实现 Value、算子重载、拓扑排序与 backward()。
3. 差分排查:编写有限差分 gradcheck 函数,并成功定位并修复故意植入的梯度覆盖 Bug。
在演练区确认并记录你的独立达标情况。

客观知识测验:缺陷诊断、多次反向传播与掌握标准

请回答以下诊断题目检验掌握情况。提交前请诚实声明你的辅助条件。

声明辅助条件

说明:无 AI 辅助属于诚实声明条件,非平台监控或强制检测手段。

1.

在模型训练循环中,如果调用 loss.backward() 前未执行 optimizer.zero_grad() 清零梯度,会引发什么后果?

2.

为什么像 micrograd 这样的纯 Python 标量自动微分引擎根本无法用于预训练大语言模型?

3.

在本项目的无 AI 独立掌握标准 (Independent Mastery Check) 中,哪种情况代表真正的独立掌握?

作答应证与历史记录

    独立掌握标准

    在无外部 AI 辅助的情况下,客观测试获得 100% 得分,并满足全部三项独立掌握评估标准。

    权威源资源与权利证明

    linking MIT

    micrograd: A tiny scalar-valued autograd engine

    作者 / 机构: Andrej Karpathy | 原始出处链接 ↗

    对应章节 / 时间戳: micrograd/engine.py: class Value (_backward closures and backward() topological sort); README 'Example usage'

    验证版本与日期: Git commit master (Verified: 2026-10-30)

    入选理由:

    Minimal, readable implementation of scalar autograd and MLP layers directly matched to our project.

    来源前置条件:

    Python object-oriented programming and operator overloading from Day 31.

    安全回退方案(离线弹性):

    The local projects/autograd engine is runnable offline and matches micrograd semantics.

    对比候选资源与选型分析 (2)
    PyTorch autograd C++ engine ↗ compared_alternative [Modified BSD]

    Production scale, but too large and complex for tracing by hand in introductory lessons.

    Autograd (HIPS) ↗ compared_alternative [MIT]

    Vectorized autograd; micrograd's pure scalar model is pedagogical for hand backprop.

    linking 3Blue1Brown copyright (all rights reserved); site FAQ permits only short clips under stated conditions No video, audio, transcript or frame redistribution permitted

    What is backpropagation really doing? (Chapter 3, Deep Learning)

    作者 / 机构: Grant Sanderson (3Blue1Brown) | 原始出处链接 ↗

    对应章节 / 时间戳: The Intuition for Backpropagation

    验证版本与日期: Online video and article (Verified: 2026-10-30)

    入选理由:

    Visual and geometric intuition for how nudging weights impacts the final cost function.

    来源前置条件:

    Basic derivative intuition from Days 16-18.

    安全回退方案(离线弹性):

    The lesson derives the network gradient equations step by step without requiring video playback.

    对比候选资源与选型分析 (2)
    StatQuest Neural Networks Part 3 ↗ compared_alternative [Proprietary]

    Step-by-step arithmetic without calculus notation; 3Blue1Brown provides the calculus-visual link needed.

    Khan Academy Multivariable Chain Rule ↗ compared_alternative [CC BY-NC-SA 3.0]

    Pure math focus; 3Blue1Brown contextualizes the chain rule specifically for neural network graphs.