The Math Behind the Methods

Condition Numbers and Why Some Training Problems Are Hard

See how a single ratio predicts sensitive solutions and slow gradient descent, with worked numbers and practical ways to improve it.

Key takeaways

  • The condition number is the ratio of the largest singular value to the smallest, and it is never below 1.
  • A large condition number means solutions to linear systems can be highly sensitive to small changes in data.
  • For gradient descent on a quadratic, the steps needed grow roughly in proportion to the condition number of the Hessian.
  • Unscaled and strongly correlated features are common, fixable causes.
  • Scaling, regularization, preconditioning and better parameterization all work by evening out directions.

The condition number of a matrix compares how much it stretches vectors in its most stretched direction with how much in its least stretched one. A value near 1 means all directions are treated alike. A very large value means small errors in the inputs can become large errors in a solution, and that gradient descent will crawl.

Many training problems that look mysterious, such as a loss that zigzags and then stalls, are conditioning problems. The remedies are often simple once you recognize the cause.

The definition

For a matrix A with singular values from σ_max down to σ_min, measured in the L2 norm:

κ(A) = σ_max / σ_min

For a symmetric positive definite matrix, this is the ratio of the largest eigenvalue to the smallest. If the smallest singular value is zero, the matrix is singular and κ is infinite. Orthogonal matrices, which only rotate and reflect, have κ = 1.

A condition number is a property of a particular problem and its parameterization. The same relationship between inputs and outputs can be well or badly conditioned depending on how you write it down.

A sensitive system

Consider two equations that are almost copies of each other.

x₁ +        x₂ = 2
x₁ + 1.0001 x₂ = 2.0001        solution: (1, 1)

change only the second right-hand side by 0.0001:

x₁ +        x₂ = 2
x₁ + 1.0001 x₂ = 2.0002        solution: (0, 2)

A change of about five thousandths of a percent in one number moved the solution completely. The condition number of this matrix is about 40,000.

The general bound says that the relative change in the solution is at most κ times the relative change in the right-hand side. It is a worst case. Many perturbations do less damage, but you cannot count on being lucky.

Gradient descent on a stretched bowl

Take the loss f(w) = ½(λ₁w₁² + λ₂w₂²) with λ₁ ≥ λ₂ > 0. Its condition number is λ₁/λ₂. A gradient step with learning rate η updates each coordinate separately.

w_i ← (1 − η λ_i) · w_i

To avoid divergence you need η < 2/λ₁: the steepest direction sets the limit. But then the shallow direction shrinks only by a factor near 1 − ηλ₂ per step, which is close to 1 when λ₂ is tiny.

The best single learning rate for this problem is 2/(λ₁ + λ₂). The error then shrinks by the factor (κ − 1)/(κ + 1) each step.

κ Factor per step Steps to shrink error 1,000 times
10 about 0.818 35
100 about 0.980 346
1,000 about 0.998 3,454

Tenfold worse conditioning means roughly tenfold more steps. Momentum helps considerably: with ideally tuned settings on a quadratic, the asymptotic rate depends on √κ in place of κ. See momentum and Nesterov acceleration.

How poor conditioning slows training
1 Uneven curvature Some directions are far steeper than others. 2 Steep sets the limit The learning rate must stay small to remain stable. 3 Shallow crawls Flat directions barely move at that rate. 4 Loss stalls Progress looks slow although the optimum is far. Uneven curvature Some directions are far steeper than others. 1 Steep sets the limit The learning rate must stay small to remain stable. 2 Shallow crawls Flat directions barely move at that rate. 3 Loss stalls Progress looks slow although the optimum is far. 4

Watching the learning rate limit

Put numbers on the stretched bowl with λ₁ = 100 and λ₂ = 1. Stability requires η < 0.02.

η Steep factor, 1 − 100η Shallow factor, 1 − η What you see
0.021 −1.1 0.979 The steep coordinate flips sign and grows
0.019 −0.9 0.981 Zigzag that dies out; slow shallow progress
0.010 0 0.99 Steep error removed at once; shallow still slow

A negative factor produces the familiar back-and-forth across a valley. No single value is quick in both directions.

This short loop reproduces the step counts from the table above, using the best fixed rate for each case.

import numpy as np

for kappa in [10, 100, 1000]:
    lam = np.array([kappa, 1.0])
    eta = 2 / (kappa + 1)
    w = np.array([1.0, 1.0])
    start, steps = np.linalg.norm(w), 0
    while np.linalg.norm(w) > 1e-3 * start:
        w = w - eta * lam * w
        steps += 1
    print(kappa, steps)      # 35, 346, 3454

With stochastic mini-batches, gradient noise adds a second limit on the learning rate, so real curves are less tidy than this.

Feature scales

In linear regression with mean squared error, the Hessian is proportional to XᵀX. Suppose two centered, uncorrelated features have standard deviations of 1,000 and 1. Then XᵀX/n is diagonal with entries 1,000,000 and 1, so κ = 1,000,000. Standardizing both makes κ = 1.

This is the clearest reason to scale inputs before using gradient-based training. Compute means and standard deviations on the training fold only, as covered in standardization and normalization.

An intercept column interacts with uncentered features too. A feature such as a calendar year, with values near 2,000 and a small spread, is almost a multiple of the column of ones. Centering it removes that near-collinearity.

Correlated features

Scaling cannot fix correlation. For two standardized features with correlation ρ ≥ 0, the correlation matrix has eigenvalues 1 + ρ and 1 − ρ.

Correlation ρ κ = (1 + ρ)/(1 − ρ)
0 1
0.5 3
0.9 19
0.99 199
0.999 1,999

Removing a redundant column, combining the pair into a sum and a difference, or rotating to principal axes and rescaling can all help. Rescaling directions with very small variance also magnifies noise, so add a floor.

Numerical precision

Computers store numbers with limited digits. Single precision carries about 7 significant decimal digits and double precision about 16. As a rough guide, solving a system with condition number 10ᵏ can lose about k of them.

Forming XᵀX squares the condition number: κ(XᵀX) = κ(X)². A moderately difficult matrix with κ = 10⁴ becomes 10⁸, enough to ruin a single-precision solve. Use least squares routines based on QR or SVD, as discussed in projections and least squares. Lower precision formats in deep learning leave even less room.

The problem and the algorithm

Keep two ideas apart. A problem is ill-conditioned if its true answer is very sensitive to its inputs. An algorithm is numerically unstable if it adds error beyond what that sensitivity explains.

A stable solver applied to an ill-conditioned system still returns an answer that may be far from the one you wanted, because the data do not pin it down. An unstable procedure can spoil even a well-conditioned system.

The sensitive pair of equations shows that a small residual is not proof of accuracy. The guess (0, 2) satisfies the original first equation exactly and misses the second by only 0.0001, yet it is far from the true solution (1, 1). In model fitting, very different weight vectors can likewise give almost the same loss.

That matters for interpretation. Predictions on data resembling the training set may be stable while individual coefficients are not. Predictions for inputs that break the usual correlation pattern depend heavily on the poorly determined directions, and deserve little trust.

You can probe this directly. Refit on several bootstrap samples and compare both the coefficients and the predictions. Wide swings in coefficients with steady predictions point to redundancy among features. Wide swings in both suggest too little data for the model you chose.

Improving the conditioning

Regularization. Adding λ to every eigenvalue changes the ratio to (λ_max + λ)/(λ_min + λ). With eigenvalues 100 and 0.01, κ is 10,000. With λ = 1 it falls to 101/1.01 = 100. This changes the solution as well as the speed.

Preconditioning. Transform the problem or rescale the gradient with a matrix that approximates the inverse curvature. Newton's method uses the full Hessian. Diagonal methods such as Adam scale each parameter separately, which helps with mismatched scales but cannot fully undo a rotated valley.

Parameterization. Normalization layers, careful initialization and residual connections tend to make optimization of deep networks better behaved. Explanations of why are still partly debated, especially for batch normalization.

Second-order information. The Hessian describes local curvature. For nonconvex networks it varies from place to place and often has many near-zero eigenvalues, so a single global condition number is not very meaningful there.

Diagnosing it

For a design matrix that fits in memory, the check is short.

import numpy as np

rng = np.random.default_rng(0)
X = np.column_stack([rng.normal(0, 1000, 500),
                     rng.normal(0, 1, 500)])        # (500, 2)
Z = (X - X.mean(axis=0)) / X.std(axis=0)

print(np.linalg.cond(X))   # roughly a thousand
print(np.linalg.cond(Z))   # close to 1

Note that this reports κ(X), which is about 1,000 here. The Hessian of the squared loss has the square, about a million.

In training, suspect conditioning when:

  1. The loss oscillates unless the learning rate is tiny, and then barely falls.
  2. Some weights converge quickly while others keep drifting.
  3. Fitted coefficients are huge with opposite signs and change sharply between data samples.
  4. Standardizing inputs changes the training curve dramatically.

Other faults produce similar symptoms, so rule out bugs and data errors first. Reading gradient norms per layer helps separate them.

Common questions

What counts as a large condition number?

It depends on precision and noise. In double precision with clean data, 10⁶ may be harmless for a direct solve. With noisy measurements and plain gradient descent, a few hundred already costs time.

Does the determinant tell me the same thing?

No. The matrix 0.001 · I has a tiny determinant and a perfect condition number of 1. Conditioning concerns ratios between directions, not overall size.

Do tree models suffer from conditioning?

Not in the same way. Standard decision trees do not solve linear systems or follow gradients in feature space, so rescaling a feature leaves their splits essentially unchanged.

Written for 4MachineLearning with AI writing tools and not reviewed line by line by a person. Check anything you rely on against a textbook or primary source. Report an error.

Keep reading

All 26 articles in The Math Behind the Methods →

Latest news on this topic