-
-
Notifications
You must be signed in to change notification settings - Fork 387
Newton optimizer: handle flat directions without producing NaN #3429
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
Open
SteveBronder
wants to merge
3
commits into
develop
Choose a base branch
from
fix/newton-flat-direction-3425
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
4691bee
Newton optimizer: handle flat directions without producing NaN
SteveBronder 2f30ed9
Newton eigenvalue cutoff: add Eigen's factor of 4 and clamp at smalle…
SteveBronder 5c598bb
Newton: floor small eigenvalues at sqrt(eps) instead of dropping them
SteveBronder 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
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,12 @@ | ||
| /** | ||
| * The target does not depend on x, so the gradient and Hessian | ||
| * are identically zero along that direction. Used to check that | ||
| * the Newton optimizer handles a flat direction without producing | ||
| * non-finite parameter values. | ||
| */ | ||
| parameters { | ||
| real x; | ||
| } | ||
| model { | ||
| target += 0.5; | ||
| } |
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,11 @@ | ||
| /** | ||
| * The target is linear in x, so the gradient is nonzero while the | ||
| * Hessian is identically zero. Used to check that the Newton optimizer | ||
| * still moves along a direction with no curvature. | ||
| */ | ||
| parameters { | ||
| real x; | ||
| } | ||
| model { | ||
| target += x; | ||
| } |
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,28 @@ | ||
| #include <gtest/gtest.h> | ||
| #include <stan/optimization/newton.hpp> | ||
| #include <stan/io/empty_var_context.hpp> | ||
| #include <test/test-models/good/optimization/linear_target.hpp> | ||
| #include <cmath> | ||
| #include <limits> | ||
| #include <vector> | ||
|
|
||
| typedef linear_target_model_namespace::linear_target_model Model; | ||
|
|
||
| TEST(OptimizationNewton, linear_target_moves_uphill_by_bounded_step) { | ||
| const double sqrt_eps = std::sqrt(std::numeric_limits<double>::epsilon()); | ||
| stan::io::empty_var_context dummy_context; | ||
| Model model(dummy_context); | ||
|
|
||
| std::vector<double> params_r(1, 0.0); | ||
| std::vector<int> params_i; | ||
|
|
||
| double f = stan::optimization::newton_step<Model, false>(model, params_r, | ||
| params_i); | ||
|
|
||
| ASSERT_EQ(1u, params_r.size()); | ||
| EXPECT_TRUE(std::isfinite(params_r[0])); | ||
| EXPECT_GT(params_r[0], 0.0) << "zero-curvature direction must still move"; | ||
| EXPECT_LE(params_r[0], 2.0 / sqrt_eps) | ||
| << "step along a zero-curvature direction must be bounded by the floor"; | ||
| EXPECT_GT(f, 0.0) << "objective must improve along a linear target"; | ||
| } |
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,101 @@ | ||
| #include <gtest/gtest.h> | ||
| #include <stan/optimization/newton.hpp> | ||
| #include <stan/io/empty_var_context.hpp> | ||
| #include <test/test-models/good/optimization/flat_target.hpp> | ||
| #include <cmath> | ||
| #include <limits> | ||
| #include <vector> | ||
|
|
||
| typedef flat_target_model_namespace::flat_target_model Model; | ||
|
|
||
| // Regression test for https://github.com/stan-dev/stan/issues/3425 | ||
| TEST(OptimizationNewton, flat_direction_keeps_parameters_finite) { | ||
| stan::io::empty_var_context dummy_context; | ||
| Model model(dummy_context); | ||
|
|
||
| std::vector<double> params_r(1, 1.0); | ||
| std::vector<int> params_i; | ||
|
|
||
| double f = stan::optimization::newton_step<Model, false>(model, params_r, | ||
| params_i); | ||
|
|
||
| EXPECT_FLOAT_EQ(0.5, f); | ||
| ASSERT_EQ(1u, params_r.size()); | ||
| EXPECT_TRUE(std::isfinite(params_r[0])) | ||
| << "newton_step produced non-finite parameter: " << params_r[0]; | ||
| } | ||
|
|
||
| TEST(OptimizationNewton, | ||
| make_negative_definite_and_solve_floors_small_eigenvalue_at_sqrt_eps) { | ||
| const double eps = std::numeric_limits<double>::epsilon(); | ||
| const double sqrt_eps = std::sqrt(eps); | ||
| stan::optimization::matrix_d H = stan::optimization::matrix_d::Zero(2, 2); | ||
| H(0, 0) = -1.0; | ||
| H(1, 1) = -4.0 * eps; | ||
| stan::optimization::vector_d g = stan::optimization::vector_d::Ones(2); | ||
|
|
||
| stan::optimization::make_negative_definite_and_solve(H, g); | ||
|
|
||
| EXPECT_FLOAT_EQ(-1.0, g[0]); | ||
| EXPECT_FLOAT_EQ(-1.0 / sqrt_eps, g[1]) | ||
| << "eigenvalue below sqrt(eps) * max should be floored, not dropped"; | ||
| } | ||
|
|
||
| TEST(OptimizationNewton, | ||
| make_negative_definite_and_solve_zero_eigenvalue_uses_relative_floor) { | ||
| const double sqrt_eps = std::sqrt(std::numeric_limits<double>::epsilon()); | ||
| stan::optimization::matrix_d H = stan::optimization::matrix_d::Zero(2, 2); | ||
| H(0, 0) = -1.0; | ||
| stan::optimization::vector_d g = stan::optimization::vector_d::Ones(2); | ||
|
|
||
| stan::optimization::make_negative_definite_and_solve(H, g); | ||
|
|
||
| EXPECT_FLOAT_EQ(-1.0, g[0]); | ||
| EXPECT_FLOAT_EQ(-1.0 / sqrt_eps, g[1]); | ||
| } | ||
|
|
||
| TEST(OptimizationNewton, | ||
| make_negative_definite_and_solve_is_continuous_at_old_cutoff) { | ||
| const double eps = std::numeric_limits<double>::epsilon(); | ||
| const double old_cutoff = 4.0 * 2 * eps; | ||
| stan::optimization::matrix_d H_above | ||
| = stan::optimization::matrix_d::Zero(2, 2); | ||
| H_above(0, 0) = -1.0; | ||
| H_above(1, 1) = -1.01 * old_cutoff; | ||
| stan::optimization::matrix_d H_below = H_above; | ||
| H_below(1, 1) = -0.99 * old_cutoff; | ||
| stan::optimization::vector_d g_above = stan::optimization::vector_d::Ones(2); | ||
| stan::optimization::vector_d g_below = g_above; | ||
|
|
||
| stan::optimization::make_negative_definite_and_solve(H_above, g_above); | ||
| stan::optimization::make_negative_definite_and_solve(H_below, g_below); | ||
|
|
||
| EXPECT_NEAR(g_above[1], g_below[1], 1e-6 * std::fabs(g_above[1])) | ||
| << "step must not jump when an eigenvalue crosses the cutoff"; | ||
| } | ||
|
|
||
| TEST(OptimizationNewton, | ||
| make_negative_definite_and_solve_zero_hessian_nonzero_gradient) { | ||
| const double sqrt_eps = std::sqrt(std::numeric_limits<double>::epsilon()); | ||
| stan::optimization::matrix_d H = stan::optimization::matrix_d::Zero(2, 2); | ||
| stan::optimization::vector_d g = stan::optimization::vector_d::Ones(2); | ||
|
|
||
| stan::optimization::make_negative_definite_and_solve(H, g); | ||
|
|
||
| for (int i = 0; i < g.size(); ++i) { | ||
| EXPECT_FLOAT_EQ(-1.0 / sqrt_eps, g[i]) | ||
| << "all-zero Hessian must use the absolute floor, component " << i; | ||
| } | ||
| } | ||
|
|
||
| TEST(OptimizationNewton, make_negative_definite_and_solve_zero_hessian) { | ||
| stan::optimization::matrix_d H = stan::optimization::matrix_d::Zero(2, 2); | ||
| stan::optimization::vector_d g = stan::optimization::vector_d::Zero(2); | ||
|
|
||
| stan::optimization::make_negative_definite_and_solve(H, g); | ||
|
|
||
| for (int i = 0; i < g.size(); ++i) { | ||
| EXPECT_TRUE(std::isfinite(g[i])) | ||
| << "step direction has non-finite component " << i << ": " << g[i]; | ||
| } | ||
| } |
44 changes: 44 additions & 0 deletions
44
src/test/unit/services/optimize/newton_flat_target_test.cpp
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,44 @@ | ||
| #include <stan/services/optimize/newton.hpp> | ||
| #include <gtest/gtest.h> | ||
| #include <stan/io/empty_var_context.hpp> | ||
| #include <test/test-models/good/optimization/flat_target.hpp> | ||
| #include <test/unit/services/instrumented_callbacks.hpp> | ||
| #include <stan/callbacks/stream_writer.hpp> | ||
| #include <cmath> | ||
|
|
||
| struct ServicesOptimizeNewtonFlatTarget : public testing::Test { | ||
| ServicesOptimizeNewtonFlatTarget() | ||
| : init(init_ss), parameter(parameter_ss), model(context, 0, &model_ss) {} | ||
|
|
||
| std::stringstream init_ss, parameter_ss, model_ss; | ||
| stan::test::unit::instrumented_logger logger; | ||
| stan::callbacks::stream_writer init; | ||
| stan::test::unit::values_writer parameter; | ||
| stan::io::empty_var_context context; | ||
| stan_model model; | ||
| }; | ||
|
|
||
| // Regression test for https://github.com/stan-dev/stan/issues/3425 | ||
| // The service must not report success while writing non-finite parameters. | ||
| TEST_F(ServicesOptimizeNewtonFlatTarget, does_not_report_ok_with_nan_params) { | ||
| unsigned int seed = 0; | ||
| unsigned int chain = 1; | ||
| double init_radius = 1; | ||
| int num_iterations = 10; | ||
| bool save_iterations = false; | ||
| stan::test::unit::instrumented_interrupt interrupt; | ||
|
|
||
| int return_code = stan::services::optimize::newton( | ||
| model, context, seed, chain, init_radius, num_iterations, save_iterations, | ||
| interrupt, logger, init, parameter); | ||
|
|
||
| ASSERT_EQ(3, parameter.names_.size()); | ||
| EXPECT_EQ("x", parameter.names_[2]); | ||
| ASSERT_EQ(1, parameter.states_.size()); | ||
|
|
||
| double x = parameter.states_.back()[2]; | ||
| EXPECT_TRUE(std::isfinite(x) | ||
| || return_code != stan::services::error_codes::OK) | ||
| << "newton returned error_codes::OK with x = " << x; | ||
| EXPECT_TRUE(std::isfinite(x)) << "final x = " << x; | ||
| } |
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.
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.
This looks like a reimplementation of
stan::math::is_scal_finite