Added some asserts into the optimization code to detect when the user

accidentally creates objective functions which output infinite or NaN values.
This commit is contained in:
Davis King 2013-08-08 10:20:38 -04:00
parent 625488ee34
commit ec4865ed60
2 changed files with 25 additions and 0 deletions

View File

@ -188,6 +188,9 @@ namespace dlib
double f_value = f(x);
g = der(x);
DLIB_ASSERT(is_finite(f_value), "The objective function generated non-finite outputs");
DLIB_ASSERT(is_finite(g), "The objective function generated non-finite outputs");
while(stop_strategy.should_continue_search(x, f_value, g) && f_value > min_f)
{
s = search_strategy.get_next_direction(x, f_value, g);
@ -202,6 +205,9 @@ namespace dlib
// Take the search step indicated by the above line search
x += alpha*s;
DLIB_ASSERT(is_finite(f_value), "The objective function generated non-finite outputs");
DLIB_ASSERT(is_finite(g), "The objective function generated non-finite outputs");
}
return f_value;
@ -249,6 +255,9 @@ namespace dlib
double f_value = -f(x);
g = -der(x);
DLIB_ASSERT(is_finite(f_value), "The objective function generated non-finite outputs");
DLIB_ASSERT(is_finite(g), "The objective function generated non-finite outputs");
while(stop_strategy.should_continue_search(x, f_value, g) && f_value > -max_f)
{
s = search_strategy.get_next_direction(x, f_value, g);
@ -269,6 +278,9 @@ namespace dlib
// from the unnegated versions of f() and der()
g *= -1;
f_value *= -1;
DLIB_ASSERT(is_finite(f_value), "The objective function generated non-finite outputs");
DLIB_ASSERT(is_finite(g), "The objective function generated non-finite outputs");
}
return -f_value;
@ -312,6 +324,9 @@ namespace dlib
double f_value = f(x);
g = derivative(f,derivative_eps)(x);
DLIB_ASSERT(is_finite(f_value), "The objective function generated non-finite outputs");
DLIB_ASSERT(is_finite(g), "The objective function generated non-finite outputs");
while(stop_strategy.should_continue_search(x, f_value, g) && f_value > min_f)
{
s = search_strategy.get_next_direction(x, f_value, g);
@ -330,6 +345,9 @@ namespace dlib
x += alpha*s;
g = derivative(f,derivative_eps)(x);
DLIB_ASSERT(is_finite(f_value), "The objective function generated non-finite outputs");
DLIB_ASSERT(is_finite(g), "The objective function generated non-finite outputs");
}
return f_value;

View File

@ -264,6 +264,9 @@ namespace dlib
model.get_derivative_and_hessian(x,g,h);
DLIB_ASSERT(is_finite(x), "The objective function generated non-finite outputs");
DLIB_ASSERT(is_finite(g), "The objective function generated non-finite outputs");
DLIB_ASSERT(is_finite(h), "The objective function generated non-finite outputs");
// Sometimes the loop below won't modify x because the trust region step failed.
// This bool tells us when we are in that case.
@ -323,6 +326,10 @@ namespace dlib
{
stale_x = true;
}
DLIB_ASSERT(is_finite(x), "The objective function generated non-finite outputs");
DLIB_ASSERT(is_finite(g), "The objective function generated non-finite outputs");
DLIB_ASSERT(is_finite(h), "The objective function generated non-finite outputs");
}
return f_value;