Commit ad8fb3ff authored by Davis King's avatar Davis King

Made the line search a little more robust.

--HG--
extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%403516
parent 1a28cca6
...@@ -268,12 +268,10 @@ namespace dlib ...@@ -268,12 +268,10 @@ namespace dlib
- for all i: #dot_prods[i] == dot(colm(#w,0,w.size()-1), samples(i)) - #w(w.size()-1) - for all i: #dot_prods[i] == dot(colm(#w,0,w.size()-1), samples(i)) - #w(w.size()-1)
!*/ !*/
{ {
const scalar_type mu = 0.1;
for (long i = 0; i < samples.size(); ++i) for (long i = 0; i < samples.size(); ++i)
dot_prods[i] = dot_helper(w,samples(i)) - w(w.size()-1); dot_prods[i] = dot_helper(w,samples(i)) - w(w.size()-1);
if (is_first_call) if (is_first_call)
{ {
is_first_call = false; is_first_call = false;
...@@ -313,18 +311,23 @@ namespace dlib ...@@ -313,18 +311,23 @@ namespace dlib
f0 += B; f0 += B;
} }
scalar_type opt_k = 1;
// ks.size() == 0 shouldn't happen but check anyway // ks.size() == 0 shouldn't happen but check anyway
if (f0 >= 0 || ks.size() == 0) if (f0 >= 0 || ks.size() == 0)
{ {
// getting here means that we aren't searching in a descent direction. So don't // Getting here means that we aren't searching in a descent direction.
// move the best_so_far position. // We could take a zero step but instead lets just assign w to the new best
// so far point just to make sure we don't get stuck coming back to this
// case over and over. This might happen if we never move the best point
// seen so far.
// So we let opt_k be 1
} }
else else
{ {
std::sort(ks.begin(), ks.end()); std::sort(ks.begin(), ks.end());
// figure out where f0 goes positive. // figure out where f0 goes positive.
scalar_type opt_k = 1;
for (unsigned long i = 0; i < ks.size(); ++i) for (unsigned long i = 0; i < ks.size(); ++i)
{ {
f0 += ks[i].B; f0 += ks[i].B;
...@@ -335,17 +338,21 @@ namespace dlib ...@@ -335,17 +338,21 @@ namespace dlib
} }
} }
// take the step suggested by the line search
best_so_far = (1-opt_k)*best_so_far + opt_k*w;
// update best_so_far dot products
for (unsigned long i = 0; i < dot_prods_best.size(); ++i)
dot_prods_best[i] = (1-opt_k)*dot_prods_best[i] + opt_k*dot_prods[i];
} }
// Put the best_so_far point into w but also take a little bit of w as well. We do // take the step suggested by the line search
// this since it is possible that some steps won't advance the best_so_far point. best_so_far = (1-opt_k)*best_so_far + opt_k*w;
// So this ensures we always make some progress each iteration.
// update best_so_far dot products
for (unsigned long i = 0; i < dot_prods_best.size(); ++i)
dot_prods_best[i] = (1-opt_k)*dot_prods_best[i] + opt_k*dot_prods[i];
const scalar_type mu = 0.1;
// Make sure we always take a little bit of a step twoards w regardless of what the
// line search says to do. We do this since it is possible that some steps won't
// advance the best_so_far point. So this ensures we always make some progress each
// iteration.
w = (1-mu)*best_so_far + mu*w; w = (1-mu)*best_so_far + mu*w;
// update dot products // update dot products
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment