数学基础 | Jacobian 是局部的「一个空间里的变化,怎样传到另一个空间」;SVD 找出这张线性映射最敏感的输入与输出方向;Hessian 描述 scalar objective 的 slope 怎样随参数继续变化。三者经常在同一篇论文里出现,但它们分析的未必是同一个矩阵,也未必活在同一个空间。| 2026-07
这篇教程面向一个很具体的阅读目标:看到论文里的 Jacobian spectrum、Hessian top eigenspace、low-rank update 或 directional curvature 时,能够先写出对象和 shape,再判断作者究竟测了什么。重点是 neural network、LLM training 与 post-training,不追求完整的 matrix calculus 推导。
0. 先把对象钉住
统一使用下面的记号:
这里 是模型的全部独立 parameters,shape 为 ; 是选定的 vector output,shape 为 ; 是最后用于训练或评测的 scalar objective,shape 为 。对 LLM, 可以是一个 token 的 vocabulary logits,也可以把多个 tokens、prompts 或 hidden states 摊成一个长向量。 取决于论文选择观察什么。
整篇可以先压成这张图:
是 的 Jacobian。它把一个 parameter perturbation 映射成 output change; 再把 维 output change 映射成一阶 scalar loss change。图里的 arrows 都是原函数在当前点的 local linearization,不是完整的 nonlinear forward pass。Hessian 最终描述这个 scalar signal 在 parameter space 中的二阶变化。
先记住四个对象:
| 对象 | Shape | 它回答的问题 |
|---|---|---|
| Gradient | scalar objective 对每个 parameter 的局部 slope 是什么 | |
| Jacobian | parameter perturbation 会怎样改变 vector output | |
| SVD | 取决于 | 一个 linear map 最敏感、最不敏感的方向是什么 |
| Hessian | gradient 怎样随 parameters 改变 |
阅读这些公式时按一个固定顺序:先问谁对谁求导,再写 shape,最后谈 spectrum、rank 或 geometry。
1. Derivative、gradient 与三类容易混淆的导数
1.1 Derivative 的核心是局部线性近似
对 scalar function ,如果当前点附近二阶可微,一元导数满足
、、 和 都是 scalar; 表示随 缩小得更快的二阶 remainder。这个式子不描述函数整体,只描述当前 附近走一小步时的 output change。导数是 local quantity;换一个位置,slope 通常也会变。
参数变成 vector 后,同一个思想仍然成立。若 scalar loss 在附近二阶可微:
与 gradient 的 shape 都是 ,内积 是 scalar。Gradient 收集了 loss 对全部 parameter coordinates 的一阶偏导;它指向当前 Euclidean coordinates 中上升最快的方向。这里 Euclidean 指直接使用当前坐标下的 length 与 inner product。
若训练是在最小化 loss,gradient descent 使用
与 update 都是 维 vector, 是 scalar learning rate。这个式子只用到一阶局部信息,并没有显式使用 Hessian。
Partial derivative 只问第 个 coordinate 单独变化时的 slope。Directional derivative 则允许所有 coordinates 按一个联合方向 一起变化:
是 scalar step, 是 维 perturbation,结果是 scalar slope。Gradient 可以一次回答所有方向,因为给定任意 后,只需做一次 inner product。
1.2 三类导数必须连同输入、输出一起说
神经网络论文中的「Jacobian」至少常指下面三种对象:
| 导数 | Shape | 常见用途 |
|---|---|---|
| Input Jacobian | input sensitivity、robustness | |
| Parameter Jacobian | training dynamics、function change | |
| Activation Jacobian | gradient propagation、residual path |
三者都叫 Jacobian,因为输出都是 vector;但它们的列分别对应 input coordinates、parameter coordinates 和 hidden-state coordinates。它们不是同一个矩阵,singular vectors 也不在同一个空间。
还有一个常被混进来的对象:
它是 scalar loss 对 parameters 的 gradient。把 scalar 看作一维 output 时,gradient 可以视为 Jacobian 的特殊写法;实践中通常把它写成 column vector,而 Jacobian 的每一行对应一个 output coordinate。
1.3 Backprop 不是把完整 Jacobian 存下来
设 ,再令 。Chain rule 给出
Shape 可以直接检查: 是 , 是 ,结果 是 。自然语言是:loss 先在 output space 给出「哪些 output changes 会使 loss 增大」,再由 把这个 sensitivity 拉回 parameter space。
这个乘法叫 vector-Jacobian product(VJP)。Backprop 无需显式构造可能大得离谱的 Jacobian;它沿 computation graph 反向计算 VJP:
u <- derivative of loss with respect to final output
for operation in reverse(computation_graph):
u <- operation.VJP(u)
return u # derivative of loss with respect to parameters这里每次 u 的 shape 都与当前 operation 的 output shape 相同;经过 VJP 后,它变成对该 operation inputs 的 sensitivity。标准 gradient descent 与 Jacobian 的关系就在这里,但「Jacobian」不等于「gradient descent」。前者是 local linear map,后者是消费 gradient 的 optimization rule。
2. 复杂 computation graph 怎样进入 Jacobian
如果一个函数由两步组成:
、、。Chain rule 写成
的 shape 是 , 是 ,乘积是 。矩阵乘法的中间维度 ,正对应 computation graph 中两步连接的 activation space。
Transformer 的 residual block 可以抽象成
其中 ,该 block 的 parameters 摊平后为 。对输入 activation 求导:
是 residual path 的 Jacobian, 是 attention、MLP、normalization 等变换共同产生的 Jacobian。Graph 中的分支没有因为参数被 flatten 而消失:串行路径对应 Jacobian multiplication,分支汇合对应 derivative addition,parameter sharing 则让同一 parameter coordinate 接收多个使用位置的贡献。
把所有 parameters 写成 ,只是给不同 tensors 里的 scalar entries 编号。计算图如何组合这些数字,仍由固定 architecture 决定。更完整的讨论见 参数空间与计算图。
3. SVD:一个 linear map 放大了哪些方向
3.1 SVD 不是另一种导数
SVD(Singular Value Decomposition)可以作用于任意 real matrix。对 ,设它的 rank 为 ,compact SVD 写成
,,。这里 rank 是 non-zero singular values 的数量;orthonormal 表示每个 vector 长度为 1,且彼此正交。 和 的 columns 分别是 left / right singular vectors, 的 diagonal entries 为 。
SVD 的方向含义集中在下面这个关系:
是输入侧方向, 是输出侧方向, 是 scalar amplification factor。沿 输入一个单位变化,linear map 会把它变成沿 、长度为 的输出变化。
因此
的 shape 是 , 的 shape 是 ;top right singular vector 是在当前 Euclidean norm 下让 output change 最大的单位 input direction。
3.2 SVD 放到 Jacobian 上,方向才有具体含义
对 parameter Jacobian :
这里 是联合 parameter direction,可以同时在许多 layers 上有非零分量; 是 output change pattern。 大,表示沿 的小幅 parameter move 会产生较大的一阶 output change。
这仍然不等于 loss 一定沿 变化很大。令 output-space loss gradient 为
把 SVD 代入 backprop:
Gradient 在第 个 parameter singular direction 上的分量为
所以 large 只说明网络对这个方向敏感。要形成 large loss gradient,output loss signal 还要与 对齐。Sensitivity 与 usefulness 不是同一件事。
若 ,则 位于这个 Jacobian 的 nullspace:在当前 checkpoint、当前 inputs 和当前 output definition 下,沿 的一阶 output change 为零。它不保证 finite step 后仍不变,也不保证换一批 prompts 或换一种输出后仍然不敏感。
3.3 Truncated SVD 与 low rank
保留前 个 singular components,可以得到 rank- approximation:
每个 outer product 的 shape 是 ,所以 与 shape 相同,但 rank 不超过 。如果后面的 singular values 很小, 能保留 的主要作用。
Outer product 把两个 vectors 组合成一个 rank-one matrix。论文报告 effective rank 时,还会用 threshold 或 cumulative singular-value energy 决定「保留多少项」。这个数字依赖定义、normalization 与被分解的矩阵,不表示剩余 parameters 没用。
3.4 论文说「做了 SVD」时,先问做在谁身上
| 被分解的矩阵 | Rows / columns 是什么 | Singular directions 的含义 |
|---|---|---|
| Weight matrix | output features / input features | 单层线性变换的 feature directions |
| Weight update | output features / input features | 该 layer 的 update 是否接近 low rank |
| Activation matrix | tokens or samples / features | 数据变化和 feature variation |
| Parameter Jacobian | outputs / all parameters | 全模型局部 function sensitivity |
这些谱不能直接互换。某一层的 rank 很低,不推出 full parameter Jacobian 低秩;global Hessian 只有少数大 eigenvalues,也不推出每层 weight update 都低秩。
LoRA 是最常见的例子。它直接参数化
其中 ,,,因此 。标准 LoRA 不是先训练完整 ,再做 SVD 截断;它从一开始就只优化两个 low-rank factors。这里的 rank 是单个 weight matrix 的 matrix rank,不是整个 LLM 的 intrinsic dimension。
Low rank、sparse 与 low-dimensional subspace
Low rank 说一个 matrix 能由少量 outer products 表示;sparse 说很多 coordinates 为零;low-dimensional subspace 说允许的全局 update 只需少量 basis directions。三者都在表达「结构少」,但数学对象不同,不能相互代换。
4. Hessian:gradient 怎样继续变化
4.1 Hessian 是 gradient 的 Jacobian
对 scalar objective :
Gradient 是 维 vector;再看它对 个 parameter coordinates 怎样变化,就得到 Hessian。这里的 smooth 是指所需导数在局部存在且连续,不是视觉上「曲线很圆滑」。若 二阶连续,mixed partial derivatives 相等, 是 symmetric matrix。
Hessian 最直接的用途是 local quadratic approximation:
,,而 与 都是 scalar。第一项描述 slope,第二项描述 slope 在这一步内怎样弯曲。这个近似只在 足够小时可信;走得太远,higher-order terms 会接管。
4.2 Directional curvature
对单位方向 ,,定义
是 维 vector, 才是 scalar directional curvature。它回答:沿 这一条参数方向观察,objective 的 slope 改变得多快。
因为 对称,可以做 eigendecomposition(EVD);对 real symmetric matrix,这也叫 spectral decomposition:
的 columns 是 orthonormal eigenvectors, 的 diagonal entries 是 signed eigenvalues。沿 eigenvector 方向:
,结果 是 scalar。对正在最小化的 loss:
- :沿 看起来像向上的碗;
- :沿 存在向下弯的方向;
- :二阶近似下较 flat,但 higher-order change 仍可能存在。
若不同 相差很大,local quadratic model 中的不同 directions 会表现出不同 time scales。High-curvature directions 对 step size 更敏感;weak directions 的 objective change 较慢。
4.3 Full Hessian 不是逐层 Hessian 的简单拼接
把 parameters 按 layers 或 tensors 分成 ,其中 ,则 Hessian 的 block 是
Diagonal block 描述同一 parameter group 内 gradient 的变化;off-diagonal block 描述改变 group 后,group 的 gradient 怎样变化。Full Hessian 包含这些 cross-layer couplings。丢掉 off-diagonal blocks 得到的是一种便宜的 block approximation,不是原矩阵的等价重排。
一个两层 linear network 已经会产生这种 coupling。令
其中 、、。同时扰动两层:
四项都是 scalar。最后一项同时含 与 ,是二阶的 cross-layer term:两个 parameter blocks 的 coordinates 没有重叠,network function 却通过 matrix product 把它们耦合起来。
因此,global eigenvector 可以在许多 parameter blocks 上同时有非零分量。论文说「只有少量 stiff directions」时,不能自动翻译成「只有少数 layers 重要」。
4.4 Exact Hessian 不一定 PSD
若 是一个二阶可微 loss 的 local minimum,则
是必要条件。 表示任意 都满足 。但普通 training checkpoint 未必是 gradient 为零的 stationary point,更未必是 local minimum;non-convex neural network 的 exact parameter Hessian 可以同时有 positive、negative 和 near-zero eigenvalues。
Reward maximization 还会反转符号。若 是 reward 的 local maximum,则通常讨论
。很多论文用 PSD 的 表示「curvature magnitude」,所以正文中出现 positive curvature eigenvalues,不代表 reward Hessian 本身是 PSD。读论文时要检查作者分析的是 、,还是 。
Hessian 不自动构成 distance metric
Hessian 描述 chosen objective 在当前 coordinates 中的二阶变化。它可能 indefinite,而且 parameter rescaling 会改变 eigenvalues;因此它不能不加条件地定义 distance,也不是 coordinate-free 的模型几何。详见 参数化与几何的边界。
4.5 Hessian 应用 EVD,Jacobian 通常应用 SVD
| 分解 | 适合的对象 | 谱值 | 保留什么信息 |
|---|---|---|---|
| SVD | rectangular Jacobian、weight 或 activation matrix | 输入方向到输出方向的 amplification | |
| EVD | symmetric Hessian | 可正可负 | signed curvature |
对 indefinite Hessian 做 SVD,会把 eigenvalue 变成 singular value ,从而丢掉向上弯还是向下弯的符号。只有当 时,Hessian 的 singular values 才与 eigenvalues 一致。
5. Jacobian、SVD 与 Hessian 怎样接起来
考虑 squared error:
,所以 是 scalar。记 residual ,parameter Jacobian 。Exact Hessian 可以写成
两项的 shape 都是 。第一项来自 output loss curvature,必然 PSD;第二项来自 network output 对 parameters 的非线性,可以带来 negative curvature。
如果 residual 为零,或者只保留 network 的 local linearization,第二项消失:
若 ,则
的 shape 是 ;它的 eigenvectors 是 的 right singular vectors ,eigenvalues 是 。这给出 SVD 与 curvature 的直接联系。
但这不是一个可以无条件使用的恒等式。一般 loss 的 exact Hessian 还保留 model-curvature term。论文中常用的 Generalized Gauss-Newton(GGN)只保留 PSD 的第一部分:
其中 是 loss 对 model outputs 的 Hessian。若 ,则 。
若定义 loss-weighted Jacobian ,则 。所以 GGN 的 eigendirections 是 的 right singular directions,eigenvalues 是相应 singular values 的平方。
Fisher 可以写成 per-sample score outer product 的 expectation:
每个 都是 PSD,因此 expectation 仍是 PSD,但可以 singular,也就是某些 directions 的 quadratic form 为零。Fisher 局部衡量 parameter change 对 predictive distribution 的 KL change;它对谁取 expectation、何时与 GGN 重合,需要额外条件。三类矩阵的边界如下:
| 矩阵 | 是否保证 PSD | 它在描述什么 |
|---|---|---|
| Exact Hessian | 否 | chosen scalar objective 的真实二阶变化 |
| GGN | output loss convex 时是 | local-linearized network 上的 loss curvature |
| Fisher | 是,但可 singular | predictive distribution 的局部 KL sensitivity |
一些论文宽泛地把 GGN 或 Fisher 也叫 curvature matrix。这在语境明确时没有问题,但不能把它们与 exact Hessian 当成同一个对象。
6. 两个 LLM training cases
Case A:Next-token cross-entropy
对一个 token,令 logits ,probability vector ,target token 的 one-hot vector 为 。Cross-entropy 对 logits 的 gradient 是
设 logit parameter Jacobian 为 。Backprop 得到
是 ,所以 vocabulary-space error signal 被拉回整个 parameter space。实际 LLM 不会存下 Jacobian,只计算这个 VJP。
Cross-entropy 对 logits 的 Hessian 是
是 PSD;直觉上,它是 categorical distribution 的 covariance matrix。于是 GGN
也是 PSD。但 full parameter exact Hessian 还包含 logits 对 parameters 的二阶导数,因此一般仍不保证 PSD。如果只训练固定 features 上的 linear head,logits 对 head parameters 是 affine,也就是「linear map 加 bias」,额外的 model-curvature term 才会消失。
Case B:Reward Model / DPO 的 preference loss
先看 pairwise reward model。对同一个 prompt,preferred response 和 rejected response 的 reward margin 为
Sigmoid 把 scalar margin 映射到 ,preference loss 取 logistic form:
记 margin gradient 。它的 exact Hessian 可以分成
两项都是 。第一项是一个 outer product,rank 至多为 1 且 PSD;第二项来自 reward margin 对 parameters 的非线性,可以使 exact Hessian indefinite。
这个例子解释了一个常见误判:loss 对 scalar margin 是 convex 的,不代表 loss 对 neural-network parameters 也是 convex 的。 对 batch size 为 的 preference data,第一类 GGN terms 相加后的 rank 至多为 ;包含第二项的 exact Hessian 不受这个 rank 上界约束。因此 mini-batch GGN 看起来 low rank,部分原因可能只是 output dimension 与 batch size 有限,不能据此识别模型是否具有全局低维结构。
DPO 把 换成 preferred / rejected responses 的 log-policy-ratio margin:
每个 sequence log-probability 都是 scalar, 也是 scalar,reference policy 固定不更新。DPO 的外层仍是 ,所以上面的 Jacobian/Hessian 分解保持一致。这里讨论的是数学结构,不等于声称所有 RLHF 或 preference optimization 方法拥有相同的 curvature spectrum。
7. 大模型里怎样使用这些巨大矩阵
对 billion-parameter model,显式 Jacobian 需要 entries,显式 Hessian 需要 entries,通常都不可行。实际计算依赖 matrix-vector products:
| 运算 | Input → output shape | 直觉 |
|---|---|---|
| JVP | 沿 parameter direction 走一步,outputs 怎样变 | |
| VJP | 把 output sensitivity 拉回 parameters;backprop 的核心 | |
| HVP | 沿 移动时,完整 gradient 怎样变化 | |
| Quadratic form | 沿 的 signed directional curvature |
HVP 可以写成
这里把 视为固定方向。括号内是 scalar directional derivative,再对 求 gradient,得到 维 HVP。伪代码只需要两次 derivative operations:
g <- gradient(L(theta), theta) # shape D
s <- dot(g, v) # scalar
Hv <- gradient(s, theta) # shape DPower iteration、Lanczos 和 SLQ 是反复查询 HVP 的 numerical algorithms:前者可估计 dominant-magnitude eigenmode,后两者可估计 extreme eigenvalues 或 spectral density,而不实例化 Hessian。类似地,若要估计 Jacobian 的 top singular values,可以交替计算 与 ,相当于使用 的 matrix-vector product。
还要区分
前者是 HVP vector,后者是 directional-curvature scalar。用三个 function evaluations 的 finite difference 可以估计后者:
分子、分母和结果都是 scalar。这个 probe 没有直接恢复完整 ,更没有恢复 Hessian matrix。
8. 回到 weight-space search 与 BoD
对 scalar reward ,沿随机方向 扰动:
和 都是 scalar;前者是一阶 reward-gradient projection,后者是二阶 directional curvature。Positive reward tail 可能来自其中任一项,也可能受到 higher-order structure 与 evaluation noise 影响。
BoD 假设 reward geometry 只有少量 stiff directions,而大量 parameter directions 较 weak。在 local reward maximum 附近的 quadratic abstraction 中,对 PSD curvature matrix 做谱分解:
都是 ;top eigenvectors 定义 curvature-active subspace。这些 projections 保留了主导 quadratic curvature 的 coordinates,却不单独决定 reward 是否改善。改进还取决于 、当前位置相对 optimum 的方向、step size 和 higher-order terms;在 exact local maximum 上,任何非零 quadratic move 反而都会降低 reward。
这个解释还有四个限定:
- 是 reward maximum 附近的 ,不是 reward Hessian 本身。
- 一个 top eigenvector 可以同时在所有 parameter tensors 上有分量;low active dimension 不等于只有少数 layers 有用。
- Gaussian perturbation 能否命中 positive tail,还取决于 noise scale、normalization、checkpoint、task 和 evaluation noise。
- Best-of- 能采到好模型,不单独证明 positive tail 是由 low-rank Hessian 造成的。
因此,BoD、ES、RandOpt 的结论只适用于指定的 checkpoint、parameterization、noise distribution、task 与 evaluation protocol。更完整的算法与证据边界见 无反向传播的权重空间后训练。
9. 阅读论文时怎样拆公式
看到 Jacobian,依次问:
- 谁是 input,谁是 output?
- 它是对 input、activation 还是 parameters 求导?
- Rows 和 columns 分别对应什么,shape 是多少?
- 是单个 prompt 的 Jacobian,还是把 batch、tokens、logits stack 起来?
- 作者构造了完整矩阵,还是只用了 JVP / VJP?
看到 SVD,依次问:
- 被分解的是 weight、weight update、activation 还是 Jacobian?
- Left / right singular vectors 分别活在哪个空间?
- Singular values 是否做过 normalization?
Effective rank使用什么 threshold 或 energy definition?- Low rank 是否被误写成 sparse、few layers 或 global low dimension?
看到 Hessian,依次问:
- Scalar objective 是 training loss、reward、KL,还是经过 sampling average 的 smoothed objective?
- 矩阵是 exact Hessian、、GGN 还是 Fisher?
- Spectrum 是否保留 eigenvalue 的正负号?
- 对谁取 expectation:single example、mini-batch、dataset 还是 model distribution?
- 作者使用 full parameters、LoRA parameters,还是 layerwise block approximation?
- 结果依赖哪个 checkpoint、parameterization 与 perturbation scale?
10. 最后的 mental model
Jacobian 是局部 linear map;SVD 可以拆出它的 sensitivity directions;Hessian 则是 scalar objective 的二阶局部模型。Backprop 用 VJP 把 output-space signal 拉回 parameters,second-order analysis 用 HVP 探测 curvature,large-scale spectral methods 再利用这些 matrix-free products 估计重要 directions。
阅读相关工作时,始终记录 operator 的 input space、output space、shape、metric 与 objective。对 weight matrix 做 SVD、对 parameter Jacobian 做 SVD、对 full-objective Hessian 做 eigendecomposition,得到的是三套不同的方向;它们之间的关系需要公式或实验建立,不能仅凭「都是低维结构」相互替代。
延伸阅读
- Deep Learning Book:Linear Algebra:eigendecomposition、SVD 与 pseudoinverse。
- Deep Learning Book:Numerical Computation:gradient、Jacobian、Hessian 与 optimization 的基础关系。
- Practical Gauss-Newton:exact Hessian 与 GGN 的分解及 PSD 边界。
- Hessian Eigenvalue Density:不用构造完整 Hessian,估计 neural-network Hessian spectrum。
- LoRA:逐层 weight update 的 low-rank parameterization。
- InstructGPT:pairwise reward-model loss 与 RLHF training pipeline。
- DPO:preference loss 在 LLM post-training 中的具体形式。
- 参数空间与计算图:为什么复杂 graph 仍能坐标化为一个 parameter vector,以及这种坐标化省略了什么。
- 有限学习率的隐式梯度正则:Hessian 怎样进入离散 gradient descent 的 modified dynamics。