Core Math & Gradients
tensorflow/tensorflow · C++, Eigen, Python
Safeguarded tf.linalg.det backward pass against gradient explosion and NaN contamination on singular matrices, fixed tf.math.floordiv for -inf divisors, added Jacobian gradient tests for igamma, and prevented +inf NaN cascades in cumulative_logsumexp.
Verify with Bazel
Run the upstream regression suite in tensorflow/tensorflow for this subsystem.
$bazel test //tensorflow/python/kernel_tests/linalg:linalg_grad_testChanged Files (1)
1. Introduction & Motivation
Differentiable linear algebra layers frequently compute determinants of learned or intermediate matrices that can become rank-deficient during training. Because TensorFlow's _MatrixDeterminantGrad evaluated via direct matrix inversion, singular matrices () triggered division by zero, gradient explosion, and NaN contamination across the entire backward pass [1].
2. Matrix Calculus & Singular Extension
By Jacobi's formula, for any invertible matrix , the differential of the determinant satisfies [2]:
Because each entry of the classical adjugate is an cofactor minor (a polynomial in the entries of ), it requires zero division by and extends continuously across singular matrices:
| Matrix Condition | (Before) | Adjugate (After) |
|---|---|---|
| Invertible () | Finite gradient | Identical finite gradient |
| Singular () | NaN / inf crash | Exact continuous cofactor gradient |
3. Implementation & Verification
4. References
- Giles, An Extended Collection of Matrix Derivative Results for Forward and Reverse Mode AD, 2008.
- Petersen & Pedersen, The Matrix Cookbook, Section 2.1 (Derivatives of Determinants).
- Anandhu P Shaji, Hunting Compiler Bugs and Numerical Traps in TensorFlow,
anandhupshaji.me/posts/tensorflow-stories.html#singular-matrix-gradients.