Skip to content

Commit

Permalink
Fix line search to avoid non-finite gradients
Browse files Browse the repository at this point in the history
Related to stan-dev#3306

Modify the `WolfeLineSearch` function in `src/stan/optimization/bfgs_linesearch.hpp` to handle non-finite gradients.

* Check if the function value `func_val` is finite.
* Check if the gradient `gradx1` is finite.
* If either the function value or the gradient is non-finite, restart the line search with a smaller step size.

---

For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/stan-dev/stan/issues/3306?shareId=XXXX-XXXX-XXXX-XXXX).
  • Loading branch information
cpfiffer committed Aug 5, 2024
1 parent 01f5923 commit 801093b
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion src/stan/optimization/bfgs_linesearch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ int WolfeLineSearch(FunctorType &func, Scalar &alpha, XType &x1,

x1.noalias() = x0 + alpha1 * p;
ret = func(x1, func_val, gradx1);
if (ret != 0) {
if (ret != 0 || !std::isfinite(func_val) || !gradx1.allFinite()) {
if (lsRestarts >= maxLSRestarts) {
retCode = 1;
break;
Expand Down

0 comments on commit 801093b

Please sign in to comment.