Skip to content

FIX np.divide undefined behaviour with where in gaussian processes #24245

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions doc/whats_new/v1.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,13 @@ Changelog
:mod:`sklearn.feature_selection`
................................

:mod:`sklearn.gaussian_process`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This section seems to be out of place in the whats new.

...............................

- |Fix| Fix :class:`gaussian_process.kernels.Matern` gradient computation with
`nu=0.5` for PyPy (and possibly other non CPython interpreters). :pr:`24245`
by :user:`Loïc Estève <lesteve>`.

:mod:`sklearn.linear_model`
...........................

Expand Down
9 changes: 7 additions & 2 deletions sklearn/gaussian_process/kernels.py
Original file line number Diff line number Diff line change
Expand Up @@ -1733,9 +1733,14 @@ def __call__(self, X, Y=None, eval_gradient=False):

if self.nu == 0.5:
denominator = np.sqrt(D.sum(axis=2))[:, :, np.newaxis]
K_gradient = K[..., np.newaxis] * np.divide(
D, denominator, where=denominator != 0
divide_result = np.zeros_like(D)
np.divide(
D,
denominator,
out=divide_result,
where=denominator != 0,
)
K_gradient = K[..., np.newaxis] * divide_result
elif self.nu == 1.5:
K_gradient = 3 * D * np.exp(-np.sqrt(3 * D.sum(-1)))[..., np.newaxis]
elif self.nu == 2.5:
Expand Down
3 changes: 0 additions & 3 deletions sklearn/gaussian_process/tests/test_kernels.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
assert_array_equal,
assert_array_almost_equal,
assert_allclose,
fails_if_pypy,
)


Expand Down Expand Up @@ -70,8 +69,6 @@
kernels.append(PairwiseKernel(gamma=1.0, metric=metric))


# Numerical precisions errors in PyPy
@fails_if_pypy
@pytest.mark.parametrize("kernel", kernels)
def test_kernel_gradient(kernel):
# Compare analytic and numeric gradient of kernels.
Expand Down