-
Notifications
You must be signed in to change notification settings - Fork 45
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
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this 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
- Infinite values and
- 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!
# Check for infinite values in the user-provided gradient | ||
self._check_infinite_gradients(params, out_jac, jac_value) |
There was a problem hiding this comment.
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.
params: PyTree, | ||
out_jac: NDArray[np.float64], | ||
jac_value: PyTree, |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
x: internal parameter vector at which the gradient was evaluated. |
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. |
There was a problem hiding this comment.
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( |
There was a problem hiding this comment.
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)): |
There was a problem hiding this comment.
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).
Added support for infinite gradient check as per this
Note that the method added also checks for
nan
values in grads along withinfinite
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