-
Notifications
You must be signed in to change notification settings - Fork 48
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
Merged
+167
−2
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
375d062
Added support for infinite gradients
Aziz-Shameem 7d3ea5a
Added Infinite Gradient handling
Aziz-Shameem b9fc5d2
minor fixes
Aziz-Shameem 107b0b9
added tests
Aziz-Shameem d399ea4
final polishing
Aziz-Shameem e787b88
Merge branch 'main' into InfGrads
janosg 7910e13
added exhaustive tests
Aziz-Shameem 8980861
bug fixes
Aziz-Shameem 4989d2b
bug fixes
Aziz-Shameem deaedcf
Merge branch 'InfGrads' of https://github.com/Aziz-Shameem/optimagic …
Aziz-Shameem f92c967
Polishing
Aziz-Shameem 848acb0
Merge branch 'main' into InfGrads
timmens 763b204
Merge branch 'optimagic-dev:main' into InfGrads
Aziz-Shameem 6b84b37
Merge branch 'main' of https://github.com/Aziz-Shameem/optimagic into…
Aziz-Shameem eb81484
Merge branch 'InfGrads' of https://github.com/Aziz-Shameem/optimagic …
Aziz-Shameem 2806349
polishing
Aziz-Shameem 7e36af6
Final Changes
Aziz-Shameem File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
tests/optimagic/optimization/test_invalid_jacobian_value.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| import numpy as np | ||
| import pytest | ||
|
|
||
| from optimagic.exceptions import UserFunctionRuntimeError | ||
| from optimagic.optimization.optimize import minimize | ||
|
|
||
| # ====================================================================================== | ||
| # Test setup: | ||
| # -------------------------------------------------------------------------------------- | ||
| # We test that minimize raises an error if the user function returns a jacobian | ||
| # containing invalid values (np.inf, np.nan). To test that this works not only at | ||
| # the start parameters, we create jac functions that return invalid values if the | ||
| # parameter norm becomes smaller than one. | ||
| # ====================================================================================== | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def params(): | ||
| return {"a": 1, "b": np.array([3, 4])} | ||
|
|
||
|
|
||
| def sphere(params): | ||
| return params["a"] ** 2 + (params["b"] ** 2).sum() | ||
|
|
||
|
|
||
| def sphere_gradient(params): | ||
| return { | ||
| "a": 2 * params["a"], | ||
| "b": 2 * params["b"], | ||
| } | ||
|
|
||
|
|
||
| def sphere_and_gradient(params): | ||
| return sphere(params), sphere_gradient(params) | ||
|
|
||
|
|
||
| def params_norm(params): | ||
| squared_norm = params["a"] ** 2 + np.linalg.norm(params["b"]) ** 2 | ||
| return np.sqrt(squared_norm) | ||
|
|
||
|
|
||
| def get_invalid_jac(invalid_jac_value): | ||
| """Get function that returns invalid jac if the parameter norm < 1.""" | ||
|
|
||
| def jac(params): | ||
| if params_norm(params) < 1: | ||
| return invalid_jac_value | ||
| else: | ||
| return sphere_gradient(params) | ||
|
|
||
| return jac | ||
|
|
||
|
|
||
| def get_invalid_fun_and_jac(invalid_jac_value): | ||
| """Get function that returns invalid fun and jac if the parameter norm < 1.""" | ||
|
|
||
| def fun_and_jac(params): | ||
| if params_norm(params) < 1: | ||
| return sphere(params), invalid_jac_value | ||
| else: | ||
| return sphere_and_gradient(params) | ||
|
|
||
| return fun_and_jac | ||
|
|
||
|
|
||
| INVALID_JACOBIAN_VALUES = [ | ||
| {"a": np.inf, "b": 2 * np.array([1, 2])}, | ||
| {"a": 1, "b": 2 * np.array([np.inf, 2])}, | ||
| {"a": np.nan, "b": 2 * np.array([1, 2])}, | ||
| {"a": 1, "b": 2 * np.array([np.nan, 2])}, | ||
| ] | ||
|
|
||
|
|
||
| # ====================================================================================== | ||
| # Test Invalid Jacobian raises proper error with jac argument | ||
| # ====================================================================================== | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("invalid_jac_value", INVALID_JACOBIAN_VALUES) | ||
| def test_minimize_with_invalid_jac(invalid_jac_value, params): | ||
| with pytest.raises( | ||
| UserFunctionRuntimeError, | ||
| match=( | ||
| "The optimization failed because the derivative provided via jac " | ||
| "contains infinite or NaN values." | ||
| ), | ||
| ): | ||
| minimize( | ||
| fun=sphere, | ||
| params=params, | ||
| algorithm="scipy_lbfgsb", | ||
| jac=get_invalid_jac(invalid_jac_value), | ||
| ) | ||
|
|
||
|
|
||
| # ====================================================================================== | ||
| # Test Invalid Jacobian raises proper error with fun_and_jac argument | ||
| # ====================================================================================== | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("invalid_jac_value", INVALID_JACOBIAN_VALUES) | ||
| def test_minimize_with_invalid_fun_and_jac(invalid_jac_value, params): | ||
| with pytest.raises( | ||
| UserFunctionRuntimeError, | ||
| match=( | ||
| "The optimization failed because the derivative provided via fun_and_jac " | ||
| "contains infinite or NaN values." | ||
| ), | ||
| ): | ||
| minimize( | ||
| params=params, | ||
| algorithm="scipy_lbfgsb", | ||
| fun_and_jac=get_invalid_fun_and_jac(invalid_jac_value), | ||
| ) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.