Skip to content
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

Fix line search to avoid non-finite gradients #3309

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
123 changes: 123 additions & 0 deletions src/test/unit/optimization/bfgs_linesearch_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,126 @@ TEST(OptimizationBfgsLinesearch, wolfeLineSearch) {
EXPECT_LE(f1, f0 + c1 * alpha * p.dot(gradx0));
EXPECT_LE(std::fabs(p.dot(gradx1)), c2 * std::fabs(p.dot(gradx0)));
}

class linesearch_testfunc_nonfinite {
public:
double operator()(const Eigen::Matrix<double, Eigen::Dynamic, 1> &x) {
return x.dot(x) - 1.0;
}
int operator()(const Eigen::Matrix<double, Eigen::Dynamic, 1> &x, double &f,
Eigen::Matrix<double, Eigen::Dynamic, 1> &g) {
f = x.dot(x) - 1.0;
g = 2.0 * x;
if (!g.allFinite()) {
return 1;
}
return 0;
}
};

TEST(OptimizationBfgsLinesearch, wolfeLineSearch_nonfinite_gradient) {
using stan::optimization::WolfeLineSearch;

static const double c1 = 1e-4;
static const double c2 = 0.9;
static const double minAlpha = 1e-16;
static const double maxLSIts = 20;
static const double maxLSRestarts = 10;

linesearch_testfunc_nonfinite func1;
Eigen::Matrix<double, -1, 1> x0, x1;
double f0, f1;
Eigen::Matrix<double, -1, 1> p, gradx0, gradx1;
double alpha;
int ret;

x0.setOnes(5, 1);
func1(x0, f0, gradx0);

p = -gradx0;

alpha = 2.0;
ret = WolfeLineSearch(func1, alpha, x1, f1, gradx1, p, x0, f0, gradx0, c1, c2,
minAlpha, maxLSIts, maxLSRestarts);
EXPECT_EQ(1, ret);
}

class linesearch_testfunc_nan {
public:
double operator()(const Eigen::Matrix<double, Eigen::Dynamic, 1> &x) {
return std::numeric_limits<double>::quiet_NaN();
}
int operator()(const Eigen::Matrix<double, Eigen::Dynamic, 1> &x, double &f,
Eigen::Matrix<double, Eigen::Dynamic, 1> &g) {
f = std::numeric_limits<double>::quiet_NaN();
g = 2.0 * x;
return 1;
}
};

TEST(OptimizationBfgsLinesearch, wolfeLineSearch_nan) {
using stan::optimization::WolfeLineSearch;

static const double c1 = 1e-4;
static const double c2 = 0.9;
static const double minAlpha = 1e-16;
static const double maxLSIts = 20;
static const double maxLSRestarts = 10;

linesearch_testfunc_nan func1;
Eigen::Matrix<double, -1, 1> x0, x1;
double f0, f1;
Eigen::Matrix<double, -1, 1> p, gradx0, gradx1;
double alpha;
int ret;

x0.setOnes(5, 1);
func1(x0, f0, gradx0);

p = -gradx0;

alpha = 2.0;
ret = WolfeLineSearch(func1, alpha, x1, f1, gradx1, p, x0, f0, gradx0, c1, c2,
minAlpha, maxLSIts, maxLSRestarts);
EXPECT_EQ(1, ret);
}

class linesearch_testfunc_inf {
public:
double operator()(const Eigen::Matrix<double, Eigen::Dynamic, 1> &x) {
return std::numeric_limits<double>::infinity();
}
int operator()(const Eigen::Matrix<double, Eigen::Dynamic, 1> &x, double &f,
Eigen::Matrix<double, Eigen::Dynamic, 1> &g) {
f = std::numeric_limits<double>::infinity();
g = 2.0 * x;
return 1;
}
};

TEST(OptimizationBfgsLinesearch, wolfeLineSearch_inf) {
using stan::optimization::WolfeLineSearch;

static const double c1 = 1e-4;
static const double c2 = 0.9;
static const double minAlpha = 1e-16;
static const double maxLSIts = 20;
static const double maxLSRestarts = 10;

linesearch_testfunc_inf func1;
Eigen::Matrix<double, -1, 1> x0, x1;
double f0, f1;
Eigen::Matrix<double, -1, 1> p, gradx0, gradx1;
double alpha;
int ret;

x0.setOnes(5, 1);
func1(x0, f0, gradx0);

p = -gradx0;

alpha = 2.0;
ret = WolfeLineSearch(func1, alpha, x1, f1, gradx1, p, x0, f0, gradx0, c1, c2,
minAlpha, maxLSIts, maxLSRestarts);
EXPECT_EQ(1, ret);
}
Loading