Commit d286560e authored by Davis King's avatar Davis King

Added be_verbose() functions to the stop strategies. Now you can tell them

to print the progress of an optimization algorithm to standard out.

--HG--
extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%403772
parent 4901ed60
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include "../matrix.h" #include "../matrix.h"
#include "../algs.h" #include "../algs.h"
#include "optimization_stop_strategies_abstract.h" #include "optimization_stop_strategies_abstract.h"
#include <iostream>
namespace dlib namespace dlib
{ {
...@@ -19,7 +20,7 @@ namespace dlib ...@@ -19,7 +20,7 @@ namespace dlib
public: public:
objective_delta_stop_strategy ( objective_delta_stop_strategy (
double min_delta = 1e-7 double min_delta = 1e-7
) : _been_used(false), _min_delta(min_delta), _max_iter(0), _cur_iter(0), _prev_funct_value(0) ) : _verbose(false), _been_used(false), _min_delta(min_delta), _max_iter(0), _cur_iter(0), _prev_funct_value(0)
{ {
DLIB_ASSERT ( DLIB_ASSERT (
min_delta >= 0, min_delta >= 0,
...@@ -32,7 +33,7 @@ namespace dlib ...@@ -32,7 +33,7 @@ namespace dlib
objective_delta_stop_strategy ( objective_delta_stop_strategy (
double min_delta, double min_delta,
unsigned long max_iter unsigned long max_iter
) : _been_used(false), _min_delta(min_delta), _max_iter(max_iter), _cur_iter(0), _prev_funct_value(0) ) : _verbose(false), _been_used(false), _min_delta(min_delta), _max_iter(max_iter), _cur_iter(0), _prev_funct_value(0)
{ {
DLIB_ASSERT ( DLIB_ASSERT (
min_delta >= 0 && max_iter > 0, min_delta >= 0 && max_iter > 0,
...@@ -43,6 +44,13 @@ namespace dlib ...@@ -43,6 +44,13 @@ namespace dlib
); );
} }
const objective_delta_stop_strategy& be_verbose(
) const
{
_verbose = true;
return *this;
}
template <typename T> template <typename T>
bool should_continue_search ( bool should_continue_search (
const T& , const T& ,
...@@ -50,6 +58,12 @@ namespace dlib ...@@ -50,6 +58,12 @@ namespace dlib
const T& const T&
) )
{ {
if (_verbose)
{
using namespace std;
cout << "iteration: " << _cur_iter << " objective: " << funct_value << endl;
}
++_cur_iter; ++_cur_iter;
if (_been_used) if (_been_used)
{ {
...@@ -69,6 +83,7 @@ namespace dlib ...@@ -69,6 +83,7 @@ namespace dlib
} }
private: private:
mutable bool _verbose;
bool _been_used; bool _been_used;
double _min_delta; double _min_delta;
...@@ -84,7 +99,7 @@ namespace dlib ...@@ -84,7 +99,7 @@ namespace dlib
public: public:
gradient_norm_stop_strategy ( gradient_norm_stop_strategy (
double min_norm = 1e-7 double min_norm = 1e-7
) : _min_norm(min_norm), _max_iter(0), _cur_iter(0) ) : _verbose(false), _min_norm(min_norm), _max_iter(0), _cur_iter(0)
{ {
DLIB_ASSERT ( DLIB_ASSERT (
min_norm >= 0, min_norm >= 0,
...@@ -97,7 +112,7 @@ namespace dlib ...@@ -97,7 +112,7 @@ namespace dlib
gradient_norm_stop_strategy ( gradient_norm_stop_strategy (
double min_norm, double min_norm,
unsigned long max_iter unsigned long max_iter
) : _min_norm(min_norm), _max_iter(max_iter), _cur_iter(0) ) : _verbose(false), _min_norm(min_norm), _max_iter(max_iter), _cur_iter(0)
{ {
DLIB_ASSERT ( DLIB_ASSERT (
min_norm >= 0 && max_iter > 0, min_norm >= 0 && max_iter > 0,
...@@ -108,13 +123,26 @@ namespace dlib ...@@ -108,13 +123,26 @@ namespace dlib
); );
} }
const gradient_norm_stop_strategy& be_verbose(
) const
{
_verbose = true;
return *this;
}
template <typename T> template <typename T>
bool should_continue_search ( bool should_continue_search (
const T& , const T& ,
const double , const double funct_value,
const T& funct_derivative const T& funct_derivative
) )
{ {
if (_verbose)
{
using namespace std;
cout << "iteration: " << _cur_iter << " objective: " << funct_value << " derivative norm: " << length(funct_derivative) << endl;
}
++_cur_iter; ++_cur_iter;
// Check if we have hit the max allowable number of iterations. (but only // Check if we have hit the max allowable number of iterations. (but only
...@@ -130,6 +158,7 @@ namespace dlib ...@@ -130,6 +158,7 @@ namespace dlib
} }
private: private:
mutable bool _verbose;
double _min_norm; double _min_norm;
unsigned long _max_iter; unsigned long _max_iter;
......
...@@ -53,6 +53,15 @@ namespace dlib ...@@ -53,6 +53,15 @@ namespace dlib
executed. executed.
!*/ !*/
const objective_delta_stop_strategy& be_verbose(
) const;
/*!
ensures
- causes this object to print status messages to standard out
every time should_continue_search() is called.
- returns *this
!*/
template <typename T> template <typename T>
bool should_continue_search ( bool should_continue_search (
const T& x, const T& x,
...@@ -111,6 +120,15 @@ namespace dlib ...@@ -111,6 +120,15 @@ namespace dlib
max_iter iterations has been executed. max_iter iterations has been executed.
!*/ !*/
const gradient_norm_stop_strategy& be_verbose(
) const;
/*!
ensures
- causes this object to print status messages to standard out
every time should_continue_search() is called.
- returns *this
!*/
template <typename T> template <typename T>
bool should_continue_search ( bool should_continue_search (
const T& x, const T& x,
......
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