PLearn 0.1
MultiClassAdaBoost.h
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // plearn_learners/meta/MultiClassAdaBoost.h
00004 //
00005 // Copyright (C) 2007 Frederic Bastien
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 // Authors: Frederic Bastien
00036 
00040 #ifndef MultiClassAdaBoost_INC
00041 #define MultiClassAdaBoost_INC
00042 
00043 #include <plearn_learners/generic/PLearner.h>
00044 #include <plearn_learners/meta/AdaBoost.h>
00045 #include <plearn/misc/PTimer.h>
00046 
00047 namespace PLearn {
00048 
00059 class MultiClassAdaBoost : public PLearner
00060 {
00061     typedef PLearner inherited;
00062 
00063     mutable Vec tmp_input;
00064     mutable Vec tmp_target;
00065     mutable Vec tmp_output;
00066     mutable Vec tmp_costs;
00067 
00068     mutable Vec output1;
00069     mutable Vec output2;
00070     mutable Vec subcosts1;
00071     mutable Vec subcosts2;
00072 
00073     mutable TVec<VMat> saved_testset;
00074     mutable TVec<VMat> saved_testset1;
00075     mutable TVec<VMat> saved_testset2;
00076 
00078     real train_time;
00080     real total_train_time;
00081 
00083     real test_time;
00085     real total_test_time;
00086 
00087     bool time_costs;
00088     bool warn_once_target_gt_2;
00089     mutable bool done_warn_once_target_gt_2;
00090 
00091     PP<PTimer> timer;
00092 
00093     //Variable used when forward_test==2.
00094     //They are used to determine if inherited::test is faster then forwarding the test fct.
00095     mutable real time_sum;
00096     mutable real time_sum_ft;
00097     mutable real time_last_stage;
00098     mutable real time_last_stage_ft;
00099     mutable int last_stage;
00100     mutable int nb_sequential_ft;
00101 
00102 public:
00103     //#####  Public Build Options  ############################################
00104 
00107 
00109     bool forward_sub_learner_test_costs;
00110 
00112     uint16_t forward_test;
00113 
00115     PP<AdaBoost> learner1;
00116     PP<AdaBoost> learner2;
00117     PP<AdaBoost> learner_template;
00118 
00119 public:
00120     //#####  Public Member Functions  #########################################
00121 
00123     // ### Make sure the implementation in the .cc
00124     // ### initializes all fields to reasonable default values.
00125     MultiClassAdaBoost();
00126 
00127 
00128     //#####  PLearner Member Functions  #######################################
00129 
00132     virtual int outputsize() const;
00133 
00134     virtual void finalize();
00135 
00139     virtual void forget();
00140 
00144     virtual void train();
00145 
00147     virtual void computeOutput(const Vec& input, Vec& output) const;
00148 
00150     virtual void computeCostsFromOutputs(const Vec& input, const Vec& output,
00151                                          const Vec& target, Vec& costs) const;
00152 
00153     void computeCostsFromOutputs_(const Vec& input, const Vec& output,
00154                                   const Vec& target, Vec& sub_costs1,
00155                                   Vec& sub_costs2, Vec& costs) const;
00156     virtual TVec<string> getOutputNames() const;
00157 
00160     virtual TVec<std::string> getTestCostNames() const;
00161 
00164     virtual TVec<std::string> getTrainCostNames() const;
00165 
00166 
00167     // *** SUBCLASS WRITING: ***
00168     virtual void computeOutputAndCosts(const Vec& input, const Vec& target,
00169                                        Vec& output, Vec& costs) const;
00170     // virtual void computeCostsOnly(const Vec& input, const Vec& target,
00171     //                               Vec& costs) const;
00172     virtual void test(VMat testset, PP<VecStatsCollector> test_stats,
00173                       VMat testoutputs=0, VMat testcosts=0) const;
00174     // virtual int nTestCosts() const;
00175     // virtual int nTrainCosts() const;
00176     // virtual void resetInternalState();
00177     // virtual bool isStatefulLearner() const;
00178 
00179 
00180     //#####  PLearn::Object Protocol  #########################################
00181 
00182     // Declares other standard object methods.
00183     PLEARN_DECLARE_OBJECT(MultiClassAdaBoost);
00184 
00185     // Simply calls inherited::build() then build_()
00186     virtual void build();
00187 
00189     virtual void makeDeepCopyFromShallowCopy(CopiesMap& copies);
00190 
00191 
00192     virtual void setTrainingSet(VMat training_set, bool call_forget=true);
00193 
00194 protected:
00195     //#####  Protected Options  ###############################################
00196 
00197     // ### Declare protected option fields (such as learned parameters) here
00198 
00199 protected:
00200     //#####  Protected Member Functions  ######################################
00201 
00203     static void declareOptions(OptionList& ol);
00204 
00205 private:
00206     //#####  Private Member Functions  ########################################
00207 
00209     void build_();
00210 
00211     void getSubLearnerTarget(const Vec target, TVec<Vec> sub_target)const;
00212 private:
00213     //#####  Private Data Members  ############################################
00214     mutable TVec<Vec> sub_target_tmp;
00215 
00216     string targetname;
00217 };
00218 
00219 // Declares a few other classes and functions related to this class
00220 DECLARE_OBJECT_PTR(MultiClassAdaBoost);
00221 
00222 } // end of namespace PLearn
00223 
00224 #endif
00225 
00226 
00227 /*
00228   Local Variables:
00229   mode:c++
00230   c-basic-offset:4
00231   c-file-style:"stroustrup"
00232   c-file-offsets:((innamespace . 0)(inline-open . 0))
00233   indent-tabs-mode:nil
00234   fill-column:79
00235   End:
00236 */
00237 // 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