for h in 10.0 ** -np.arange(1, 6):
print(f'h = {h:.0e} ratio = {(loss(1 + h) - loss(1)) / h:.5f}')Dive into Deep Learning · §1.4
Differentialrechnung: Ableitungen, Gradienten, Jacobi-Matrizen und Kettenregel
Lokale lineare Näherungen · Tangentensteigungen · steilster Abstieg · azyklische Berechnungsgraphen · Grenzen endlicher Differenzen
Motivation
Analysis einer Variablen
h = 1e-01 ratio = 2.30000
h = 1e-02 ratio = 2.03000
h = 1e-03 ratio = 2.00300
h = 1e-04 ratio = 2.00030
h = 1e-05 ratio = 2.00003
Lokale Linearität
Analytisches Differenzieren
| Funktion | Ableitung \frac{d}{dx} | Regel | Formel |
|---|---|---|---|
| Konstante C | 0 | Linearität | (\alpha u + \beta v)' = \alpha u' + \beta v' |
| Potenz x^n | n x^{n-1} | Produkt | (u\,v)' = u'v + u\,v' |
| Exponentialfunktion e^x | e^x | Quotient | (u/v)' = (u'v - u\,v')/v^2 |
| Logarithmus \ln x | 1/x | 3w^2 - 4w + 2 | 3(2w) - 4(1) = 6w - 4 |
Analysis mehrerer Variablen
array([2.000003, 4.000005])
Geometrie des Gradienten
Optimierung erster Ordnung
Werden alle n Koordinaten gleichzeitig um \boldsymbol{\epsilon} \in \mathbb{R}^n verändert, addieren sich die linearen Änderungen entlang der einzelnen Achsen:
f(\mathbf{x} + \boldsymbol{\epsilon}) \approx f(\mathbf{x}) + \nabla_{\mathbf{x}} f(\mathbf{x})^\top \boldsymbol{\epsilon}
Verkettungen und gerichtete azyklische Graphen (DAGs)
Die mehrdimensionale Kettenregel in der Praxis
Für z = u^2 + v mit u = x_1 x_2 und v = x_2^2 an der Stelle (x_1, x_2) = (1, 2) gilt:
Vektor- und Matrixanalysis
| Ausdruck | Funktion f(\mathbf{x}) | Gradient / Jacobi-Matrix | Anwendung im Deep Learning |
|---|---|---|---|
| Linearform | \mathbf{a}^\top \mathbf{x} | \nabla_{\mathbf{x}}(\mathbf{a}^\top \mathbf{x}) = \mathbf{a} | Lineares Neuron \mathbf{w}^\top \mathbf{x} + b |
| Quadratische Form | \mathbf{x}^\top \mathbf{A} \mathbf{x} | \nabla_{\mathbf{x}}(\mathbf{x}^\top \mathbf{A} \mathbf{x}) = (\mathbf{A} + \mathbf{A}^\top)\mathbf{x} | Terme der Hesse-Matrix (2\mathbf{A}\mathbf{x} für \mathbf{A} = \mathbf{A}^\top) |
| Quadrierte L_2-Norm | \|\mathbf{x}\|_2^2 = \mathbf{x}^\top \mathbf{x} | \nabla_{\mathbf{x}}\|\mathbf{x}\|_2^2 = 2\mathbf{x} | Gewichtszerfall und MSE-Residuen |
| Kleinste Quadrate | \|\mathbf{X}\mathbf{w} - \mathbf{y}\|_2^2 | \nabla_{\mathbf{w}} L = 2\mathbf{X}^\top(\mathbf{X}\mathbf{w} - \mathbf{y}) | Batchgradient der linearen Regression |
| Vektorwertige Abbildung | \mathbf{f}: \mathbb{R}^n \to \mathbb{R}^m | \mathbf{J}_{\mathbf{f}}(\mathbf{x}) \in \mathbb{R}^{m \times n},\; (\mathbf{J}_{\mathbf{f}})_{ij} = \frac{\partial f_i}{\partial x_j} | Verkettung von Schichten: \mathbf{J}_{\mathbf{f} \circ \mathbf{g}} = \mathbf{J}_{\mathbf{f}}\mathbf{J}_{\mathbf{g}} |
Für jede affine Abbildung \mathbf{u} = \mathbf{A}\mathbf{x} + \mathbf{b} gilt \mathbf{J}_{\mathbf{u}}(\mathbf{x}) = \mathbf{A}. Beim Rückwärtsdurchlauf wird \nabla_{\mathbf{u}} y daher mit \mathbf{A}^\top multipliziert: \nabla_{\mathbf{x}} y = \mathbf{A}^\top \nabla_{\mathbf{u}} y.
Vorwärts- und Rückwärtsdurchlauf
array([2., 4.])
Numerisches und analytisches Differenzieren
Die Näherung f'(x) \approx \frac{f(x+h) - f(x)}{h} hat im Deep Learning zwei entscheidende Nachteile:
float32 bleiben höchstens \sim 4 gültige Stellen).Wissensprüfung
Lösungen:
Das Wichtigste
| Größe | Formel | Form | Geometrische Bedeutung | Rolle im Deep Learning |
|---|---|---|---|---|
| Ableitung f'(x) | \lim_{h \to 0} \frac{f(x+h)-f(x)}{h} | () |
Tangentensteigung bei x | Lineare 1D-Näherung f(x+h) \approx f(x) + f'(x)h |
| Partielle Ableitung \frac{\partial f}{\partial x_i} | Ableitung bei festem x_{j \neq i} | () |
Steigung im Schnitt entlang Achse i | Empfindlichkeit des Verlusts gegenüber einem Gewicht w_i |
| Gradient \nabla_{\mathbf{x}} f(\mathbf{x}) | [\partial f/\partial x_1, \ldots, \partial f/\partial x_n]^\top | (n,) |
Zeigt bergauf, \perp zu Niveaulinien | Abstiegsschritt \mathbf{w} \leftarrow \mathbf{w} - \eta \nabla_{\mathbf{w}}\mathcal{L} |
| Jacobi-Matrix \mathbf{J}_{\mathbf{f}}(\mathbf{x}) | (\mathbf{J}_{\mathbf{f}})_{ij} = \partial f_i / \partial x_j | (m, n) |
Lokale lineare Abbildung \mathbf{J}\mathbf{h} | Matrix der Empfindlichkeiten zwischen Schichten |
| Kettenregel (VJP) | \nabla_{\mathbf{x}} y = \mathbf{J}_{\mathbf{u}}(\mathbf{x})^\top \nabla_{\mathbf{u}} y | (n,) |
Summe über alle DAG-Pfade | Backpropagation im Rückwärtsmodus (§1.5) |
| Endliche Differenz | \frac{f(x+h)-f(x-h)}{2h} | 2d Auswertungen | Sekantensteigung auf [x-h, x+h] | Gradientenprüfung in Softwaretests mit float64 |