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
Differential Calculus: Derivatives, Gradients, Jacobians, and the Chain Rule
local linear approximations · tangent slopes · steepest descent · computation DAGs · finite-difference limits
Motivation
Single-Variable Calculus
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
Local Linearity
Analytical Differentiation
| Function | Derivative \frac{d}{dx} | Rule | Formula |
|---|---|---|---|
| Constant C | 0 | Linearity | (\alpha u + \beta v)' = \alpha u' + \beta v' |
| Power x^n | n x^{n-1} | Product | (u\,v)' = u'v + u\,v' |
| Exp e^x | e^x | Quotient | (u/v)' = (u'v - u\,v')/v^2 |
| Log \ln x | 1/x | 3w^2 - 4w + 2 | 3(2w) - 4(1) = 6w - 4 |
Multivariate Calculus
array([2.000003, 4.000005])
Gradient Geometry
First-Order Optimization
Perturbing all n coordinates simultaneously by \boldsymbol{\epsilon} \in \mathbb{R}^n sums the linear changes across every axis:
f(\mathbf{x} + \boldsymbol{\epsilon}) \approx f(\mathbf{x}) + \nabla_{\mathbf{x}} f(\mathbf{x})^\top \boldsymbol{\epsilon}
Compositions & DAGs
Multivariate Chain Rule in Action
For z = u^2 + v with u = x_1 x_2 and v = x_2^2 at (x_1, x_2) = (1, 2):
Vector & Matrix Calculus
| Expression | Function f(\mathbf{x}) | Gradient / Jacobian | Deep Learning Application |
|---|---|---|---|
| Linear form | \mathbf{a}^\top \mathbf{x} | \nabla_{\mathbf{x}}(\mathbf{a}^\top \mathbf{x}) = \mathbf{a} | Linear neuron \mathbf{w}^\top \mathbf{x} + b |
| Quadratic 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} | Hessian terms (2\mathbf{A}\mathbf{x} if \mathbf{A} = \mathbf{A}^\top) |
| Squared L_2 norm | \|\mathbf{x}\|_2^2 = \mathbf{x}^\top \mathbf{x} | \nabla_{\mathbf{x}}\|\mathbf{x}\|_2^2 = 2\mathbf{x} | Weight decay & MSE residuals |
| Least squares | \|\mathbf{X}\mathbf{w} - \mathbf{y}\|_2^2 | \nabla_{\mathbf{w}} L = 2\mathbf{X}^\top(\mathbf{X}\mathbf{w} - \mathbf{y}) | Linear regression batch gradient |
| Vector map | \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} | Chaining layers: \mathbf{J}_{\mathbf{f} \circ \mathbf{g}} = \mathbf{J}_{\mathbf{f}}\mathbf{J}_{\mathbf{g}} |
For any affine map \mathbf{u} = \mathbf{A}\mathbf{x} + \mathbf{b}, \mathbf{J}_{\mathbf{u}}(\mathbf{x}) = \mathbf{A}, so propagating \nabla_{\mathbf{u}} y backward multiplies by \mathbf{A}^\top: \nabla_{\mathbf{x}} y = \mathbf{A}^\top \nabla_{\mathbf{u}} y.
Forward & Backward Passes
array([2., 4.])
Numerical vs. Analytical Differentiation
Approximating f'(x) \approx \frac{f(x+h) - f(x)}{h} has two fatal flaws in deep learning:
float32 at \sim 4 valid digits).Check Your Understanding
Answers:
Key Takeaways
| Calculus Object | Formula | Shape | Geometric Meaning | Deep Learning Role |
|---|---|---|---|---|
| Derivative f'(x) | \lim_{h \to 0} \frac{f(x+h)-f(x)}{h} | () |
Tangent slope at x | 1D linear approx f(x+h) \approx f(x) + f'(x)h |
| Partial \frac{\partial f}{\partial x_i} | Derivative with x_{j \neq i} fixed | () |
Axis-i slice slope | Sensitivity of loss to single weight w_i |
| Gradient \nabla_{\mathbf{x}} f(\mathbf{x}) | [\partial f/\partial x_1, \ldots, \partial f/\partial x_n]^\top | (n,) |
Points uphill \perp contours | Descent step \mathbf{w} \leftarrow \mathbf{w} - \eta \nabla_{\mathbf{w}}\mathcal{L} |
| Jacobian \mathbf{J}_{\mathbf{f}}(\mathbf{x}) | (\mathbf{J}_{\mathbf{f}})_{ij} = \partial f_i / \partial x_j | (m, n) |
Local linear map \mathbf{J}\mathbf{h} | Layer-to-layer sensitivity matrix |
| Chain rule (VJP) | \nabla_{\mathbf{x}} y = \mathbf{J}_{\mathbf{u}}(\mathbf{x})^\top \nabla_{\mathbf{u}} y | (n,) |
Sum over all DAG paths | Reverse-mode backpropagation (§1.5) |
| Finite difference | \frac{f(x+h)-f(x-h)}{2h} | 2d evals | Secant slope on [x-h, x+h] | Unit-test gradient checks in float64 |