31 / 90

第 31 天:前向计算图、局部导数与反向传播链式法则

掌握自动微分核心基础:构建标量有向无环图 (DAG) 计算图、推导基础算子局部导数,并手算跟踪反向传播传播过程。

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

计算图构建与标量前向求值

Agent 辅助工程检查

前置要求

第 30 天数学基础、Python 面向对象基础,以及理解变量作为对象引用的内存模型。

学习目标

理解如何将标量算术表达式映射为由 `Value` 节点组成的有向无环图 (DAG),并跟踪前向求值流程。

操作指引

在自动微分中,计算图将每个数学运算表示为内部节点,操作数表示为输入边。运行图结构查看命令:

python3 projects/autograd/cli.py graph --preset neuron --format ascii

观察 $x_1 w_1 + x_2 w_2 + b$ 如何流入激活函数 $o = \tanh(n)$,并核对各前向计算节点数据。

自主演练与记录

对于表达式 $f(x, y) = (x \cdot y + 2)^2$,在 $x = 1.5, y = -2.0$ 处:
1. 拆解步骤构建中间节点:令 $u = x \cdot y$, $v = u + 2$, $f = v^2$。
2. 计算 $u, v, f$ 的数值。
3. 在下方演练区写出你预测的前向节点数值,先不运行代码。

独立掌握标准

学习者能够准确将标量算术拆解为带有父节点依赖 (`_prev`) 与标量数值 (`data`) 的 DAG 计算节点。

权威源资源与权利证明

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)

入选理由:

Clear, minimal scalar DAG implementation demonstrating fundamental autograd data structures.

来源前置条件:

Basic Python and calculus fundamentals

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

Local engine implementation in projects/autograd/engine.py and offline documentation.

对比候选资源与选型分析 (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 分钟

局部导数推导与基础算子微分表

独立掌握检查

前置要求

一元微积分基础(幂函数、指数、对数、双曲正切导数)。

学习目标

手算推导所有核心算子 (+, -, *, /, pow, exp, log, relu, tanh) 的局部偏导数,并设计 `_backward` 闭包。

操作指引

对每一个运算节点 $z = f(x, y)$,节点需要封装 _backward() 闭包,将上游传入梯度 (out.grad) 乘以局部导数 $\frac{\partial z}{\partial x}$。查阅 projects/autograd/docs/guide.zh-Hans.md 中的算子导数汇总表。

自主演练与记录

手算推导下列局部导数:
1. 乘法 $z = x \cdot y$:推导 $\frac{\partial z}{\partial x}$ 与 $\frac{\partial z}{\partial y}$。
2. 幂函数 $z = x^n$($n$ 为常数):推导 $\frac{\partial z}{\partial x}$。
3. 双曲正切 $z = \tanh(x)$:用 $z$ 自身表达 $\frac{\partial z}{\partial x}$。

独立掌握标准

学习者无需查表或 AI 提示,能独立写出所有核心算子的局部导数与反向更新语句。

权威源资源与权利证明

linking SIAM Copyright / Academic Linking Only No text extraction or distribution permitted

Evaluating Derivatives: Principles and Techniques of Algorithmic Differentiation

作者 / 机构: Andreas Griewank, Andrea Walther | 原始出处链接 ↗

对应章节 / 时间戳: Chapter 3: Reverse Mode and Computational Graphs

验证版本与日期: 2nd Edition, SIAM 2008 (Verified: 2026-10-30)

入选理由:

Detailed mathematical reference for algorithmic differentiation theory and complexity.

来源前置条件:

Multivariate calculus and linear algebra

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

Self-contained mathematical derivations in activity instructions and offline guide.

对比候选资源与选型分析 (2)
micrograd: A tiny scalar-valued autograd engine ↗ compared_alternative [MIT License]

Executable scalar reference implementation the learner later rebuilds; the textbook supplies the formal reverse-mode theory.

Broad survey taxonomy of the field; the textbook was selected for its rigorous treatment of reverse-mode complexity.

03 120 分钟

反向模式链式法则直觉与单神经元反向传播手算跟踪

Agent 辅助工程检查

前置要求

完成活动 31-01 与 31-02。

学习目标

理解多元微积分链式法则如何从目标损失反向传递至输入权重,并对单神经元前向反向进行完整手算跟踪。

操作指引

执行单神经元预设的前向与反向命令:

python3 projects/autograd/cli.py forward-backward --preset neuron

验证反向传播从 $o.\text{grad} = 1.0$ 开始,穿过 $n = x_1 w_1 + x_2 w_2 + b$,得出 $\frac{\partial o}{\partial w_1} = 1.0$ 与 $\frac{\partial o}{\partial x_1} = -1.5$。

自主演练与记录

已知 $o = \tanh(n)$,$n = 0.8814$($o \approx 0.7071$):
1. 计算 $\frac{\partial o}{\partial n} = 1 - o^2$。
2. 已知 $n = x_1 w_1 + x_2 w_2 + b$,其中 $x_1 = 2.0, w_1 = -3.0$,计算 $\frac{\partial o}{\partial x_1}$ 与 $\frac{\partial o}{\partial w_1}$。
3. 对比你的手动推导数值与 CLI 输出。

独立掌握标准

学习者能够穿越多层计算节点准确应用链式法则连乘,不遗漏任何中间缩放因子。

权威源资源与权利证明

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.

来源前置条件:

Python systems programming and multivariable calculus

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

Stand-alone projects/autograd engine.

对比候选资源与选型分析 (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 分钟

独立掌握检查:标量计算图手算推导与反向传播测验

独立掌握检查

前置要求

活动 31-01 至 31-03。

学习目标

在声明无 AI 辅助的条件下,验证独立推导标量计算图与链式法则反向传播的能力。

操作指引

请在无大语言模型、无自动代数计算器的独立条件下完成下列推导。在演练草稿区写下推导步骤与结果。

自主演练与记录

对于损失函数 $L = (x \cdot y + 2)^2$,在 $x = 1.5, y = -2.0$ 处:
计算精确的解析导数 $\frac{\partial L}{\partial x}$ 与 $\frac{\partial L}{\partial y}$。写明每一步链式法则代换。

客观知识测验:计算图与反向链式法则

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

声明辅助条件

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

1.

在反向模式自动微分中,遍历 DAG 之前,根损失节点(Loss)的梯度 loss.grad 必须初始化为多少?

2.

对于运算节点 $z = x \cdot y$,若当前 z.grad 为 $5.0$,且 $x = 2.0, y = -3.0$,则回传给输入 $x$ 的梯度贡献是多少?

3.

为什么在神经网络训练中,反向模式自动微分渐近复杂度显著优于前向模式?

作答应证与历史记录

    独立掌握标准

    独立推导得出 $\frac{\partial L}{\partial x} = 4.0$ 与 $\frac{\partial L}{\partial y} = -3.0$,并写明中间变量定义与偏导相乘步骤。

    权威源资源与权利证明

    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 Modified BSD

    PyTorch Documentation: Autograd Mechanics

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

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

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

    入选理由:

    Official reference for computational graph construction, backward pass execution, and in-place mutation pitfalls.

    来源前置条件:

    Basic understanding of computational graphs and tensors.

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

    The training loop mechanics and zero_grad requirements are illustrated with runnable Python code.

    对比候选资源与选型分析 (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.