00001 // -*- C++ -*- 00002 00003 // StackedLearner.h 00004 // 00005 // Copyright (C) 2003 Yoshua Bengio 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: StackedLearner.h,v 1.11 2004/07/21 16:30:56 chrish42 Exp $ 00037 ******************************************************* */ 00038 00039 // Authors: Yoshua Bengio 00040 00043 #ifndef StackedLearner_INC 00044 #define StackedLearner_INC 00045 00046 #include "PLearner.h" 00047 #include <plearn/vmat/Splitter.h> 00048 00049 namespace PLearn { 00050 using namespace std; 00051 00052 class StackedLearner: public PLearner 00053 { 00054 protected: 00055 // ********************* 00056 // * protected options * 00057 // ********************* 00058 00059 // ### declare protected option fields (such as learnt parameters) here 00060 // ... 00061 00062 // NON-OPTIONs 00063 Mat base_learners_outputs; 00064 00065 public: 00066 00067 typedef PLearner inherited; 00068 00069 00070 // ************************ 00071 // * public build options * 00072 // ************************ 00073 00074 // ### declare public option fields (such as build options) here 00075 00078 TVec<PP<PLearner> > base_learners; 00079 00083 PP<PLearner> combiner; 00084 00087 PP<Splitter> splitter; 00088 00092 PP<Splitter> base_train_splitter; 00093 00096 bool train_base_learners; 00097 00100 bool normalize_base_learners_output; 00101 00104 bool precompute_base_learners_output; 00105 00107 bool put_raw_input; 00108 00111 bool put_costs; 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 StackedLearner(); 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 00142 void setTrainStatsCollector(PP<VecStatsCollector> statscol); 00143 00145 virtual void build(); 00146 00148 virtual void makeDeepCopyFromShallowCopy(map<const void*, void*>& copies); 00149 00150 // Declares other standard object methods 00151 // If your class is not instantiatable (it has pure virtual methods) 00152 // you should replace this by PLEARN_DECLARE_ABSTRACT_OBJECT_METHODS 00153 PLEARN_DECLARE_OBJECT(StackedLearner); 00154 00155 00156 // ************************** 00157 // **** PLearner methods **** 00158 // ************************** 00159 00162 // (PLEASE IMPLEMENT IN .cc) 00163 virtual int outputsize() const; 00164 00167 // (PLEASE IMPLEMENT IN .cc) 00168 virtual void forget(); 00169 00170 00173 // (PLEASE IMPLEMENT IN .cc) 00174 virtual void train(); 00175 00176 00178 // (PLEASE IMPLEMENT IN .cc) 00179 virtual void computeOutput(const Vec& input, Vec& output) const; 00180 00182 // (PLEASE IMPLEMENT IN .cc) 00183 virtual void computeCostsFromOutputs(const Vec& input, const Vec& output, 00184 const Vec& target, Vec& costs) const; 00185 00186 00188 // (PLEASE IMPLEMENT IN .cc) 00189 virtual TVec<string> getTestCostNames() const; 00190 00193 // (PLEASE IMPLEMENT IN .cc) 00194 virtual TVec<string> getTrainCostNames() const; 00195 00196 00199 virtual void setTrainingSet(VMat training_set, bool call_forget=true); 00200 00201 // *** SUBCLASS WRITING: *** 00202 // While in general not necessary, in case of particular needs 00203 // (efficiency concerns for ex) you may also want to overload 00204 // some of the following methods: 00205 // virtual void computeOutputAndCosts(const Vec& input, const Vec& target, Vec& output, Vec& costs) const; 00206 // virtual void computeCostsOnly(const Vec& input, const Vec& target, Vec& costs) const; 00207 // virtual void test(VMat testset, PP<VecStatsCollector> test_stats, VMat testoutputs=0, VMat testcosts=0) const; 00208 // virtual int nTestCosts() const; 00209 // virtual int nTrainCosts() const; 00210 00211 }; 00212 00213 // Declares a few other classes and functions related to this class 00214 DECLARE_OBJECT_PTR(StackedLearner); 00215 00216 } // end of namespace PLearn 00217 00218 #endif