§4.1–4.3Overflow & Underflow · Poor Conditioning · Gradient-Based Optimization

Part I DL pp. 80–85 · ~6 min read

  • underflow
  • overflow
  • softmax
  • condition number
  • objective function
  • gradient
  • gradient descent
  • learning rate
  • critical point
  • saddle point

Machine learning runs on numerical computation: iterative methods that update estimates rather than deriving symbolic solutions. The fundamental difficulty: infinitely many reals must live in finitely many bit patterns, so almost every number carries rounding error — and error compounds across operations, making theoretically correct algorithms fail in practice.

§4.1 Overflow and underflow

Underflow : numbers near zero round to exactly zero — and many functions change qualitatively at zero (division raises or returns NaN; log 0 = −∞, which becomes NaN downstream). Overflow : large magnitudes become ±∞, and further arithmetic turns them into NaN.

The canonical function needing stabilization is the softmax , used to produce multinoulli probabilities:

Term by term

softmax(x)i=exp(xi)j=1nexp(xj)\mathrm{softmax}(\boldsymbol{x})_i = \frac{\exp(x_i)}{\sum_{j=1}^n \exp(x_j)}
exp(xi)\exp(x_i)exponentiate each score — the overflow/underflow hazard: e^{710} = ∞ and e^{−746} = 0 in float64positive
jexp(xj)\sum_j \exp(x_j)the normalizer — if EVERY term underflows, this is 0 and we divide by zeropositive
softmax(x)i\mathrm{softmax}(\boldsymbol{x})_ia probability; with all inputs equal to c, every output should be exactly 1/n regardless of c(0,1), sums to 1

The fix exploits shift-invariance — adding a scalar to every input does not change the value: evaluate softmax(z)\mathrm{softmax}(\boldsymbol{z}) with z=xmaxixi\boldsymbol{z} = \boldsymbol{x} - \max_i x_i. The largest argument to exp becomes 0 (no overflow) and the denominator contains a 1 (no divide-by-zero). Try to break both versions:

The softmax failure and its fix, in genuine float64 — drag c to ±800
x = [c, c+1, c+2] = [0, 1, 2]

naive: exp(xᵢ)/Σexp(xⱼ)OK

exp terms[1.000, 2.718, 7.389]
denominator11.11
softmax[0.09003, 0.2447, 0.6652]

fine at this scale — but drag c to either extreme…

stable: z = x − max(x) firstOK

exp terms[0.1353, 0.3679, 1.000]
denominator1.503
softmax[0.09003, 0.2447, 0.6652]

largest argument to exp is 0, so no overflow; denominator ≥ 1, so no divide-by-zero. Same analytic value — softmax is shift-invariant.

these are real float64 computations happening in your browser, not a simulation

One residue remains: the numerator can still underflow, so naively taking log(softmax(x))\log(\mathrm{softmax}(x)) can produce -\infty — log softmax needs its own stabilized routine (same trick). In practice, most readers lean on stable low-level libraries; Theano-style packages even detect and auto-stabilize common unstable expressions.

§4.2 Poor conditioning

Conditioning measures how rapidly a function’s output changes under small input perturbations — fast change means input rounding error becomes large output error. For f(x)=A1xf(\boldsymbol{x}) = \boldsymbol{A}^{-1}\boldsymbol{x}:

Term by term

κ(A)=maxi,jλiλj\kappa(\boldsymbol{A}) = \max_{i,j} \left| \frac{\lambda_i}{\lambda_j} \right|
λi/λj\lambda_i / \lambda_jratio of largest to smallest eigenvalue magnitude — how unevenly A stretches space (§2.7’s ellipse, very elongated)ratio ≥ 1
κ\kappathe condition number: large κ ⟹ inversion amplifies pre-existing input error — an INTRINSIC property of A, before any rounding in the inversion itselfscalar

Worked example

A=diag(5,1)\boldsymbol{A} = \mathrm{diag}(5, 1) has κ=5\kappa = 5: an input error along the second axis is magnified 5× more by A1\boldsymbol{A}^{-1} than one along the first. In the EigenLens, set λ₁ = 2.5, λ₂ = 0.5 — the elongated ellipse IS bad conditioning, and the same ratio returns as gradient descent’s nemesis in §4.3.1.

§4.3 Gradient-based optimization

Optimization: minimize (or maximize — just minimize f-f) an objective function , a.k.a. criterion — or, when minimized, cost / loss / error function (this book uses the terms interchangeably). The optimum gets a star: x=argminf(x)x^* = \arg\min f(x).

The derivative f(x)f'(x) is the slope: f(x+ϵ)f(x)+ϵf(x)f(x + \epsilon) \approx f(x) + \epsilon f'(x) — it tells us how to change xx to improve yy: f(xϵsign(f(x)))<f(x)f(x - \epsilon\,\mathrm{sign}(f'(x))) < f(x) for small ϵ\epsilon. Stepping opposite the derivative is gradient descent (Cauchy, 1847):

global minimum at x = 0: f′(0) = 0, descent haltsx < 0: f′ < 0 —move right to decrease fx > 0: f′ > 0 —move left to decrease ff(x) = ½x²,  f′(x) = x

Fig 4.1 (recreated) — the derivative always points the way downhill.

Where f(x)=0f'(x) = 0, the derivative says nothing — critical (stationary) points , of three kinds:

MinimumMaximumSaddle point

Fig 4.2 (recreated) — zero slope can mean lower than all neighbors, higher than all neighbors, or ( saddle ) neighbors both higher AND lower.

Deep learning objectives have many suboptimal local minima and saddle points in flat regions, so we settle for finding ff very low — not provably minimal (fig 4.3’s landscape: a near-optimal local minimum is an acceptable halting point; a poor one should be avoided; the global one may be unreachable).

The multivariate case: which direction is downhill?

With inputs xRn\boldsymbol{x} \in \mathbb{R}^n, partial derivatives f/xi\partial f/\partial x_i assemble into the gradient xf\nabla_{\boldsymbol{x}} f, and the slope along a unit vector u\boldsymbol{u} is the directional derivative uxf\boldsymbol{u}^\top \nabla_{\boldsymbol{x}} f. The best direction falls out in three moves:

Why steepest descent means −∇f (eqs 4.3–4.4)
minu,uu=1uxf(x)\min_{\boldsymbol{u},\, \boldsymbol{u}^\top\boldsymbol{u} = 1} \boldsymbol{u}^\top \nabla_{\boldsymbol{x}} f(\boldsymbol{x})

step 1/3: Find the unit direction with the most negative slope — the directional derivative comes from the chain rule on f(x + αu) at α = 0.

Term by term

x=xϵxf(x)\boldsymbol{x}' = \boldsymbol{x} - \epsilon \nabla_{\boldsymbol{x}} f(\boldsymbol{x})
xf\nabla_{\boldsymbol{x}} fthe uphill direction — negated to descendn×1
ϵ\epsilonthe LEARNING RATE: a small constant, a value solved to zero the directional derivative, or the best of several tried (a LINE SEARCH)scalar > 0
x\boldsymbol{x}'the proposed new point; iterate until every gradient element is (nearly) zero — or solve ∇f = 0 directly when possiblen×1

What this really says

Gradient descent is local greed: take the best infinitesimal step, repeatedly, with the learning rate deciding how far to trust the local slope. The same repeated-small-improvement idea works in discrete spaces too, where it is called hill climbing. What the slope does NOT tell you is how it will change as you move — that is curvature, the subject of §4.3.1.

Check yourself — numerics and gradients

1.In the SoftmaxStabilizer you drag c to 800. Predict both pipelines.

2.Even after stabilizing softmax, why does log softmax need its OWN stabilized routine?

3.What does a large condition number of A tell you about solving with A⁻¹?

4.Why does the negative gradient point 'directly downhill'?

5.Why does deep learning 'settle' for a low—but not provably minimal—objective value?

5 questions