Commit 9a1be53
committed
Avoid division by zero in
The `update` subfunction in `adagrad()` will produce division-by-zero
errors. These occur when running `check/pytest` in Python 3.12 with
NumPy 2.x:
```
src/openfermion/resource_estimates/pbc/thc/factorizations/thc_jax_test.py::test_kpoint_thc_helper
src/openfermion/resource_estimates/pbc/thc/generate_costing_table_thc_test.py::test_generate_costing_table_thc
/usr/local/google/home/mhucka/project-files/google/github/openfermion/src/openfermion/resource_estimates/thc/utils/adagrad.py:43:
RuntimeWarning: divide by zero encountered in divide
g_sq_inv_sqrt = np.where(g_sq > 0, 1.0 / np.sqrt(g_sq), 0.0)
```
A common method to avoid this situation is to add a small positive
number to `g_sq` before taking the square root. This ensures that the
value is never exactly zero, avoiding the division-by-zero problem.
This approach is a common and robust method for avoiding zero
denominators in gradient-based optimization. It also acts as a
smoothing factor, preventing the update from becoming unstable when
`g_sq` gets very small.adagrad
1 parent 7f04e57 commit 9a1be53
1 file changed
Lines changed: 4 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
37 | 37 | | |
38 | 38 | | |
39 | 39 | | |
40 | | - | |
| 40 | + | |
41 | 41 | | |
42 | 42 | | |
43 | | - | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
44 | 46 | | |
45 | 47 | | |
46 | 48 | | |
| |||
0 commit comments