-
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
Merged
Changes from 1 commit
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,7 @@ | |
| import warnings | ||
| from copy import copy | ||
| from dataclasses import asdict, dataclass, replace | ||
| from typing import Any, Callable, cast | ||
| from typing import Any, Callable, Literal, cast | ||
|
|
||
| import numpy as np | ||
| from numpy.typing import NDArray | ||
|
|
@@ -11,10 +11,7 @@ | |
| from optimagic.batch_evaluators import process_batch_evaluator | ||
| from optimagic.differentiation.derivatives import first_derivative | ||
| from optimagic.differentiation.numdiff_options import NumdiffOptions | ||
| from optimagic.exceptions import ( | ||
| UserFunctionRuntimeError, | ||
| get_traceback, | ||
| ) | ||
| from optimagic.exceptions import UserFunctionRuntimeError, get_traceback | ||
| from optimagic.logging.logger import LogStore | ||
| from optimagic.logging.types import IterationState | ||
| from optimagic.optimization.fun_value import ( | ||
|
|
@@ -474,7 +471,7 @@ def _pure_evaluate_jac( | |
| out_jac = _process_jac_value( | ||
| value=jac_value, direction=self._direction, converter=self._converter, x=x | ||
| ) | ||
| self._assert_finite_jac(out_jac, jac_value, params) | ||
| _assert_finite_jac(out_jac, jac_value, params, "jac") | ||
|
|
||
| stop_time = time.perf_counter() | ||
|
|
||
|
|
@@ -548,7 +545,7 @@ def func(x: NDArray[np.float64]) -> SpecificFunctionValue: | |
| warnings.warn(msg) | ||
| fun_value, jac_value = self._error_penalty_func(x) | ||
|
|
||
| self._assert_finite_jac(jac_value, jac_value, params) | ||
| _assert_finite_jac(jac_value, jac_value, params, "numerical") | ||
|
|
||
| algo_fun_value, hist_fun_value = _process_fun_value( | ||
| value=fun_value, # type: ignore | ||
|
|
@@ -689,7 +686,7 @@ def _pure_evaluate_fun_and_jac( | |
| if self._direction == Direction.MAXIMIZE: | ||
| out_jac = -out_jac | ||
|
|
||
| self._assert_finite_jac(out_jac, jac_value, params) | ||
| _assert_finite_jac(out_jac, jac_value, params, "fun_and_jac") | ||
|
|
||
| stop_time = time.perf_counter() | ||
|
|
||
|
|
@@ -713,31 +710,48 @@ def _pure_evaluate_fun_and_jac( | |
|
|
||
| return (algo_fun_value, out_jac), hist_entry, log_entry | ||
|
|
||
| def _assert_finite_jac( | ||
| self, out_jac: NDArray[np.float64], jac_value: PyTree, params: PyTree | ||
| ) -> None: | ||
| """Check for infinite and NaN values in the jacobian and raise an error if | ||
| found. | ||
|
|
||
| Args: | ||
| out_jac: internal processed jacobian to check for infinities. | ||
| jac_value: original jacobian value as returned by the user function, | ||
| included in error messages for debugging. | ||
| params: user-facing parameter representation at evaluation point. | ||
| def _assert_finite_jac( | ||
| out_jac: NDArray[np.float64], | ||
| jac_value: PyTree, | ||
| params: PyTree, | ||
| origin: Literal["numerical", "jac", "fun_and_jac"], | ||
| ) -> None: | ||
| """Check for infinite and NaN values in the Jacobian and raise an error if found. | ||
|
|
||
| Raises: | ||
| UserFunctionRuntimeError: If any infinite values are found in the jacobian. | ||
| Args: | ||
| out_jac: internal processed Jacobian to check for finiteness. | ||
| jac_value: original Jacobian value as returned by the user function, | ||
| params: user-facing parameter representation at evaluation point. | ||
timmens marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| """ | ||
| if not np.all(np.isfinite(out_jac)): | ||
| Raises: | ||
| UserFunctionRuntimeError: | ||
| If any infinite or NaN values are found in the Jacobian. | ||
|
|
||
| """ | ||
| if not np.all(np.isfinite(out_jac)): | ||
| if origin == "jac": | ||
| msg = ( | ||
| "The optimization failed because the derivative provided via " | ||
| "jac contains infinite or NaN values." | ||
|
||
| "\nPlease validate the derivative function." | ||
| ) | ||
| elif origin == "fun_and_jac": | ||
| msg = ( | ||
| "The optimization received Jacobian containing infinite " | ||
| "or NaN values.\nCheck your objective function or its " | ||
| "jacobian, or try a different optimizer.\n" | ||
| f"Parameters at evaluation point: {params}\n" | ||
| f"Jacobian values: {jac_value}" | ||
| "The optimization failed because the derivative provided via " | ||
| "fun_and_jac contains infinite or NaN values." | ||
| "\nPlease validate the derivative function." | ||
| ) | ||
| raise UserFunctionRuntimeError(msg) | ||
| elif origin == "numerical": | ||
| msg = ( | ||
| "The optimization failed because the numerical derivative " | ||
| "(computed using fun) contains infinite or NaN values." | ||
| "\nPlease validate the criterion function or try a different optimizer." | ||
| ) | ||
| msg += ( | ||
| f"\nParameters at evaluation point: {params}\nJacobian values: {jac_value}" | ||
| ) | ||
| raise UserFunctionRuntimeError(msg) | ||
|
|
||
|
|
||
| def _process_fun_value( | ||
|
|
||
Oops, something went wrong.
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.