|
| 1 | +import { Collapse } from "@site/src/components/collapse"; |
| 2 | + |
1 | 3 | #### 导数和微分 |
2 | 4 |
|
3 | 5 | > 通常定义导数为在函数的点的切线的斜率,微分描述在函数上的点附近的变化率 |
|
9 | 11 | 则 f 在 x 处的导数 $f'(x)$ 被定义为: |
10 | 12 |
|
11 | 13 | $$ |
12 | | -f'(x) = \lim_{h \to 0} \frac{f(x+h) - f(x)}{h} |
| 14 | +f'(x) = \lim_{h \to 0} \frac{f(x+h) - f(x)}{h} \tag{1.1.1} |
13 | 15 | $$ |
14 | 16 |
|
15 | 17 | 微分被定义为: |
16 | 18 |
|
17 | 19 | $$ |
18 | | -df = f'(x) \cdot dx |
| 20 | +df = f'(x) \cdot dx \tag{1.1.2} |
19 | 21 | $$ |
20 | 22 |
|
21 | 23 | ##### 通用函数的微分方法 |
|
48 | 50 |
|
49 | 51 | #### 偏导数 |
50 | 52 |
|
51 | | -针对单个变量 $x$ 的函数 $f(x)$ 的导数被定义为 $f'(x)$,而对于向量(多变量组合)的函数,则需要使用偏导数来计算。 |
| 53 | +针对单个变量 $\mathbf{x}$ 的函数 $f(x)$ 的导数被定义为: |
| 54 | + |
| 55 | +$$ |
| 56 | +f'(x) = \lim_{h \to 0} \frac{f(x+h) - f(x)}{h} \tag{1.2.1} |
| 57 | +$$ |
| 58 | + |
| 59 | +而对于多元变量组合 $x=(x, y, z)$ 的函数,则需要使用偏导数来计算,定义为: |
52 | 60 |
|
53 | | -偏导数被定义为: |
| 61 | +$$ |
| 62 | +\frac{\partial f}{\partial x} = \lim_{h \to 0} |
| 63 | +\frac{f(x + h, y, z) - f(x, y, z)}{h} |
| 64 | +$$ |
| 65 | + |
| 66 | +$$ |
| 67 | +\frac{\partial f}{\partial y} = \lim_{h \to 0} |
| 68 | +\frac{f(x, y + h, z) - f(x, y, z)}{h} |
| 69 | +$$ |
54 | 70 |
|
55 | 71 | $$ |
56 | | -\frac{\partial f}{\partial x_i} = \lim_{h \to 0} \frac{f(x_1, x_2, \cdots, x_i + h, \cdots, x_n) - f(x_1, x_2, \cdots, x_i, \cdots, x_n)}{h} |
| 72 | +\frac{\partial f}{\partial z} = \lim_{h \to 0} |
| 73 | +\frac{f(x, y, z + h) - f(x, y, z)}{h} |
57 | 74 | $$ |
58 | 75 |
|
59 | | -将偏导数推广到向量,则有: |
| 76 | +将多元变量组合推广到向量 \ |
| 77 | +:ctip[$\mathbf{x} = [x_1, x_2, \cdots, x_n]^T$]{id="数学和深度学习中通常使用列向量,向量的自然表达是行向量,故而使用*转置*"},\ |
| 78 | +则可根据 $1.2.2$ 推出梯度公式: |
60 | 79 |
|
61 | 80 | $$ |
62 | | -\nabla f = (\frac{\partial f}{\partial x_1}, \frac{\partial f}{\partial x_2}, \cdots, \frac{\partial f}{\partial x_n}) |
| 81 | +\nabla_x f(x) = [\frac{\partial f}{\partial x_1}, |
| 82 | +\frac{\partial f}{\partial x_2}, \cdots, \frac{\partial f}{\partial x_n}]^T \tag{1.2.2} |
63 | 83 | $$ |
64 | 84 |
|
65 | | -**即向量 $\nabla f$ 的每个分量都是对向量 $x$ 的每个变量求偏导数。** |
| 85 | +**即向量 :ctip[$\nabla_x f(x)$]{id="当 $\nabla_x f(x)$ 没有歧义时,可被简写为 $\nabla f$"} 的第 $i$ 个分量即是对向量 $\mathbf{x}$ 的第 $i$ 个分量 $x_i$ 求偏导数。** |
66 | 86 |
|
67 | 87 | #### 梯度 |
68 | 88 |
|
69 | 89 | > 梯度和导数相关,但梯度是向量,导数是实数,梯度是导数在多变量情况下的推广。 |
70 | 90 |
|
71 | | -**对于输入为单个变量,输出为实数的函数 $f(x)$**,_其梯度为其导数_,公式为: |
| 91 | +:ctip[**$\mathbf{f}: R \to R$**]{id="输入和输出都是**实数**的函数 $f$"},梯度即为导数: |
72 | 92 |
|
73 | 93 | $$ |
74 | | -\nabla f = \frac{d}{dx} f(x) |
| 94 | +\nabla \mathbf{f} = \nabla_{\mathbf{x}} \mathbf{f}(\mathbf{x}) = \mathbf{f}'(\mathbf{x}) = \frac{d}{d\mathbf{x}} \mathbf{f}(\mathbf{x}) |
75 | 95 | $$ |
76 | 96 |
|
77 | | -**对于输入为向量,输出为实数的函数 $f(x_1, x_2, \cdots, x_n)$**,_其梯度为偏导数组成的向量_,公式为: |
| 97 | +:ctip[**$f: R^n \to R$**]{id="输入为 n 维向量,输出为标量的函数"},其中 $\mathbf = [x_1, x_2, \cdots, x_n]^T$,梯度为偏导数组成的向量: |
78 | 98 |
|
79 | 99 | $$ |
80 | | -\nabla f = (\frac{\partial f}{\partial x_1}, \frac{\partial f}{\partial x_2}, \cdots, \frac{\partial f}{\partial x_n}) |
| 100 | +\nabla f = [\frac{\partial f}{\partial x_1}, \frac{\partial f}{\partial x_2}, \cdots, \frac{\partial f}{\partial x_n}]^T |
81 | 101 | $$ |
82 | 102 |
|
| 103 | +梯度在矩阵微积分中通常存在以下常用公式: |
| 104 | + |
| 105 | +- **线性变换**: |
| 106 | + |
| 107 | + - 对于所有的 :ctip[$\mathbf{x} \in R^n$]{id="$\mathbf{x} = [x_1, x_2, \cdots, x_n]^T$"} \ |
| 108 | + 和 :ctip[$A \in R^{m \times n}$]{id="$\mathbf{A} = \begin{bmatrix} \ |
| 109 | +A_{11} & \cdots & A_{1n} \\ \vdots & \ddots & \vdots \\ A_{m1} & \cdots & A_{mn} \ |
| 110 | +\end{bmatrix}$"}: |
| 111 | + |
| 112 | + <Collapse label="$$ |
| 113 | + \nabla_x (A x) = A^T |
| 114 | + $$"> |
| 115 | + |
| 116 | + $$ |
| 117 | + \begin{split} |
| 118 | + &\ \nabla_x (A x) = \nabla_x (A_1 x_1 + A_2 x_2 + \cdots + A_n x_n) \\ |
| 119 | + =&\ \nabla_x ([A_{11} \cdots A_{1m}] x_1 + [A_{21} \cdots A_{2m}] x_2 + \cdots + [A_{n1} \cdots A_{nm}] x_n) \\ |
| 120 | + =&\ \nabla_x (A_1 x_1 + A_2 x_2 + \cdots + A_n x_n) \\ |
| 121 | + =&\ \nabla_x (A_1 x_1) + \nabla_x (A_2 x_2) + \cdots + \nabla_x (A_n x_n) \\ |
| 122 | + =&\ \begin{bmatrix} |
| 123 | + \frac{\partial}{\partial x_1} (A_1 x_1) , \frac{\partial}{\partial x_2} (A_2 x_2) , \cdots , \frac{\partial}{\partial x_n} (A_n x_n) |
| 124 | + \end{bmatrix} \\ |
| 125 | + =&\ \begin{bmatrix} |
| 126 | + A_1 , A_2 , \cdots , A_n |
| 127 | + \end{bmatrix} = A^T |
| 128 | + \end{split} |
| 129 | + $$ |
| 130 | + |
| 131 | + </Collapse> |
| 132 | + |
| 133 | + - 对于所有的 :ctip[$\mathbf{x} \in R^n$]{id="$\mathbf{x} = [x_1, x_2, \cdots, x_n]^T$"} \ |
| 134 | + 和 :ctip[$A \in R^{n \times m}$]{id="$\mathbf{A} = \begin{bmatrix} \ |
| 135 | +A_{11} & \cdots & A_{1m} \\ \vdots & \ddots & \vdots \\ A_{n1} & \cdots & A_{nm} \ |
| 136 | +\end{bmatrix}$"}: |
| 137 | + |
| 138 | + <Collapse label="$$ |
| 139 | + \nabla_x (x^T A) = A |
| 140 | + $$"> |
| 141 | + |
| 142 | + $$ |
| 143 | + \begin{split} |
| 144 | + &\ \nabla_x (x^T A) = \nabla_x (x_1 A_1 + x_2 A_2 + \cdots + x_n A_n) \\ |
| 145 | + =&\ \nabla_x (x_1 [A_{11} \cdots A_{1m}] + x_2 [A_{21} \cdots A_{2m}] + \cdots + x_n [A_{n1} \cdots A_{nm}]) \\ |
| 146 | + =&\ \nabla_x (x_1 A_1 + x_2 A_2 + \cdots + x_n A_n) \\ |
| 147 | + =&\ \nabla_x (x_1 A_1) + \nabla_x (x_2 A_2) + \cdots + \nabla_x (x_n A_n) \\ |
| 148 | + =&\ \begin{bmatrix} \ |
| 149 | + \frac{\partial}{\partial x_1} (x_1 A_1) , \frac{\partial}{\partial x_2} (x_2 A_2) , \cdots , \frac{\partial}{\partial x_n} (x_n A_n) |
| 150 | + \end{bmatrix} \\ |
| 151 | + =&\ \begin{bmatrix} |
| 152 | + A_1 , A_2 , \cdots , A_n |
| 153 | + \end{bmatrix} = A |
| 154 | + \end{split} |
| 155 | + $$ |
| 156 | + |
| 157 | + </Collapse> |
| 158 | + |
| 159 | +- **二次型**(二次型是二次函数在向量空间中的推广): |
| 160 | + |
| 161 | + - 对于所有的 :ctip[$\mathbf{x} \in R^n$]{id="$\mathbf{x} = [x_1, x_2, \cdots, x_n]^T$"} \ |
| 162 | + 和 :ctip[$A \in R^{n \times n}$]{id="$\mathbf{A} = \begin{bmatrix} \ |
| 163 | +A_{11} & \cdots & A_{1n} \\ \vdots & \ddots & \vdots \\ A_{n1} & \cdots & A_{nn} \ |
| 164 | +\end{bmatrix}$"}: |
| 165 | + |
| 166 | + <Collapse label="$$ |
| 167 | + \nabla_x x^T A x = (A + A^T) x |
| 168 | + $$"> |
| 169 | + |
| 170 | + $$ |
| 171 | + \begin{split} |
| 172 | + &\ \nabla_x x^T A x = \nabla_x \sum_{i=1}^n \sum_{j=1}^n x_i A_{ij} x_j \\ |
| 173 | + =&\ \frac{\partial}{\partial x_k} \sum_{i=1}^n \sum_{j=1}^n x_i A_{ij} x_j + \frac{\partial}{\partial x_k} \sum_{i=1}^n \sum_{j=1}^n x_j A_{ji} x_i \\ |
| 174 | + =&\ \sum_{i=1}^n \sum_{j=1}^n A_{ij} x_j + \sum_{i=1}^n \sum_{j=1}^n A_{ji} x_i \\ |
| 175 | + =&\ \sum_{j=1}^n A_{kj} x_j + \sum_{i=1}^n A_{ik} x_i \text{($i, j = k$ 时,$A_{kj} x_j, A_{ik} x_i$ 分别存在一项 $A_{kk} x_k$)} \\ |
| 176 | + =&\ \sum_{i=1}^n (\sum_{j=1}^n A_{ij} x_j) \cdot e_i + \sum_{j=1}^n (\sum_{i=1}^n A_{ji} x_i) \cdot e_j \\ |
| 177 | + =&\ \sum_{i=1}^n (\mathbf{A} \mathbf{x})_i \cdot e_i + \sum_{j=1}^n (\mathbf{A^T} \mathbf{x})_j \cdot e_j \\ \ |
| 178 | + =&\ (A + A^T) x |
| 179 | + \end{split} |
| 180 | + $$ |
| 181 | + |
| 182 | + </Collapse> |
| 183 | + |
| 184 | +- **范数**: |
| 185 | + |
| 186 | + | 范数 | 定义 | 梯度 | |
| 187 | + | ---------- | ----------------------------------------------- | ---------------------------------------- | |
| 188 | + | $L^1$ | $\|x\|_1 = \sum_{i=1}^n x_i$ | $\nabla_x \|x\|_1 = \text{sign}(x)$ | |
| 189 | + | $L^2$ | $\|x\|_2 = \sqrt{\sum_{i=1}^n x_i^2}$ | $\nabla_x \|x\|_2 = \frac{x}{\|x\|_2}$ | |
| 190 | + | $L^\infty$ | $\|x\|_\infty = \max_{i=1}^n x_i$ | $\nabla_x \|x\|_\infty = \text{sign}(x)$ | |
| 191 | + | $L^0$ | $\|x\|_0 = \sum_{i=1}^n \mathbb{I}(x_i \neq 0)$ | $\nabla_x \|x\|_0 = \text{sign}(x)$ | |
| 192 | + |
| 193 | + - 对于所有的 :ctip[$\mathbf{x} \in R^n$]{id="$\mathbf{x} = [x_1, x_2, \cdots, x_n]^T$"} \ |
| 194 | + 和 :ctip[$A \in R^{n \times n}$]{id="$\mathbf{A} = \begin{bmatrix} \ |
| 195 | +A_{11} & \cdots & A_{1n} \\ \vdots & \ddots & \vdots \\ A_{n1} & \cdots & A_{nn} \ |
| 196 | +\end{bmatrix}$"}: |
| 197 | + |
| 198 | + <Collapse label="$$ |
| 199 | + \nabla_x \|x\|_2^2 = \nabla_x (x^T x) = 2x |
| 200 | + $$"> |
| 201 | + |
| 202 | + $$ |
| 203 | + \begin{split} |
| 204 | + &\ \nabla_x \|x\|_2 = \nabla_x (\sqrt{x^T x}) ^ 2 \\ |
| 205 | + =&\ \nabla_x (x^T x) = \nabla_x (x_1^2 + x_2^2 + \cdots + x_n^2) \\ |
| 206 | + =&\ 2x |
| 207 | + \end{split} |
| 208 | + $$ |
| 209 | + |
| 210 | + </Collapse> |
| 211 | + |
83 | 212 | :::nerd |
84 | | -在深度学习中每层神经网络之间由 **_权重矩阵_(通常还会添加同维度的偏置向量)桥接不同纬度矩阵的计算**。随后再通过激活函数将计算结果映射到非线性空间。 |
| 213 | +在深度学习中每层神经网络之间由 **_权重矩阵_(通常还会添加同维度的偏置向量)桥接不同纬度矩阵的计算**。随后再通过:term[激活函数]{./terms/dl#activation-function}将计算结果映射到非线性空间,是:term[神经元]{./terms/dl#neuron}的计算核心。 |
85 | 214 | ::: |
86 | 215 |
|
87 | 216 | #### 链式法则 |
| 217 | + |
| 218 | +> 本质是利用复合函数求导的思想,逐层求导(当计算到内层函数时转换为求梯度)并计算内积。 |
| 219 | + |
| 220 | +1. 对于复合函数来说,变量有且仅有囊括在复合函数中的变量,即 $g(x)$。 |
| 221 | +2. 对于两层的复合函数来说,若变量 $\mathbf{x} \in R^n$ 应该用梯度进行表示,则有: |
| 222 | + |
| 223 | +$$ |
| 224 | +f'(x) = f'(g(x)) \cdot \nabla_{x} g(x) |
| 225 | +$$ |
| 226 | + |
| 227 | +对于复合函数 $f$ 存在 $y_1, y_2, \cdots, y_n$($\mathbf{y} \in R^n$)等变量,对于函数 $y$ 有 $y_1 = g_1(x), y_2 = g_2(x), \cdots, y_n = g_n(x)$ 的复合函数,则有: |
| 228 | + |
| 229 | +$$ |
| 230 | +\nabla_{x} f(y) = \frac{\partial f}{\partial y_1} \cdot \nabla_{x} y_1 + \frac{\partial f}{\partial y_2} \cdot \nabla_{x} y_2 + \cdots + \frac{\partial f}{\partial y_n} \cdot \nabla_{x} y_n |
| 231 | +$$ |
0 commit comments