TExcelTAD
Home › Math for Deep Learning & LLMs › Poor Conditioning

Conditioning and Numerical Stability

A computation is well conditioned if small changes in its input cause small changes in its output, and ill conditioned if they can cause large ones. Conditioning is a property of the problem, not of the code, but it decides how much floating-point rounding (and noise in data) the answer can absorb. In deep learning it shows up in matrix inversions, in normalization layers and, most importantly, in how hard a loss surface is to optimize.

The condition number of a matrix

For solving Ax=bA\mathbf{x} = \mathbf{b}, a relative error in b\mathbf{b} can be amplified by up to

κ(A)=∥A∥ ∥A−1∥=σmax⁡(A)σmin⁡(A)\kappa(A) = \lVert A \rVert\,\lVert A^{-1} \rVert = \frac{\sigma_{\max}(A)}{\sigma_{\min}(A)}

the ratio of the largest to the smallest singular value. κ=1\kappa = 1 is perfect (an orthogonal matrix); κ=10k\kappa = 10^{k} means roughly kk decimal digits of accuracy can be lost. A singular matrix has κ=∞\kappa = \infty.

python · try it yourself
import numpy as np

A = np.array([[1.0, 1.0], [1.0, 1.0001]])
b = np.array([2.0, 2.0001])
x = np.linalg.solve(A, b)
b2 = b + np.array([0.0, 0.0001])                 # a tiny change in b
x2 = np.linalg.solve(A, b2)
print("cond(A) =", f"{np.linalg.cond(A):.0f}")
print("x  =", np.round(x, 6))
print("x' =", np.round(x2, 6) + 0)
rel_in = np.linalg.norm(b2 - b) / np.linalg.norm(b)
rel_out = np.linalg.norm(x2 - x) / np.linalg.norm(x)
print(f"relative change in b: {rel_in:.1e}, in x: {rel_out:.1e}, amplification {rel_out / rel_in:.0f}")
cond(A) = 40002
x  = [1. 1.]
x' = [0. 2.]
relative change in b: 3.5e-05, in x: 1.0e+00, amplification 28285

A change of 0.00010.0001 in one entry of b\mathbf{b} moved the solution from (1,1)(1, 1) to (0,2)(0, 2).

Conditioning and gradient descent

For a quadratic loss 12wTHw\frac{1}{2}\mathbf{w}^T H\mathbf{w}, the condition number of the Hessian κ(H)=λmax⁡/λmin⁡\kappa(H) = \lambda_{\max}/\lambda_{\min} controls gradient descent. The step size must stay below 2/λmax⁡2/\lambda_{\max} for stability, and then the flattest direction shrinks only by a factor of about 1−1/κ1 - 1/\kappa per step: the number of steps grows in proportion to κ\kappa.

python · try it yourself
import numpy as np

def steps_to_converge(kappa, tol=1e-6):
    H = np.diag([1.0, kappa]); w = np.array([1.0, 1.0]); lr = 1.0 / kappa
    for t in range(1, 10**6):
        w = w - lr * (H @ w)
        if np.linalg.norm(w) < tol: return t
for k in (1, 10, 100, 1000):
    print(f"kappa = {k:5d}: {steps_to_converge(k)} steps")
kappa =     1: 1 steps
kappa =    10: 132 steps
kappa =   100: 1375 steps
kappa =  1000: 13809 steps

Much of the machinery of deep learning reduces effective ill conditioning: input standardization, careful initialization, batch and layer normalization, and per-parameter step sizes in Adam.

Standardizing inputs

Two features on very different scales (age in years, income in dollars) make the Hessian of a linear model ill conditioned, because H∝XTXH \propto X^TX. Subtracting the mean and dividing by the standard deviation of each feature brings κ\kappa close to 1 when the features are uncorrelated.

In software

  • np.linalg.cond computes κ\kappa; never invert a matrix you can solve with (np.linalg.solve, lstsq, Cholesky).
  • Normalization layers add ϵ\epsilon to variances (eps=1e-5) to keep division well conditioned.
  • Second-order and preconditioned optimizers (Shampoo, K-FAC) explicitly multiply gradients by an approximate inverse curvature to undo ill conditioning.

Exercises

Exercise 1: Condition number of a diagonal matrix

Find κ(diag⁡(10,0.1))\kappa(\operatorname{diag}(10, 0.1)).

Answer
Solution

The singular values of a diagonal matrix are the absolute values of its entries: 10/0.1=10010/0.1 = 100.

Exercise 2: Rescaling a feature

A linear model has features with standard deviations 1 and 1000 (uncorrelated, zero mean). Estimate κ(XTX)\kappa(X^TX) before and after standardization.

Answer

About 10610^6 before, about 1 after.

Solution

XTX/n≈diag⁡(1,106)X^TX/n \approx \operatorname{diag}(1, 10^6), so κ≈106\kappa \approx 10^6. After dividing each feature by its standard deviation, XTX/n≈IX^TX/n \approx I.

Exercise 3: Digits lost (code)

Solve a 10×1010 \times 10 Hilbert system Hx=H1H\mathbf{x} = H\mathbf{1} (with Hij=1/(i+j−1)H_{ij} = 1/(i + j - 1)) and compare the error with κ(H)ϵ\kappa(H)\epsilon.

Answer

κ≈1.6×1013\kappa \approx 1.6 \times 10^{13}; the error is about 3×10−43 \times 10^{-4}, within the bound κϵ≈3.6×10−3\kappa\epsilon \approx 3.6 \times 10^{-3}.

Solution
python · try it yourself
import numpy as np
n = 10
H = 1.0 / (np.arange(1, n + 1)[:, None] + np.arange(n)[None, :])
x = np.linalg.solve(H, H @ np.ones(n))
print(f"cond {np.linalg.cond(H):.1e}, max error {np.max(np.abs(x - 1)):.1e}, kappa*eps {np.linalg.cond(H) * np.finfo(float).eps:.1e}")
cond 1.6e+13, max error 3.2e-04, kappa*eps 3.6e-03

Test yourself

The condition number of a matrix is…

  1. its determinant
  2. the ratio of its largest to its smallest singular value
  3. its trace
  4. the number of nonzero entries

On a quadratic with Hessian condition number κ\kappa, plain gradient descent needs a number of steps that grows roughly…

  1. like log⁡κ\log\kappa
  2. in proportion to κ\kappa
  3. not at all
  4. like κ3\kappa^3

Which practice does NOT aim to improve conditioning?

  1. Standardizing input features
  2. Normalization layers
  3. Increasing the batch printout frequency
  4. Adaptive per-parameter step sizes