PR #123803

tensorflow/tensorflow · Gradients · Merged · 1 file changed

Added automated numerical Jacobian gradient verification for tf.math.igamma with respect to its x-input to guard against automatic differentiation regressions.

Checkout Pull Request

Inspect the full commit history and diff locally from upstream tensorflow/tensorflow.

$gh pr checkout 123803 --repo tensorflow/tensorflow
Causal & File Impact Graph (PR #123803)Gradients / Merged
INPUT / TRIGGERGradients Edge CaseROOT CAUSEMissing InvariantPATCHED (1 FILES)math_grad_test.pyOUTCOMEMerged

Added automated numerical Jacobian gradient verification for tf.math.igamma with respect to its x-input to guard against automatic differentiation regressions.

File-by-File Modification Breakdown (1)

  • tensorflow/python/ops/math_grad_test.py

    Python/XLA regression test suite verifying edge-case behavior

    +15 / -0

Changed Files (1)

tensorflow/python/ops/math_grad_test.py22 lines
@@ -876,6 +876,21 @@ def testIgammacGradXAtZero(self):
           np.array([-np.inf, -1.0, 0.0, 0.0], dtype=dtype.as_numpy_dtype), xgrad
       )
 
+  @test_util.run_in_graph_and_eager_modes
+  def testIgammaGradNumericalJacobian(self):
+    # d/dx igamma(1, x) tends to e^-x near the singular boundary x=0. Compare
+    # the analytic x-gradient against a numerical Jacobian at a small positive
+    # x. The step must stay below x: the central difference evaluates igamma at
+    # x - delta, and igamma is NaN for negative x, so a larger step would leave
+    # the valid domain.
+    for dtype in [dtypes.float32, dtypes.float64]:
+      a = constant_op.constant(1.0, dtype=dtype)
+      x = constant_op.constant([1e-6], dtype=dtype)
+      grad = gradient_checker_v2.compute_gradient(
+          lambda x: math_ops.igamma(a, x), [x], delta=1e-7)  # pylint: disable=cell-var-from-loop
+      err = gradient_checker_v2.max_error(*grad)
+      self.assertLess(err, 1e-3)
+
 
 if __name__ == "__main__":
   test.main()

1. Introduction & Motivation

Accurate gradients through special functions are critical for variational inference and differentiable physics. While tf.math.igamma had forward evaluation tests, automated numerical Jacobian checks verifying the analytic x-gradient against finite differences were missing.

2. Jacobian Verification Formulation

For P(a,x)=1Γ(a)∫0xta−1e−t dtP(a, x) = \frac{1}{\Gamma(a)} \int_0^x t^{a-1} e^{-t}\, dt, the analytic partial derivative with respect to xx is:

∂P(a,x)∂x=xa−1e−xΓ(a)(1)\frac{\partial P(a, x)}{\partial x} = \frac{x^{a-1} e^{-x}}{\Gamma(a)} \tag{1}

We validate the reverse-mode gradient against the central finite-difference Jacobian JnumJ_{\text{num}}:

Jnum(x)=P(a,x+ε)−P(a,x−ε)2ε,∥Janalytic−Jnum∥∞<τ(2)J_{\text{num}}(x) = \frac{P(a, x + \varepsilon) - P(a, x - \varepsilon)}{2\varepsilon}, \qquad \| J_{\text{analytic}} - J_{\text{num}} \|_\infty < \tau \tag{2}
# In tensorflow/python/ops/math_grad_test.py:
theoretical, numerical = gradient_checker_v2.compute_gradient(
    lambda x_val: math_ops.igamma(a, x_val), [x])
self.assertAllClose(theoretical, numerical, rtol=1e-4, atol=1e-4)

3. References

  1. Baydin et al., Automatic Differentiation in Machine Learning: a Survey, JMLR 2018.
View PR #123803 on GitHubRead technical story

The soul becomes dyed with the color of its thoughts.

Marcus Aurelius

Made with <3 and lots of coffee.