PLearn 0.1
|
00001 // -*- C++ -*- 00002 00003 // AdaBoost.h 00004 // 00005 // Copyright (C) 2003 Pascal Vincent 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 * $Id: AdaBoost.h 10097 2009-04-07 15:58:04Z nouiz $ 00037 ******************************************************* */ 00038 00039 // Authors: Yoshua Bengio 00040 00043 #ifndef AdaBoost_INC 00044 #define AdaBoost_INC 00045 00046 #include <plearn_learners/generic/PLearner.h> 00047 00048 namespace PLearn { 00049 using namespace std; 00050 00051 class AdaBoost: public PLearner 00052 { 00053 typedef PLearner inherited; 00054 00056 mutable Vec weighted_costs; 00057 mutable Vec sum_weighted_costs; 00058 mutable Vec weak_learner_output; 00059 00061 mutable TVec<VMat> saved_testset; 00062 mutable TVec<VMat> saved_testoutputs; 00063 mutable TVec<int> saved_last_test_stages; 00064 00065 protected: 00066 // average weighted error of each learner 00067 Vec learners_error; 00068 // weighing scheme over examples 00069 Vec example_weights; 00070 00071 // ********************* 00072 // * protected options * 00073 // ********************* 00074 00075 // saved options: 00076 // (unnormalized) weight associated to each weak learner 00077 Vec voting_weights; 00078 real sum_voting_weights; // = sum(voting_weights); 00079 real initial_sum_weights; 00080 00082 TVec< PP<PLearner> > weak_learners; 00083 00085 bool found_zero_error_weak_learner; 00086 00087 public: 00088 00089 // ************************ 00090 // * public build options * 00091 // ************************ 00092 00097 PP<PLearner> weak_learner_template; 00098 00099 // normally 0.5 00100 real target_error; 00101 00102 // whether to give an expdir to the underlying weak learners 00103 bool provide_learner_expdir; 00104 00105 // threshold on output of weak learner to decide if class 0 or class 1 00106 real output_threshold; 00107 00108 // whether to compute training error during training 00109 bool compute_training_error; 00110 00111 // use Pseudo-loss Adaboost 00112 bool pseudo_loss_adaboost; 00113 00114 // use Confidence-rated adaboost 00115 bool conf_rated_adaboost; 00116 00117 // use resampling (vs weighting) to train the underlying classifier 00118 bool weight_by_resampling; 00119 00120 // stop if weak learner does not seem to help 00121 bool early_stopping; 00122 00123 // save model after each stage into <expdir>/model.psave 00124 bool save_often; 00125 00126 // Did we add the sub_learner_costs to our costs 00127 bool forward_sub_learner_test_costs; 00128 00129 // Did we modif directly the train_set weights? 00130 bool modif_train_set_weights; 00131 00132 // Did we save and reuse previous test result? 00133 // This is usefull to have a test time that is 00134 // independent of the number of adaboost itaration 00135 bool reuse_test_results; 00136 // **************** 00137 // * Constructors * 00138 // **************** 00139 00140 // Default constructor, make sure the implementation in the .cc 00141 // initializes all fields to reasonable default values. 00142 AdaBoost(); 00143 00144 00145 // ****************** 00146 // * PLearner methods * 00147 // ****************** 00148 00149 private: 00151 void build_(); 00152 00153 void computeTrainingError(Vec input, Vec target); 00154 00155 void computeOutput_(const Vec& input, Vec& output, 00156 const int start=0, const real sum=0.) const; 00157 00158 protected: 00160 // (Please implement in .cc) 00161 static void declareOptions(OptionList& ol); 00162 00163 public: 00164 00165 // ************************ 00166 // **** Object methods **** 00167 // ************************ 00168 00170 virtual void build(); 00171 00173 virtual void makeDeepCopyFromShallowCopy(CopiesMap& copies); 00174 00175 // Declares other standard object methods 00176 // If your class is not instantiatable (it has pure virtual methods) 00177 // you should replace this by PLEARN_DECLARE_ABSTRACT_OBJECT_METHODS 00178 PLEARN_DECLARE_OBJECT(AdaBoost); 00179 00180 00181 // ************************** 00182 // **** PLearner methods **** 00183 // ************************** 00184 00189 virtual int outputsize() const; 00190 00191 virtual void finalize(); 00192 00195 virtual void forget(); 00196 00199 virtual void train(); 00200 virtual void test(VMat testset, PP<VecStatsCollector> test_stats, 00201 VMat testoutputs, VMat testcosts) const; 00202 00204 virtual void computeOutput(const Vec& input, Vec& output) const{ 00205 computeOutput_(input,output,0,0);} 00206 virtual void computeOutputAndCosts(const Vec& input, const Vec& target, 00207 Vec& output, Vec& costs) const; 00208 00210 virtual void computeCostsFromOutputs(const Vec& input, const Vec& output, 00211 const Vec& target, Vec& costs) const; 00212 00213 00215 virtual TVec<string> getTestCostNames() const; 00216 00219 virtual TVec<string> getTrainCostNames() const; 00220 virtual void setTrainingSet(VMat training_set, bool call_forget=true); 00221 00222 00223 }; 00224 00225 // Declares a few other classes and functions related to this class 00226 DECLARE_OBJECT_PTR(AdaBoost); 00227 00228 } // end of namespace PLearn 00229 00230 #endif 00231 00232 00233 /* 00234 Local Variables: 00235 mode:c++ 00236 c-basic-offset:4 00237 c-file-style:"stroustrup" 00238 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00239 indent-tabs-mode:nil 00240 fill-column:79 00241 End: 00242 */ 00243 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :