Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members

Optimizer.h

Go to the documentation of this file.
00001 // -*- C++ -*- 00002 00003 // PLearn (A C++ Machine Learning Library) 00004 // Copyright (C) 1998 Pascal Vincent 00005 // Copyright (C) 1999,2000 Pascal Vincent, Yoshua Bengio and University of Montreal 00006 // 00007 00008 // Redistribution and use in source and binary forms, with or without 00009 // modification, are permitted provided that the following conditions are met: 00010 // 00011 // 1. Redistributions of source code must retain the above copyright 00012 // notice, this list of conditions and the following disclaimer. 00013 // 00014 // 2. Redistributions in binary form must reproduce the above copyright 00015 // notice, this list of conditions and the following disclaimer in the 00016 // documentation and/or other materials provided with the distribution. 00017 // 00018 // 3. The name of the authors may not be used to endorse or promote 00019 // products derived from this software without specific prior written 00020 // permission. 00021 // 00022 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 00023 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00024 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 00025 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00026 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00027 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00028 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00029 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00030 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00031 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00032 // 00033 // This file is part of the PLearn library. For more information on the PLearn 00034 // library, go to the PLearn Web site at www.plearn.org 00035 00036 00037 00038 00039 /* ******************************************************* 00040 * $Id: Optimizer.h,v 1.20 2004/07/21 16:30:54 chrish42 Exp $ 00041 * This file is part of the PLearn library. 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 //typedef void (*OptimizerCallback)(int t); 00065 00066 class Optimizer : public Object 00067 { 00068 typedef Object inherited; 00069 00070 public: 00071 VarArray params; 00072 Var cost; 00073 VarArray proppath; //forward and/or backward 00074 int nupdates; // deprecated TODO Remove ? 00075 int nstages; 00076 int stage; 00077 00078 bool early_stop; 00079 int early_stop_i;// number of epoch before early stopping 00080 00081 VarArray update_for_measure; // not used if length()==0 00082 //OptimizerCallback callback; //!< callback function 00083 00084 //oassignstream vlog; 00085 PStream vlog; 00086 // TODO Looks like this PStream is never used! 00087 00088 private: 00089 Vec temp_grad; 00090 /* Vec same_sign; //!< number of consecutive updates in same direction */ 00091 00092 protected: 00093 00094 Array<Measurer*> measurers; 00095 00098 string filename; // JS - that was const... 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(); } // DEPRECATED : use build() instead 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 /* while (stage < stage_init + nstages) { 00143 * params.update(..) 00144 * stats_coll.update(cost) 00145 * stage++ 00146 * if finished return is_finished 00147 * } 00148 * return false 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 //--------------------------- UTILITY FUNCTIONS ---------------------------- 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 } // end of namespace PLearn 00197 00198 #endif 00199

Generated on Tue Aug 17 16:00:36 2004 for PLearn by doxygen 1.3.7