梯度
多元函数的梯度(gradient)是一个列向量, 表示该函数对每个变量的偏导数.
函数\(f(\mathbf{x})=f(x_1,x_2,x_3,...,x_n)\),\(\mathbf{x}\)是一个\(n\)维向量\(\mathbf{x}=[x_1,x_2,\ldots,x_n]^\top\)
其梯度为:
\[\nabla_{\mathbf{x}} f(\mathbf{x}) = \bigg[\frac{\partial f(\mathbf{x})}{\partial x_1}, \frac{\partial f(\mathbf{x})}{\partial x_2}, \ldots, \frac{\partial f(\mathbf{x})}{\partial x_n}\bigg]^\top,\]
对于矩阵\(\mathbf{A}\), 关于其梯度, 有如下等式:
\[ \nabla_{\mathbf{x}} \mathbf{A} \mathbf{x} = \mathbf{A}^\top \\ \nabla_{\mathbf{x}} \mathbf{x}^\top \mathbf{A} = \mathbf{A} \\ \nabla_{\mathbf{x}} \mathbf{x}^\top \mathbf{A} \mathbf{x} = (\mathbf{A} + \mathbf{A}^\top)\mathbf{x} \\ \nabla_{\mathbf{x}} \|\mathbf{x} \|^2 = \nabla_{\mathbf{x}} \mathbf{x}^\top \mathbf{x} = 2\mathbf{x} \\ \nabla_{\mathbf{X}} \|\mathbf{X} \|_F^2 = 2\mathbf{X} \]
计算过程
以上等式通过将矩阵写成元素然后手动做加法乘法以及求导运算均可以得到证明。
\(\nabla_{\mathbf{x}} \mathbf{A} \mathbf{x} = \mathbf{A}^\top\)
\[ \begin{aligned} \nabla_{\mathbf{x}} \mathbf{A} \mathbf{x} &= \nabla_{\mathbf{x}} \begin{bmatrix} a_{11} & a_{12} & ... & a_{1n} \\ a_{21} & a_{22} & ... & a_{2n} \\ \vdots & \vdots & ... & \vdots \\ a_{m1} & a_{m2} & ... & a_{mn} \\ \end{bmatrix} \begin{bmatrix} x_1 \\ x_2 \\ ... \\ x_n \end{bmatrix} \\ &= \nabla_{\mathbf{x}} \begin{bmatrix} \sum a_{1i} x_i \\ \sum a_{2i} x_i \\ \vdots \\ \sum a_{mi} x_i \\ \end{bmatrix} \\ &= \begin{bmatrix} a_{11} & a_{21} & ... & a_{m1} \\ a_{12} & a_{22} & ... & a_{m2} \\ \vdots & \vdots & ... & \vdots \\ a_{1n} & a_{2n} & ... & a_{mn} \\ \end{bmatrix} \\ &= \mathbf{A}^{\top} \end{aligned} \]
备注:为什么对列向量求梯度能得到矩阵?
直觉上来看, 列向量的每个元素都是一个关于\(\mathbf{x}\)的多项式, 而多项式的梯度是列向量, 那么含有\(n\)个变量元的\(m\)行列向量求梯度, 应该是\(mn\)行\(1\)列的列向量.事实上得到的是一个\(m \times n\)的矩阵.
这是因为 向量值函数的梯度并非标量函数梯度的简单堆叠, 而是标量函数梯度的自然推广:
标量函数\(f\)的梯度\(\nabla f\), 表示\(f\)在点\(\mathbf{x}\)处的最速上升方向.
而对于向量函数\(\mathbf{f}: \mathbb{R}^n \to \mathbb{R}^m\), 其梯度对应最佳线性逼近的概念: $\(\mathbf{f}(\mathbf{x}) \approx \mathbf{f}(\mathbf{x}_0) + J \cdot (\mathbf{x} - \mathbf{x}_0)\)$ 要使上式的维度成立(右侧结果必须是 \(m\) 维列向量),\(J\) 必须是 \(m \times n\) 的矩阵。 因此纵向堆叠(得到 \(mn \times 1\) 的向量)从数学含义上就解释不通——它无法参与上述线性逼近的运算。Jacobian 矩阵的形式并非约定,而是由最佳线性逼近的要求唯一确定的。
\(\nabla_{\mathbf{x}} \mathbf{x}^\top \mathbf{A} = \mathbf{A}\)
\[ \begin{aligned} \nabla_{\mathbf{x}} \mathbf{x}^\top \mathbf{A} &= \nabla_{\mathbf{x}} \begin{bmatrix} x_1 & x_2 & ... & x_n \end{bmatrix} \begin{bmatrix} a_{11} & a_{12} & ... & a_{1m} \\ a_{21} & a_{22} & ... & a_{2m} \\ \vdots & \vdots & ... & \vdots \\ a_{n1} & a_{n2} & ... & a_{nm} \\ \end{bmatrix} \\ &= \nabla_{\mathbf{x}} \begin{bmatrix} \sum_i x_i a_{i1} & \sum_i x_i a_{i2} & ... & \sum_i x_i a_{im} \end{bmatrix} \\ &= \begin{bmatrix} a_{11} & a_{12} & ... & a_{1m} \\ a_{21} & a_{22} & ... & a_{2m} \\ \vdots & \vdots & ... & \vdots \\ a_{n1} & a_{n2} & ... & a_{nm} \\ \end{bmatrix} \\ &= \mathbf{A} \end{aligned} \]
\(\nabla_{\mathbf{x}} \mathbf{x}^\top \mathbf{A} \mathbf{x} = (\mathbf{A} + \mathbf{A}^\top)\mathbf{x}\)
对第 \(k\) 个分量求偏导:
\[ \begin{aligned} \nabla_{\mathbf{x}} \mathbf{x}^\top \mathbf{A} \mathbf{x} &= \nabla_{\mathbf{x}} \begin{bmatrix} x_1 & x_2 & \cdots & x_n \end{bmatrix} \begin{bmatrix} a_{11} & a_{12} & \cdots & a_{1n} \\ a_{21} & a_{22} & \cdots & a_{2n} \\ \vdots & \vdots & \ddots & \vdots \\ a_{n1} & a_{n2} & \cdots & a_{nn} \end{bmatrix} \begin{bmatrix} x_1 \\ x_2 \\ \vdots \\ x_n \end{bmatrix} \\ &= \nabla_{\mathbf{x}} \begin{bmatrix} \sum_i x_i a_{i1} & \sum_i x_i a_{i2} & \cdots & \sum_i x_i a_{in} \end{bmatrix} \begin{bmatrix} x_1 \\ x_2 \\ \vdots \\ x_n \end{bmatrix} \\ &= \nabla_{\mathbf{x}} \sum_{i,j} a_{ij} x_i x_j \\ \frac{\partial}{\partial x_k} \sum_{i,j} a_{ij} x_i x_j &= \frac{\partial}{\partial x_k} \left( \sum_j a_{kj} x_k x_j + \sum_{i \neq k} a_{ik} x_i x_k + \text{其余项} \right) \\ &= \sum_j a_{kj} x_j + \sum_i a_{ik} x_i \\ &= (\mathbf{A}\mathbf{x})_k + (\mathbf{A}^\top \mathbf{x})_k \\ &= ((\mathbf{A} + \mathbf{A}^\top)\mathbf{x})_k \\ \therefore \nabla_{\mathbf{x}} \mathbf{x}^\top \mathbf{A} \mathbf{x} &= (\mathbf{A} + \mathbf{A}^\top)\mathbf{x} \end{aligned} \]
\(\nabla_{\mathbf{x}} \|\mathbf{x}\|^2 = 2\mathbf{x}\)
\[ \begin{aligned} \nabla_{\mathbf{x}} \|\mathbf{x}\|^2 &= \nabla_{\mathbf{x}} \mathbf{x}^\top \mathbf{x} \\ &= \nabla_{\mathbf{x}} \begin{bmatrix} x_1 & x_2 & \cdots & x_n \end{bmatrix} \begin{bmatrix} x_1 \\ x_2 \\ \vdots \\ x_n \end{bmatrix} \\ &= \nabla_{\mathbf{x}} \sum_{i} x_i^2 \\ &= \nabla_{\mathbf{x}} (x_1^2 + x_2^2 + \cdots + x_n^2) \\ &= \begin{bmatrix} \frac{\partial}{\partial x_1}(x_1^2 + x_2^2 + \cdots + x_n^2) \\ \frac{\partial}{\partial x_2}(x_1^2 + x_2^2 + \cdots + x_n^2) \\ \vdots \\ \frac{\partial}{\partial x_n}(x_1^2 + x_2^2 + \cdots + x_n^2) \end{bmatrix} \\ &= \begin{bmatrix} 2x_1 \\ 2x_2 \\ \vdots \\ 2x_n \end{bmatrix} \\ &= 2\mathbf{x} \end{aligned} \]
\(\nabla_{\mathbf{X}} \|\mathbf{X}\|_F^2 = 2\mathbf{X}\)
其中 F范数(Frobenius范数)定义为 \(\|\mathbf{X}\|_F = \sqrt{\sum_{i,j} x_{ij}^2}\),因此 \(\|\mathbf{X}\|_F^2\) 表示先取F范数再平方,平方与根号恰好抵消:
\[\|\mathbf{X}\|_F^2 = \left(\sqrt{\sum_{i,j} x_{ij}^2}\right)^2 = \sum_{i,j} x_{ij}^2\]
对每个元素 \(x_{ij}\) 求偏导:
\[ \begin{aligned} \nabla_{\mathbf{X}} \|\mathbf{X}\|_F^2 &= \nabla_{\mathbf{X}} \begin{bmatrix} x_{11} & x_{12} & \cdots & x_{1n} \\ x_{21} & x_{22} & \cdots & x_{2n} \\ \vdots & \vdots & \ddots & \vdots \\ x_{m1} & x_{m2} & \cdots & x_{mn} \end{bmatrix}_F^2 \\ &= \nabla_{\mathbf{X}} \sum_{i,j} x_{ij}^2 \\ &= \nabla_{\mathbf{X}} (x_{11}^2 + x_{12}^2 + \cdots + x_{mn}^2) \\ \frac{\partial}{\partial x_{ij}} \sum_{p,q} x_{pq}^2 &= \frac{\partial}{\partial x_{ij}} x_{ij}^2 = 2x_{ij} \\ \therefore \nabla_{\mathbf{X}} \|\mathbf{X}\|_F^2 &= \begin{bmatrix} 2x_{11} & 2x_{12} & \cdots & 2x_{1n} \\ 2x_{21} & 2x_{22} & \cdots & 2x_{2n} \\ \vdots & \vdots & \ddots & \vdots \\ 2x_{m1} & 2x_{m2} & \cdots & 2x_{mn} \end{bmatrix} \\ &= 2\mathbf{X} \end{aligned} \]