PLearn 0.1
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 and Yoshua Bengio
00006 // Copyright (C) 2000, 2006 University of Montreal
00007 //
00008 
00009 // Redistribution and use in source and binary forms, with or without
00010 // modification, are permitted provided that the following conditions are met:
00011 // 
00012 //  1. Redistributions of source code must retain the above copyright
00013 //     notice, this list of conditions and the following disclaimer.
00014 // 
00015 //  2. Redistributions in binary form must reproduce the above copyright
00016 //     notice, this list of conditions and the following disclaimer in the
00017 //     documentation and/or other materials provided with the distribution.
00018 // 
00019 //  3. The name of the authors may not be used to endorse or promote
00020 //     products derived from this software without specific prior written
00021 //     permission.
00022 // 
00023 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
00024 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00025 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
00026 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00027 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00028 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00029 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00030 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00031 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00032 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00033 // 
00034 // This file is part of the PLearn library. For more information on the PLearn
00035 // library, go to the PLearn Web site at www.plearn.org
00036  
00037 
00038 /* *******************************************************      
00039  * $Id: Optimizer.h 8862 2008-04-22 14:06:32Z tihocan $
00040  * This file is part of the PLearn library.
00041  ******************************************************* */
00042 
00043 #ifndef OPTIMIZER_INC
00044 #define OPTIMIZER_INC
00045 
00046 #include <plearn/base/Object.h>
00047 #include <plearn/var/Func.h>
00048 #include <plearn/math/Mat.h>
00049 #include <plearn/measure/Measurer.h>
00050 #include <plearn/math/VecStatsCollector.h>
00051 #include <plearn/vmat/VMat.h>
00052 
00053 namespace PLearn {
00054 using namespace std;
00055 
00056 
00057 #define ALL_SAMPLES (-1)
00058 #define DEFAULT_SAMPLES (-2)
00059 
00060 class Optimizer : public Object
00061 {
00062     typedef Object inherited;
00063       
00064 public:
00065 
00066     VarArray params;
00067     Var cost;
00069     // This lets the optimizer avoid the growth of rows_to_update for
00070     // those vars, when bprop is called but not updateAndClear()
00071     // (for instance when using the stochastic_hack of GradientOptimizer).
00072     VarArray partial_update_vars;
00073     VarArray proppath; //forward and/or backward
00074 
00076     // TODO Might just be better to move it into subclasses?
00077     bool early_stop;
00078     int nstages; 
00079     int stage;   
00080 
00082     VarArray other_costs;
00084     TVec<VarArray> other_params;
00086     TVec<VarArray> other_proppaths;
00088     real other_weight;
00089 
00090 public:
00091 
00093     Optimizer();
00094       
00095     virtual void build();
00096 
00097 private:
00098 
00099     void build_();
00100 
00101 public:
00102 
00103     virtual void reset(); 
00104 
00105     virtual void setToOptimize(const VarArray& the_params, Var the_cost, VarArray the_other_costs = VarArray(0), TVec<VarArray> the_other_params = TVec<VarArray>(0), real the_other_weight = 1);
00106 
00108     void remote_setToOptimize(const VarArray& params, Var cost);
00109 
00110     /*
00111     virtual void setVarArrayOption(const string& optionname,
00112                                    const VarArray& value);
00113     virtual void setVarOption(const string& optionname, Var value);
00114     virtual void setVMatOption(const string& optionname, VMat value);
00115     */
00116       
00117     PLEARN_DECLARE_ABSTRACT_OBJECT(Optimizer);
00118 
00119     virtual void makeDeepCopyFromShallowCopy(CopiesMap& copies);
00120       
00123     virtual bool optimizeN(VecStatsCollector& stats_coll) = 0;
00124     /* while (stage < stage_init + nstages) {
00125      *   params.update(..)
00126      *   stats_coll.update(cost)
00127      *   stage++
00128      *   if finished return is_finished
00129      * }
00130      * return false
00131      */
00132 
00133     bool remote_optimizeN(PP<VecStatsCollector> stats_coll) {
00134         PLASSERT( stats_coll.isNotNull() );
00135         return optimizeN(*stats_coll);
00136     }
00137       
00140     void verifyGradient(real minval, real maxval, real step);
00141       
00144     void verifyGradient(real step);
00145 
00146     virtual void setPartialUpdateVars(const VarArray& the_partial_update_vars)
00147     {
00148         partial_update_vars = the_partial_update_vars;
00149     }
00150       
00151 protected:
00152 
00153     static void declareOptions(OptionList& ol);
00154 
00156     static void declareMethods(RemoteMethodMap& rmm);
00157 
00158 public:
00159 
00160     //--------------------------- UTILITY FUNCTIONS ----------------------------
00161 
00164     void computeRepartition(
00165         Vec v, int n, real mini, real maxi, 
00166         Vec res, int& noutliers);
00167 
00169     real collectGradientStats(const Vec& gradient);
00170 
00173     void computeGradient(const Vec& gradient);
00174 
00177     void computeOppositeGradient(const Vec& gradient);
00178 
00179 };
00180 
00181 DECLARE_OBJECT_PTR(Optimizer);
00182 
00183 } // end of namespace PLearn
00184 
00185 #endif
00186 
00187 
00188 /*
00189   Local Variables:
00190   mode:c++
00191   c-basic-offset:4
00192   c-file-style:"stroustrup"
00193   c-file-offsets:((innamespace . 0)(inline-open . 0))
00194   indent-tabs-mode:nil
00195   fill-column:79
00196   End:
00197 */
00198 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines