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,v 1.4 2004/07/21 16:30:55 chrish42 Exp $ 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 public: 00054 typedef PLearner inherited; 00055 00056 protected: 00057 // local variables: 00058 00059 // average weighted error of each learner 00060 Vec learners_error; 00061 // weighing scheme over examples 00062 Vec example_weights; 00063 00064 // ********************* 00065 // * protected options * 00066 // ********************* 00067 00068 // saved options: 00069 // (unnormalized) weight associated to each weak learner 00070 Vec voting_weights; 00071 real sum_voting_weights; // = sum(voting_weights); 00072 real initial_sum_weights; 00073 00075 TVec< PP<PLearner> > weak_learners; 00076 00077 public: 00078 00079 // ************************ 00080 // * public build options * 00081 // ************************ 00082 00087 PP<PLearner> weak_learner_template; 00088 00089 // normally 0.5 00090 real target_error; 00091 00092 // whether to give an expdir to the underlying weak learners 00093 bool provide_learner_expdir; 00094 00095 // threshold on output of weak learner to decide if class 0 or class 1 00096 real output_threshold; 00097 00098 // whether to compute training error during training 00099 bool compute_training_error; 00100 00101 // use more refined training criterion when weak classifier is soft 00102 bool pseudo_loss_adaboost; 00103 00104 // use resampling (vs weighting) to train the underlying classifier 00105 bool weight_by_resampling; 00106 00107 // stop if weak learner does not seem to help 00108 bool early_stopping; 00109 00110 // save model after each stage into <expdir>/model.psave 00111 bool save_often; 00112 00113 // **************** 00114 // * Constructors * 00115 // **************** 00116 00117 // Default constructor, make sure the implementation in the .cc 00118 // initializes all fields to reasonable default values. 00119 AdaBoost(); 00120 00121 00122 // ****************** 00123 // * PLearner methods * 00124 // ****************** 00125 00126 private: 00128 // (Please implement in .cc) 00129 void build_(); 00130 00131 protected: 00133 // (Please implement in .cc) 00134 static void declareOptions(OptionList& ol); 00135 00136 public: 00137 00138 // ************************ 00139 // **** Object methods **** 00140 // ************************ 00141 00143 virtual void build(); 00144 00146 virtual void makeDeepCopyFromShallowCopy(map<const void*, void*>& copies); 00147 00148 // Declares other standard object methods 00149 // If your class is not instantiatable (it has pure virtual methods) 00150 // you should replace this by PLEARN_DECLARE_ABSTRACT_OBJECT_METHODS 00151 PLEARN_DECLARE_OBJECT(AdaBoost); 00152 00153 00154 // ************************** 00155 // **** PLearner methods **** 00156 // ************************** 00157 00162 virtual int outputsize() const; 00163 00166 virtual void forget(); 00167 00168 00171 virtual void train(); 00172 00173 00175 virtual void computeOutput(const Vec& input, Vec& output) const; 00176 00178 virtual void computeCostsFromOutputs(const Vec& input, const Vec& output, 00179 const Vec& target, Vec& costs) const; 00180 00181 00183 virtual TVec<string> getTestCostNames() const; 00184 00187 virtual TVec<string> getTrainCostNames() const; 00188 00189 }; 00190 00191 // Declares a few other classes and functions related to this class 00192 DECLARE_OBJECT_PTR(AdaBoost); 00193 00194 } // end of namespace PLearn 00195 00196 #endif