PLearn 0.1
|
00001 // -*- C++ -*- 00002 00003 // PLearn (A C++ Machine Learning Library) 00004 // Copyright (C) 2003,2006 Olivier Delalleau 00005 // 00006 00007 // Redistribution and use in source and binary forms, with or without 00008 // modification, are permitted provided that the following conditions are met: 00009 // 00010 // 1. Redistributions of source code must retain the above copyright 00011 // notice, this list of conditions and the following disclaimer. 00012 // 00013 // 2. Redistributions in binary form must reproduce the above copyright 00014 // notice, this list of conditions and the following disclaimer in the 00015 // documentation and/or other materials provided with the distribution. 00016 // 00017 // 3. The name of the authors may not be used to endorse or promote 00018 // products derived from this software without specific prior written 00019 // permission. 00020 // 00021 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 00022 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00023 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 00024 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00025 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00026 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00027 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00028 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00029 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00030 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00031 // 00032 // This file is part of the PLearn library. For more information on the PLearn 00033 // library, go to the PLearn Web site at www.plearn.org 00034 00035 00036 /* ******************************************************* 00037 * $Id: ConjGradientOptimizer.h 6644 2007-02-09 23:09:22Z plearner $ 00038 * This file is part of the PLearn library. 00039 ******************************************************* */ 00040 00041 00042 #ifndef CONJGRADIENTOPTIMIZER_INC 00043 #define CONJGRADIENTOPTIMIZER_INC 00044 00045 #include "Optimizer.h" 00046 00047 namespace PLearn { 00048 using namespace std; 00049 00050 00051 class ConjGradientOptimizer : public Optimizer { 00052 00053 typedef Optimizer inherited; 00054 00055 public: 00056 00057 // Public options. 00058 00059 real constrain_limit; 00060 real expected_red; 00061 real max_extrapolate; 00062 real rho; 00063 real sigma; 00064 real slope_ratio; 00065 int max_eval_per_line_search; 00066 bool no_negative_gamma; 00067 int verbosity; 00068 00069 int minibatch_n_samples; 00070 int minibatch_n_line_searches; 00071 00072 protected: 00073 00074 int minibatch_curpos; // current starting sample position in dataset for minibatch 00075 00077 real bracket_limit; 00078 00080 real cubic_a, cubic_b; 00081 00083 real current_cost; 00084 00086 real fun_deriv2, fun_deriv1; 00087 00089 real fun_val1, fun_val2; 00090 00092 real step1, step2; 00093 00096 int fun_eval_count; 00097 00099 bool line_search_failed, line_search_succeeded; 00100 00101 Vec current_opp_gradient; 00102 Vec search_direction; 00103 Vec tmp_storage; 00104 Vec delta; 00105 00106 public: 00107 00108 ConjGradientOptimizer(); 00109 00110 PLEARN_DECLARE_OBJECT(ConjGradientOptimizer); 00111 00112 virtual void makeDeepCopyFromShallowCopy(CopiesMap& copies); 00113 00114 virtual void build() 00115 { 00116 inherited::build(); 00117 build_(); 00118 } 00119 00120 private: 00121 00122 void build_(); 00123 00124 public: 00125 00126 virtual bool optimizeN(VecStatsCollector& stat_coll); 00127 00128 virtual void reset(); 00129 00130 protected: 00131 00132 static void declareOptions(OptionList& ol); 00133 00135 void findDirection(); 00136 00139 bool lineSearch(); 00140 00146 void updateSearchDirection(real gamma); 00147 00155 real polakRibiere(); 00156 00164 real minimizeLineSearch(); 00165 00166 protected: 00167 00172 real computeCostValue(real alpha); 00173 00179 real computeDerivative(real alpha); 00180 00181 // Same as the two functions above combined. The result is returned through 00182 // the 'cost' and 'derivative' parameters. 00183 void computeCostAndDerivative(real alpha, real& cost, real& derivative); 00184 }; 00185 00186 } // end of namespace PLearn 00187 00188 #endif 00189 00190 00191 /* 00192 Local Variables: 00193 mode:c++ 00194 c-basic-offset:4 00195 c-file-style:"stroustrup" 00196 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00197 indent-tabs-mode:nil 00198 fill-column:79 00199 End: 00200 */ 00201 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :