Dive into Deep Learning · §34.1
Single-Variable Calculus
linear approximation · differentiation rules · Mean Value Theorem · Taylor remainder · descent lemma · Newton’s method · subgradients
Motivation
Geometric Intuition
The Derivative
Numerical Pitfalls
How small must \epsilon be before a finite difference (f(x+\epsilon)-f(x))/\epsilon accurately estimates f'(x)? There is no universal threshold because higher-order terms can dominate at moderate step sizes:
epsilon = 0.10000 -> 25.11000
epsilon = 0.00100 -> 8.00270
epsilon = 0.00010 -> 8.00012
epsilon = 0.00001 -> 8.00001
First-Order Model
Differentiation Rules
Algebra of Derivatives
Running Example
Empirical Check
For the logistic loss \ell(w) = s(-w) = \log(1+e^{-w}), the chain rule gives \ell'(0) = -\sigma(0) = -\tfrac{1}{2} and curvature \ell''(0) = \sigma(0)(1-\sigma(0)) = \tfrac{1}{4}. Shrinking \epsilon tenfold shrinks the linear-model gap a hundredfold:
eps = 0.1 : l = 0.644397, line = 0.643147, gap = 1.25e-03, gap / eps^2 = 0.1249
eps = 0.01 : l = 0.688160, line = 0.688147, gap = 1.25e-05, gap / eps^2 = 0.1250
eps = 0.001: l = 0.692647, line = 0.692647, gap = 1.25e-07, gap / eps^2 = 0.1250
The ratio \text{gap}/\epsilon^2 converges to \tfrac{1}{2}\ell''(0) = \tfrac{1}{8} = 0.1250. The slight discrepancy 0.1249 at \epsilon = 0.1 comes from the fourth-order Taylor term -\epsilon^4/192.
From Points to Intervals
To infer function behavior across an entire interval [a, b] from pointwise derivatives, calculus relies on Fermat’s lemma and the Mean Value Theorem (MVT):
First-Order Optimization
Stability Threshold
On f(x) = x^2, where f'(x) = 2x and curvature is f''(x) \equiv 2, gradient descent obeys the exact recurrence x_{t+1} = (1 - 2\eta)x_t = (1 - 2\eta)^{t+1}x_0, converging if and only if |1 - 2\eta| < 1 (0 < \eta < 1 = 2/f''):
eta = 0.05 -> x_10 = +0.34868
eta = 0.50 -> x_10 = +0.00000
eta = 0.90 -> x_10 = +0.10737
eta = 1.00 -> x_10 = +1.00000
eta = 1.10 -> x_10 = +6.19174
Second-Order Geometry
Taylor Expansion
Error Bounds
Order of Accuracy
When |f^{(n+1)}| is bounded near x_0, the worst-case error of P_n on |x - x_0| \le h scales as O(h^{n+1}), so halving the window h divides the error by 2^{n+1} (4 for P_1, 8 for P_2, and 16 for P_3):
n = 1: ratios between successive windows 4.3 4.1 4.1, predicted 4
n = 2: ratios between successive windows 8.4 8.2 8.1, predicted 8
n = 3: ratios between successive windows 16.7 16.3 16.2, predicted 16
Analyticity Pitfall
Optimization Guarantee
Applying the n = 1 Lagrange remainder converts a bound L on curvature (|f''| \le L, or L-Lipschitz gradient |f'(u)-f'(v)| \le L|u-v|) into a guaranteed safe step size for gradient descent:
Second-Order Optimization
Basin of Attraction
Near a minimizer with f''(x^\star) > 0, Newton’s method doubles the number of correct digits per step—but far from x^\star, small local curvature makes the parabola nearly flat and flings iterates past the basin of attraction:
t = 0: x = 2.000000000000, error = 1.0e+00
t = 1: x = 1.416666666667, error = 4.2e-01
t = 2: x = 1.110534409842, error = 1.1e-01
t = 3: x = 1.010636768405, error = 1.1e-02
t = 4: x = 1.000111557304, error = 1.1e-04
t = 5: x = 1.000000012443, error = 1.2e-08
from x0 = 1.4: -1.41, 1.45, -1.55, 1.85, -2.89, 8.71
from x0 = 1.3: -1.16, 0.86, -0.37, 0.03, -0.00, 0.00
On the strictly convex function f(x) = x\arctan x - \tfrac{1}{2}\log(1+x^2), where f'(x)=\arctan x and f''(x)=1/(1+x^2), Newton converges from x_0 = 1.3 but diverges wildly from x_0 = 1.4 (|x_0| \gtrsim 1.39) unless the step is damped.
Nonsmooth Calculus
Limiting Slopes
d|x|/dx at 0: autograd returns 0.0
ReLU' at 0: autograd returns 0.0
Autodiff at Kinks
Chaining fixed subgradient conventions across a composite graph can produce a number that is not a valid subgradient of the composite function:
autograd: phi'(0) = 0.0 (true slope: 1.0)
Why SGD training is unaffected (Bertoin et al., 2021, as corrected in their 2023 erratum): Triggering this mismatch requires a pre-activation to land on 0.0 exactly. In 32-bit float32 arithmetic, changing \mathrm{ReLU}'(0) within [0, 1] alters gradients on only a few minibatches per million by \sim 10^{-3}, producing identical training and test accuracy.
Review & Practice
Work out these three questions using the Taylor remainder, descent lemma, and subdifferential definition:
Answers:
Key Takeaways
| Concept / Theorem | Mathematical Formulation | Deep Learning & Optimization Role |
|---|---|---|
| Linear Approximation | f(x+\epsilon) = f(x) + \epsilon f'(x) + o(\epsilon) | Local slope model; proves sum, product, and chain rules |
| Softplus & Sigmoid | s(x)=\log(1+e^x), s'=\sigma, s''=\sigma(1-\sigma) \le \tfrac{1}{4} | Smooth counterpart of \mathrm{ReLU}; binary logistic loss curvature |
| Mean Value Theorem | f'(\xi) = \frac{f(b)-f(a)}{b-a} for some \xi \in (a,b) | Connects pointwise derivatives to interval monotonicity |
| Taylor & Lagrange R_n | f(x) = \sum_{i=0}^n \frac{f^{(i)}(x_0)}{i!}(x-x_0)^i + \frac{f^{(n+1)}(\xi)}{(n+1)!}(x-x_0)^{n+1} | Quantifies O(h^{n+1}) truncation error of degree-n local models |
| Descent Lemma | f(x-\eta f'(x)) \le f(x) - \eta(1 - \frac{L\eta}{2})f'(x)^2 | Certifies safe learning rates 0 < \eta < 2/L and optimal \eta = 1/L |
| Newton’s Method | x_{t+1} = x_t - f'(x_t)/f''(x_t), e_{t+1} \approx \frac{f'''(x^\star)}{2f''(x^\star)}e_t^2 | Minimizes local parabola P_2; quadratic local convergence |
| Subdifferential | \partial f(x) = \{g : f(y) \ge f(x) + g(y-x)\ \forall y\} | Handles \mathrm{ReLU} & \|x\| kinks; global minimum iff 0 \in \partial f(x) |