(Almost) Smooth Sailing: Towards Numerical Stability of Neural Networks Through Differentiable Regularization of the Condition Number

Rossen Nenov, Daniel Haider, Peter Balazs

TL;DR

Neural networks often suffer from numerical instability due to poorly conditioned weight matrices, but directly optimizing a matrix's condition number using gradient descent is impossible because the function is discontinuous and non-differentiable. This paper introduces a novel regularizer that is provably differentiable almost everywhere and effectively drives weight matrices toward an optimal condition number of 1. When applied to noisy MNIST classification and denoising tasks, this regularizer significantly boosts model robustness against heavy noise.

What Problem Does This Paper Address?

Numerical stability in neural networks determines how sensitive a model's predictions and training dynamics are to small perturbations in inputs, weights, or quantization errors. In numerical linear algebra, a classic way to measure this stability is the condition number ($\kappa$) of a matrix—the ratio of its largest singular value to its smallest non-zero singular value. A condition number of 1 is ideal (perfectly stable), while a massive condition number means tiny input changes can cause wild, unpredictable output swings.

While we would ideally use the condition number as a regularizing term during training, it has a fatal flaw: it is discontinuous and non-differentiable whenever singular values approach zero or change order. Standard gradient descent simply cannot handle it. Existing workarounds like Tikhonov regularization ($L_2$ regularization/weight decay) help control weight magnitudes but do not directly target or optimize the condition number itself.

Methods

The authors propose a beautifully simple alternative regularizer, $r(S)$, defined as the difference between the squared spectral norm and the scaled squared Frobenius norm:

$$r(S) := \frac{1}{2}\|S\|_2^2 - \frac{1}{2\nu}\|S\|_F^2$$

where $\|S\|_2$ is the spectral norm (the largest singular value), $\|S\|_F$ is the Frobenius norm (the root-of-sum-of-squares of all singular values), and $\nu = \min\{n, m\}$ represents the matrix dimensions.

Why this works:

  1. Optimal Conditioning: The authors prove that $r(S) \ge 0$, and $r(S) = 0$ if and only if the matrix has full rank and a condition number of exactly 1.
  2. Differentiable Almost Everywhere: Crucially, they prove that this function is differentiable whenever the largest singular value is unique. Because randomly initialized matrices typically have distinct singular values, the function is differentiable almost surely during training.
  3. Explicit Gradient: They derive an explicit, easily computable formula for its gradient:

$$\nabla r(S) = \sigma_{\max}(S)u_1v_1^T - \frac{1}{\nu}S$$

where $u_1$ and $v_1$ are the top left and right singular vectors obtained via Singular Value Decomposition (SVD). This allows the regularizer to be seamlessly integrated into standard optimization algorithms like Adam or SGD.

Datasets

The method is evaluated using the classic MNIST handwritten digit dataset. To rigorously test robustness and numerical stability, the authors inject varying levels of Gaussian noise into the images, evaluating models across different Signal-to-Noise Ratios (SNR = $\infty$ [no noise], 1, and 0.5).

Key Insights and Ideas

  • The Frobenius-Spectral Gap: The regularizer cleverly exploits the mathematical gap between the maximum squared singular value (spectral norm) and the average of all squared singular values (Frobenius norm). Minimizing this gap forces all singular values to become equal, naturally driving the condition number to 1.
  • Guaranteed Full Rank: Direct condition number minimization can trivially achieve a condition number of 1 by crushing a matrix down to rank 1 (where it only has one non-zero singular value). This regularizer inherently penalizes low-rank solutions, ensuring the matrix retains full rank and preserves the network's capacity to learn complex representations.
  • Tikhonov Fallacy: The paper highlights that standard $L_2$ regularization actually causes condition numbers to increase dramatically under certain training conditions, proving that controlling weight magnitude is not the same as controlling numerical stability.

Key Results

Basic Functionality

In a standard matrix least-squares problem, increasing the regularization parameter $\lambda$ drastically drops the condition number to near-optimal levels while only incurring a minor (<6%) increase in approximation error.

Figure 2. Results of least-squares minimization of (4.1) after 105

Noisy MNIST Classification

For a 2-layer neural network under severe noise (SNR = 0.5), the unregularized baseline drops to 71.91% accuracy with a condition number of 43.12. Applying the proposed regularizer ($\lambda = 1$) drops the condition number to 1.53 and boosts classification accuracy to 84.25% (see Table 1).

MNIST classification results

Image Denoising

In an autoencoder task under heavy noise, a naively trained model completely fails to reconstruct images because its outer layer condition numbers explode to over 600. The regularized autoencoder keeps condition numbers low (under 24) and successfully denoises the digits.

Figure 1. Results of MNIST denoising with autoencoders. Top: MNIST images with added Gaussian noise. Mid: No regulariza- tion. Bottom: Proposed regularization. While the vanilla autoen- coder struggles significantly, the regularized one performs well.
Autoencoder condition numbers across layers

Limitations and Future Work

  • Architecture Scale: The experiments are restricted to small, fundamental architectures (2-layer networks and basic autoencoders) on the MNIST dataset. It remains to be seen how well this scales to deep convolutional networks, transformers, or massive datasets like ImageNet.
  • Computational Overhead: Calculating the top singular value and its corresponding singular vectors ($u_1, v_1$) requires an SVD step (or power iteration) at every training iteration, which could introduce a computational bottleneck for very large weight matrices.
  • The Clean-Data Trade-off: As seen in the results, when there is zero noise (SNR = $\infty$), the regularized model performs slightly worse than the baseline (96.50% vs 98.42%). Finding a way to maintain optimal stability without sacrificing clean-data performance is an open question for future research.

Summary updated

BibTeX
@inproceedings{nenov2024almost,
  author = {Rossen Nenov and Daniel Haider and Peter Balazs},
  title = {{(Almost) Smooth Sailing: Towards Numerical Stability of Neural Networks Through Differentiable Regularization of the Condition Number}},
  booktitle = {Differentiable Almost Everything @ ICML},
  year = {2024},
  url = {https://openreview.net/forum?id=OJkyvJXk2B},
}
Figure