00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00032 
00033 
00034 
00035 
00036 
00037  
00038 
00039 
00040 
00041 
00042 
00043 
00044 
00047 
#ifndef OPTIMIZER_INC
00048 
#define OPTIMIZER_INC
00049 
00050 
#include <plearn/var/Func.h>
00051 
#include <plearn/math/Mat.h>
00052 
#include <plearn/measure/Measurer.h>
00053 
#include <plearn/base/Object.h>
00054 
#include <plearn/math/VecStatsCollector.h>
00055 
#include <plearn/vmat/VMat.h>
00056 
00057 
namespace PLearn {
00058 
using namespace std;
00059 
00060 
00061 #define ALL_SAMPLES (-1)
00062 #define DEFAULT_SAMPLES (-2)
00063 
00064 
00065 
00066   class Optimizer : 
public Object
00067   {
00068       typedef Object inherited;
00069       
00070     
public:
00071       VarArray params;
00072       Var cost;
00073       VarArray proppath; 
00074       int nupdates; 
00075       int nstages; 
00076       int stage;   
00077 
00078       bool early_stop;
00079       int early_stop_i;
00080 
00081       VarArray update_for_measure; 
00082       
00083 
00084       
00085       PStream vlog;
00086       
00087       
00088     
private:
00089       Vec temp_grad;  
00090 
00091 
00092     
protected:
00093 
00094       Array<Measurer*> measurers;
00095 
00098       string filename; 
00099       
00100   
public:
00101       int every; 
00102 
00103     
public:
00104       
Optimizer(
int n_updates=1, 
const string& file_name=
"",
00105                 
int every_iterations=1);
00106       
Optimizer(
VarArray the_params, 
Var the_cost,
00107                 
int n_updates=1, 
const string& file_name=
"",
00108                 
int every_iterations=1);
00109       
Optimizer(
VarArray the_params, 
Var the_cost, 
00110                 
VarArray the_update_for_measure,
00111                 
int n_updates=1, 
const string& file_name=
"",
00112                 
int every_iterations=1);
00113       
00114       virtual void init() { 
build(); } 
00115       
virtual void build();
00116 
00117   
private:
00118       
void build_();
00119 
00120   
public:
00121 
00122       
virtual void reset(); 
00123 
00124       
virtual void setToOptimize(
VarArray the_params, 
Var the_cost);
00125       
00126       
virtual void setVarArrayOption(
const string& optionname, 
VarArray value);
00127       
virtual void setVarOption(
const string& optionname, 
Var value);
00128       
virtual void setVMatOption(
const string& optionname, 
VMat value);
00129       
00130       
PLEARN_DECLARE_ABSTRACT_OBJECT(
Optimizer);
00131       
virtual void makeDeepCopyFromShallowCopy(map<const void*, void*>& copies);
00132       
00133       
void addMeasurer(
Measurer& measurer);
00134       
00135       
virtual bool measure(
int t, 
const Vec& costs);
00136       
00138       
virtual real optimize() = 0;
00139 
00141       
virtual bool optimizeN(
VecStatsCollector& stats_coll) =0;
00142       
00143 
00144 
00145 
00146 
00147 
00148 
00149 
00150       
00153       
void verifyGradient(
real minval, 
real maxval, 
real step);
00154       
00157       
void verifyGradient(
real step);
00158 
00159     
virtual void oldwrite(ostream& out) 
const;
00160     
virtual void oldread(istream& in);
00161       
00162       
virtual ~Optimizer();
00163 
00164   
protected:
00165     
static void declareOptions(
OptionList& ol);
00166 
00167   
public:
00168 
00169     
00170 
00173     
void computeRepartition(
00174         
Vec v, 
int n, 
real mini, 
real maxi, 
00175         
Vec res, 
int& noutliers);
00176 
00178     
real collectGradientStats(
Vec gradient);
00179 
00182     
static void computeGradient(
00183         
Optimizer* opt,
00184         
const Vec& gradient);
00185       
00188     
static void computeOppositeGradient(
00189         
Optimizer* opt,
00190         
const Vec& gradient);
00191 
00192   };
00193 
00194 
DECLARE_OBJECT_PTR(Optimizer);
00195 
00196 } 
00197 
00198 
#endif
00199