Skip to content

Infinite Gradient Handling #582

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

Aziz-Shameem
Copy link

Added support for infinite gradient check as per this

Note that the method added also checks for nan values in grads along with infinite values, since both lead to the same errors in optimization and are therefore, related.

Let me know if any further changes/polishing is required. All the CI tests seem to pass.

Thanks

Copy link
Member

@timmens timmens left a comment

Choose a reason for hiding this comment

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

Thanks for the PR!

This looks pretty good already. I have added a few review comments to your changes.

Additionally: Can you create a new test file tests/optimagic/optimization/test_invalid_function_value.py, and add two test cases where you check the cases of

  1. Infinite values and
  2. NaN values

These tests should fail without your changes, and pass with your changes; i.e., you will have to check that your code raises the correct error! We have many such tests in our test suite -- I would just search for "pytest.raises".

Thanks again and let me know if you have any questions!

Comment on lines 478 to 479
# Check for infinite values in the user-provided gradient
self._check_infinite_gradients(params, out_jac, jac_value)
Copy link
Member

Choose a reason for hiding this comment

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

The comment is not necessary. Should be self-explanatory given the methods name. Also applies to the instances below.

Comment on lines 722 to 724
params: PyTree,
out_jac: NDArray[np.float64],
jac_value: PyTree,
Copy link
Member

Choose a reason for hiding this comment

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

I think the order should be (out_jac, jac_value, params)

"""Check for infinite values in gradients and raise an error if found.

Args:
x: internal parameter vector at which the gradient was evaluated.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
x: internal parameter vector at which the gradient was evaluated.

Comment on lines 730 to 733
params: user-facing parameter representation at evaluation point.
out_jac: internal processed gradient to check for infinities.
jac_value: original gradient value as returned by the user function,
included in error messages for debugging.
Copy link
Member

Choose a reason for hiding this comment

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

Adjust the order also here.

@@ -704,6 +717,33 @@ def _pure_evaluate_fun_and_jac(

return (algo_fun_value, out_jac), hist_entry, log_entry

def _check_infinite_gradients(
Copy link
Member

Choose a reason for hiding this comment

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

I prefer

def _assert_finite_jac(...)
 ...

instead of the negative form.

I would also speak of jac and not gradient, because we are dealing with the jacobian (it is only the gradient in the special case of scalar-valued function).

InvalidFunctionError: If any infinite values are found in the gradient.

"""
if np.any(np.isinf(out_jac)) or np.any(np.isnan(out_jac)):
Copy link
Member

Choose a reason for hiding this comment

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

Given the adjusted function name, I would also use

if not np.all(np.isfinite(out_jac))
...

here. For this you should then adjust the docstring and add a comment in the docstring that np.isfinite checks for infinity and NaN values (see np.isfinite docstring).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants