PyHessian: Neural Networks Through the Lens of the Hessian

Zhewei Yao, Amir Gholami, Kurt Keutzer, Michael W. Mahoney

Introduction

Modern deep learning architectures rely heavily on components like Batch Normalization (BN) and residual connections. While these are standard, the theoretical justification for their success often relies on simplified first-order gradient analysis. To gain a deeper understanding, researchers look to the Hessian matrix—the matrix of second-order derivatives—which describes the curvature of the loss landscape. However, for large-scale models, computing the Hessian is traditionally intractable.

PyHessian: Scalable Second-Order Analysis

The authors introduce PyHessian, an open-source library designed for fast, scalable Hessian analysis in PyTorch. By employing "matrix-free" numerical linear algebra techniques, PyHessian avoids the explicit computation of the massive Hessian matrix. Instead, it utilizes:

  • Power Iteration to identify directions of steepest curvature (top eigenvalues).
  • Hutchinson’s Method to estimate the average curvature (Hessian Trace).
  • Stochastic Lanczos Quadrature to compute the full Eigenvalue Spectral Density (ESD).

These methods require only Hessian-vector products, making them computationally comparable to standard backpropagation and allowing them to scale to massive distributed models.

Debunking the Batch Normalization Smoothing Myth

A common belief in deep learning is that Batch Normalization always smooths the loss landscape. PyHessian reveals a more nuanced reality: the impact of BN depends significantly on network depth.

3D loss landscapes for ResNet20 and ResNet38 under various configurations. For the shallower ResNet20 (top row), the landscape without BN (left) is actually smoother than with BN (middle). For the deeper ResNet38 (bottom row), BN is essential to mitigate the extreme curvature seen in the baseline model.

As illustrated in the figure above, for shallower models like ResNet20, removing BN results in a flatter Hessian spectrum. However, as depth increases (e.g., ResNet38 and beyond), the landscape naturally becomes more rugged. In these deeper regimes, BN becomes critical for controlling curvature and ensuring convergence. For models like ResNet32 and ResNet38, removing BN causes the Hessian trace to skyrocket—increasing from approximately 2,000 to over 10,000.

Key Insights on Architecture and Training

Beyond the depth-dependent effects of Batch Normalization, the PyHessian analysis yielded several other critical insights into modern neural architectures:

  • Residual Connections: Unlike BN, residual connections consistently smooth the loss landscape across both shallow and deep architectures. This suggests that skip connections provide a more robust mechanism for landscape regularization than normalization layers.
  • Stage-wise Importance: BN is not equally important at all layers. Analysis shows it is most critical in the final stages of a ResNet; removing it there causes significant spikes in curvature and drops in accuracy.
  • Sharpness and Generalization: The study confirms a strong correlation between the "sharpness" of a model (measured by the Hessian trace) and its generalization error. Models with higher curvature generally exhibit poorer performance.
  • Training Dynamics: Eigenvalue Spectral Density (ESD) plots reveal that without BN, models initially get "stuck" in degenerate regions with many zero-curvature directions before eventually finding sharper, non-degenerate areas later in training.

Conclusion and Future Directions

PyHessian provides a powerful diagnostic tool for understanding the training dynamics of deep neural networks. Future work involves integrating these second-order insights directly into the training process via Hessian-aware optimizers and utilizing curvature information to improve model compression techniques like pruning and quantization.

Summary updated

BibTeX
@inproceedings{yao2020pyhessian,
  author = {Zhewei Yao and Amir Gholami and Kurt Keutzer and Michael W. Mahoney},
  title = {{PyHessian: Neural Networks Through the Lens of the Hessian}},
  booktitle = {IEEE BigData},
  year = {2020},
  doi = {10.1109/bigdata50022.2020.9378171},
}
Figure