# 自动微分

**自动微分**（Automatic differentiation，AD），又称算法微分或简称“自动求导”，是一类通过对程序所执行的基本运算序列应用微积分链式法则，从而高效、精确地求取以计算机程序表示的数值函数导数的技术。<sup>[1](https://jmlr.csail.mit.edu/papers/v18/17-468.html)</sup> 它与训练神经网络所用的梯度计算方法——反向传播——相似，但比后者更为一般。<sup>[1](https://jmlr.csail.mit.edu/papers/v18/17-468.html)</sup><sup> • </sup><sup>[2](https://arxiv.org/abs/1404.7456)</sup>

| 关键事实 | 详情 |
|---|---|
| 计算内容 | 对以计算机模型给出的函数求导，达到工作精度，所用算术运算次数至多比原程序多一个小常数倍<sup>[3](https://wires.onlinelibrary.wiley.com/doi/10.1002/widm.1334)</sup><sup> • </sup><sup>[4](https://learn.fmi.uni-sofia.bg/mod/resource/view.php?id=217068)</sup> |
| 两种主要模式 | 前向模式计算雅可比-向量积（JVP）；反向模式计算向量-雅可比积（VJP），后者是反向传播的基础<sup>[5](https://docs.jax.dev/en/latest/notebooks/autodiff_cookbook.html?highlight=hvp)</sup> |
| 前向模式开销 | 浮点运算量约为函数求值本身的 3 倍，内存开销与计算深度无关<sup>[5](https://docs.jax.dev/en/latest/notebooks/autodiff_cookbook.html?highlight=hvp)</sup> |
| 实现方式 | 运算符重载与源变换是两种主要实现途径<sup>[6](https://research.google/pubs/automatic-differentiation-in-ml-where-we-are-and-where-we-should-be-going/)</sup> |
| 框架支持 | PyTorch（`torch.autograd`）、TensorFlow（`tf.GradientTape`）和 JAX（`jax.grad`、`jax.jvp`）将自动微分内置于标准机器学习工作流中<sup>[8](https://docs.pytorch.org/docs/main/autograd.html)</sup><sup> • </sup><sup>[9](https://www.tensorflow.org/guide/autodiff?authuser=1)</sup><sup> • </sup><sup>[10](https://github.com/jax-ml/jax)</sup> |

## 工作原理

计算一个值的程序，是通过基本运算的复合来完成的：算术运算、指数函数、三角函数等等。自动微分将函数分解为这一系列基本步骤，用已知规则求出每一步的导数，再用链式法则将结果组合起来。中间值以及产生这些中间值的指令被记录在一种称为温格特列表（Wengert list）或“磁带”的数据结构中。<sup>[4](https://learn.fmi.uni-sofia.bg/mod/resource/view.php?id=217068)</sup><sup> • </sup><sup>[11](https://leimao.github.io/downloads/article/2022-02-21-Automatic-Differentiation/AD1.pdf)</sup> 所得结果是导数在特定输入处的数值，而非导数的符号公式；这正是它与符号求导的主要区别——后者将函数作为闭式表达式进行操作。<sup>[12](https://jax.quantecon.org/_pdf/quantecon-jax.pdf)</sup><sup> • </sup><sup>[13](https://edepot.wur.nl/196517)</sup>

**前向模式。** 给定函数 \( f : \mathbb{R}^n \to \mathbb{R}^m \)，前向模式计算雅可比-向量积 \( (x, v) \mapsto \partial f(x)\, v \)，在计算过程中让每个原始值都携带一个切向量。每个基本运算执行一条规则，既对原始值求值，又将其线性化应用于切向量。由于导数在计算推进过程中即被求出，无需存储任何东西供后续使用，因此内存开销与计算深度无关，浮点开销约为原始求值的 3 倍：一份用于原始运算，一份用于其线性化，一份用于将线性化运算作用于切向量。<sup>[5](https://docs.jax.dev/en/latest/notebooks/autodiff_cookbook.html?highlight=hvp)</sup> 将一个 JVP 作用于一个独热切向量，可揭示雅可比矩阵的一列，因此完整的雅可比矩阵是逐列构建的，每一列的开销大致相当于一次函数求值。这对于输出多于输入的“高”雅可比矩阵是高效的，而对“宽”雅可比矩阵则效率低下。<sup>[5](https://docs.jax.dev/en/latest/notebooks/autodiff_cookbook.html?highlight=hvp)</sup>

**反向模式。** 反向模式则计算向量-雅可比积 \( (x, v) \mapsto \partial f(x)^{\mathsf{T}} v \)，逐行构建雅可比矩阵。计算先正向运行一次以记录中间值，然后反向遍历计算图，将导数信息从输出传播回输入。<sup>[5](https://docs.jax.dev/en/latest/notebooks/autodiff_cookbook.html?highlight=hvp)</sup><sup> • </sup><sup>[14](https://papers.neurips.cc/paper_files/paper/2024/file/06cbd2e81dfbd3bb4cb0abce95b32584-Paper-Conference.pdf)</sup> 对于含 \( n \) 个参数的标量损失函数，雅可比矩阵就是 \( 1 \times n \) 的梯度向量，反向模式无论 \( n \) 多大，都能以大约一次函数求值的时间获得它。这就是反向模式在机器学习训练中占主导地位的原因——那里的 \( n \) 可达数百万乃至数十亿——也是反向传播本质上是应用于神经网络的反向模式自动微分的原因。<sup>[5](https://docs.jax.dev/en/latest/notebooks/autodiff_cookbook.html?highlight=hvp)</sup><sup> • </sup><sup>[2](https://arxiv.org/abs/1404.7456)</sup>

两种模式可以复合。在 JAX 中，海森矩阵通过复合前向模式与反向模式的雅可比计算获得，即 `jacfwd(jacrev(f))`，而前向套反向通常是这种复合最有效的安排。<sup>[5](https://docs.jax.dev/en/latest/notebooks/autodiff_cookbook.html?highlight=hvp)</sup>

## 历史

关于该过程的第一篇文章通常归功于通用电气公司的 R. E. 温格特（R. E. Wengert），其《一个简单的自动导数求值程序》发表于 1964 年 8 月的《美国计算机学会通讯》第 7 卷，第 463 至 464 页。温格特的方法通过引入中间变量，将函数分解为一系列基本函数步骤，并配以一个子程序库对每一步求值并求导，从而以数值方式计算导数，而无需推导解析表达式。<sup>[7](https://cir.nii.ac.jp/crid/1360294647096439936)</sup><sup> • </sup><sup>[15](https://users.fmi.uni-jena.de/~autodiff/?module=Publications&search%5Bauthor%5D=Wengert&submenu=list+publications)</sup> 反向模式的出现早于机器学习中所用的误差反向传播方法，并且是其推广。<sup>[2](https://arxiv.org/abs/1404.7456)</sup>

数十年来，这一领域规模一直很小，且与机器学习相互隔绝。阿蒂利姆·居内什·拜丁（Atilim Gunes Baydin）、巴拉克·A. 珀尔马特（Barak A. Pearlmutter）、阿列克谢·安德烈耶维奇·拉杜尔（Alexey Andreyevich Radul）和杰弗里·马克·西斯金德（Jeffrey Mark Siskind）在 2018 年的一篇综述中指出，直到不久前，这两个领域基本上互不知晓，某些情况下还各自独立地发现了对方的成果；通用自动微分在机器学习工具箱中是缺失的，而随着它以“动态计算图”和“可微编程”之名被采纳，这一局面正在改变。<sup>[1](https://jmlr.csail.mit.edu/papers/v18/17-468.html)</sup>

## 实现方式

两种主要的实现技术是运算符重载——数值类型携带导数信息，每个重载运算应用相应的求导规则——和源变换——工具将程序本身改写为同时计算导数的程序。<sup>[6](https://research.google/pubs/automatic-differentiation-in-ml-where-we-are-and-where-we-should-be-going/)</sup> 除此之外，文献还涵盖高阶导数、稀疏性利用、检查点、跨模式以及迭代过程的微分等主题。<sup>[3](https://wires.onlinelibrary.wiley.com/doi/10.1002/widm.1334)</sup>

基于编译器的自动微分将这些思想推广到任意代码。Enzyme 是 LLVM 框架的一个编译器插件，它在 LLVM 中间表示上为可静态分析的程序合成梯度，为用 C、C++、Fortran、Julia、Rust、Swift、MLIR 以及其他编译器以 LLVM 为目标的语言编写的程序提供原生自动微分。由于它是在已优化的中间表示上执行自动微分，在包含微软 ADBench 的机器学习基准测试套件上，它比在未优化中间表示上执行自动微分取得了 4.5 倍的几何平均加速比。<sup>[16](https://arxiv.org/abs/2010.01709)</sup>

## 机器学习框架中的自动微分

机器学习框架将自动微分作为核心特性提供。PyTorch 的 `torch.autograd` 以最少的代码改动实现任意标量值函数的自动微分：需要梯度的张量用 `requires_grad=True` 声明，支持范围涵盖浮点张量类型（half、float、double 和 bfloat16）和复数类型（cfloat 和 cdouble）。其前向模式 API 标记为测试版。<sup>[8](https://docs.pytorch.org/docs/main/autograd.html)</sup> TensorFlow 提供 `tf.GradientTape` API，它记录运算，以便日后计算某次计算相对于输入（通常是 `tf.Variable` 对象）的梯度。<sup>[9](https://www.tensorflow.org/guide/autodiff?authuser=1)</sup>

JAX 将微分视为若干可复合函数变换之一。`jax.grad` 计算反向模式梯度，`jax.jvp` 提供前向模式微分，向量-雅可比积（VJP）提供反向模式微分，两者可以任意复合至任意阶。<sup>[19](https://docs.jax.dev/en/latest/jacobian-vector-products.html)</sup> JAX 可以穿过循环、分支、递归和闭包进行微分，因此可以对包含普通控制流的 Python 函数求梯度，例如用 `if` 语句定义的绝对值函数。<sup>[10](https://github.com/jax-ml/jax)</sup>

## 陷阱

自动微分计算的是程序字面所写内容的精确导数，这并不总意味着目标数学函数的精确导数。程序通过浮点运算和其他近似手段实现的是对真实函数的近似，即便两者产生的值非常接近，在某些或全部输入上它们的导数也可能相差甚远。自动微分还在选定的抽象层次上运作，假定该层次上的每个算子都精确计算了一个可微函数，而其导数算子又精确计算了该函数的导数。<sup>[17](https://wires.onlinelibrary.wiley.com/doi/10.1002/widm.1555)</sup>

彼得·许克尔海姆（Peter Hückelheim）及其同事在 2024 年提出的一种分类法，对混沌系统、时间平均、离散化、不动点循环、查找表、线性求解器和概率程序所例示的问题用法进行了归类，并综述了针对这些情形的调试技术。某些失效模式是常见自动微分方法所固有的，会在多种工具和输入语言上系统性地产生令人困惑的、或按某种标准衡量不正确的导数，这使得在不理解应用本身和自动微分的情况下调试变得困难。<sup>[17](https://wires.onlinelibrary.wiley.com/doi/10.1002/widm.1555)</sup>

## 应用

导数求值是优化、参数辨识、灵敏度分析、不确定性量化、非线性方程求解和微分方程积分中的基本构件。<sup>[3](https://wires.onlinelibrary.wiley.com/doi/10.1002/widm.1334)</sup> 自动微分是一个成熟的领域，在计算流体力学、大气科学和工程设计优化中都有应用，导数在模型验证的灵敏度分析、数据同化等反问题以及仿真参数选择的设计优化中起着核心作用。<sup>[1](https://jmlr.csail.mit.edu/papers/v18/17-468.html)</sup><sup> • </sup><sup>[18](https://epubs.siam.org/doi/book/10.1137/1.9780898717761)</sup> 在机器学习中，梯度和海森矩阵无处不在，自动微分是梯度训练、贝叶斯推断、不确定性量化和概率编程的基础。<sup>[1](https://jmlr.csail.mit.edu/papers/v18/17-468.html)</sup><sup> • </sup><sup>[16](https://arxiv.org/abs/2010.01709)</sup>

## References

1. [Atilim Gunes Baydin, Barak A. Pearlmutter, Alexey Andreyevich Radul, Jeffrey Mark Siskind (2018). Automatic Differentiation in Machine Learning: a Survey. Journal of Machine Learning Research 18(153):1−43.](https://jmlr.csail.mit.edu/papers/v18/17-468.html)
2. [Automatic Differentiation of Algorithms for Machine Learning, arXiv:1404.7456](https://arxiv.org/abs/1404.7456)
3. [Assefaw H. Gebremedhin, Andrea Walther (2020). An introduction to algorithmic differentiation. WIREs Data Mining and Knowledge Discovery.](https://wires.onlinelibrary.wiley.com/doi/10.1002/widm.1334)
4. [Automatic differentiation overview, Faculty of Mathematics and Informatics, Sofia University](https://learn.fmi.uni-sofia.bg/mod/resource/view.php?id=217068)
5. [The Autodiff Cookbook, JAX documentation](https://docs.jax.dev/en/latest/notebooks/autodiff_cookbook.html?highlight=hvp)
6. [Bart van Merriënboer, Olivier Breuleux, Arnaud Bergeron, Pascal Lamblin (2018). Automatic differentiation in ML: Where we are and where we should be going. NeurIPS.](https://research.google/pubs/automatic-differentiation-in-ml-where-we-are-and-where-we-should-be-going/)
7. [R. E. Wengert (1964). A simple automatic derivative evaluation program. Communications of the ACM 7(8):463-464, CiNii Research record.](https://cir.nii.ac.jp/crid/1360294647096439936)
8. [Automatic differentiation package - torch.autograd, PyTorch documentation](https://docs.pytorch.org/docs/main/autograd.html)
9. [Introduction to gradients and automatic differentiation, TensorFlow Core](https://www.tensorflow.org/guide/autodiff?authuser=1)
10. [JAX: Composable transformations of Python+NumPy programs, GitHub](https://github.com/jax-ml/jax)
11. [Lei Mao (2022). Automatic Differentiation, notes PDF](https://leimao.github.io/downloads/article/2022-02-21-Automatic-Differentiation/AD1.pdf)
12. [Quantitative Economics with Python using JAX](https://jax.quantecon.org/_pdf/quantecon-jax.pdf)
13. [Automatic Differentiation Algorithms, Wageningen University eDepot](https://edepot.wur.nl/196517)
14. [Optimizing Automatic Differentiation, NeurIPS 2024](https://papers.neurips.cc/paper_files/paper/2024/file/06cbd2e81dfbd3bb4cb0abce95b32584-Paper-Conference.pdf)
15. [www.Autodiff.org publication record: Wengert 1964](https://users.fmi.uni-jena.de/~autodiff/?module=Publications&search%5Bauthor%5D=Wengert&submenu=list+publications)
16. [William S. Moses, Valentin Churavy (2020). Instead of Rewriting Foreign Code for Machine Learning, Automatically Synthesize Fast Gradients. arXiv:2010.01709.](https://arxiv.org/abs/2010.01709)
17. [Peter Hückelheim and colleagues (2024). A taxonomy of automatic differentiation pitfalls. WIREs Data Mining and Knowledge Discovery.](https://wires.onlinelibrary.wiley.com/doi/10.1002/widm.1555)
18. [Griewank, Walther. Evaluating Derivatives: Principles and Techniques of Algorithmic Differentiation, SIAM](https://epubs.siam.org/doi/book/10.1137/1.9780898717761)
19. [Forward- and reverse-mode autodiff in JAX, JAX documentation](https://docs.jax.dev/en/latest/jacobian-vector-products.html)

---
*Initially written Sep 17, 2026 · Reviewed: Sep 30, 2026; Oct 15, 2026 · Edited: Sep 30, 2026; Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 物质世界与数学 › 数学与统计学 › 数学分析与数学模型 › Differential calculus and derivatives

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/automatic-differentiation

English original: https://www.edgepedia.org/automatic-differentiation
