PLearn 0.1
|
00001 // -*- C++ -*- 00002 00003 // LocalGaussianClassifier.h 00004 // 00005 // Copyright (C) 2007 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 // Authors: Pascal Vincent 00036 00040 #ifndef LocalGaussianClassifier_INC 00041 #define LocalGaussianClassifier_INC 00042 00043 #include <plearn_learners/generic/PLearner.h> 00044 00045 // From C++ stdlib 00046 #include <utility> 00047 #include <algorithm> 00048 00049 namespace PLearn { 00050 00061 class LocalGaussianClassifier : public PLearner 00062 { 00063 typedef PLearner inherited; 00064 00065 00066 protected: 00067 00068 // Vec emptyvec; 00069 // mutable Vec NN_outputs; 00070 // mutable Vec NN_costs; 00071 00072 // ********************* 00073 // * protected options * 00074 // ********************* 00075 00076 // PP<GenericNearestNeighbors> NN; 00077 00078 00079 public: 00080 //##### Public Build Options ############################################ 00081 00082 int nclasses; 00083 real computation_neighbors; 00084 real kernel_sigma; 00085 real regularization_sigma; 00086 real ignore_weights_below; 00087 00088 private: 00089 real minus_one_half_over_kernel_sigma_square; 00090 00092 Vec trainsample; 00093 Vec traininput; 00094 real* traintarget_ptr; 00095 real* trainweight_ptr; 00096 mutable TVec< pair<real,int> > pqvec; // priority queue (heap) vector 00097 00098 Vec log_counts; // the log_counts considering all points 00099 Vec log_counts2; // the log_counts considering only the points kept for computing the covariance 00100 Mat means; 00101 Mat allcovars; 00102 TVec<Mat> covars; 00103 00104 00105 public: 00106 //##### Public Member Functions ######################################### 00107 00109 // ### Make sure the implementation in the .cc 00110 // ### initializes all fields to reasonable default values. 00111 LocalGaussianClassifier(); 00112 00113 00114 //##### PLearner Member Functions ####################################### 00115 00118 // (PLEASE IMPLEMENT IN .cc) 00119 virtual int outputsize() const; 00120 00124 // (PLEASE IMPLEMENT IN .cc) 00125 virtual void forget(); 00126 00127 virtual void setTrainingSet(VMat training_set, bool call_forget=true); 00128 00132 // (PLEASE IMPLEMENT IN .cc) 00133 virtual void train(); 00134 00136 // (PLEASE IMPLEMENT IN .cc) 00137 virtual void computeOutput(const Vec& input, Vec& output) const; 00138 00140 // (PLEASE IMPLEMENT IN .cc) 00141 virtual void computeCostsFromOutputs(const Vec& input, const Vec& output, 00142 const Vec& target, Vec& costs) const; 00143 00146 // (PLEASE IMPLEMENT IN .cc) 00147 virtual TVec<std::string> getTestCostNames() const; 00148 00149 real computeLogWeight(const Vec& input, const Vec& traininput) const; 00150 00153 // (PLEASE IMPLEMENT IN .cc) 00154 virtual TVec<std::string> getTrainCostNames() const; 00155 00156 00157 // *** SUBCLASS WRITING: *** 00158 // While in general not necessary, in case of particular needs 00159 // (efficiency concerns for ex) you may also want to overload 00160 // some of the following methods: 00161 // virtual void computeOutputAndCosts(const Vec& input, const Vec& target, 00162 // Vec& output, Vec& costs) const; 00163 // virtual void computeCostsOnly(const Vec& input, const Vec& target, 00164 // Vec& costs) const; 00165 // virtual void test(VMat testset, PP<VecStatsCollector> test_stats, 00166 // VMat testoutputs=0, VMat testcosts=0) const; 00167 // virtual int nTestCosts() const; 00168 // virtual int nTrainCosts() const; 00169 // virtual void resetInternalState(); 00170 // virtual bool isStatefulLearner() const; 00171 00172 00173 //##### PLearn::Object Protocol ######################################### 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(LocalGaussianClassifier); 00179 00180 // Simply calls inherited::build() then build_() 00181 virtual void build(); 00182 00184 // (PLEASE IMPLEMENT IN .cc) 00185 virtual void makeDeepCopyFromShallowCopy(CopiesMap& copies); 00186 00187 protected: 00188 //##### Protected Options ############################################### 00189 00190 // ### Declare protected option fields (such as learned parameters) here 00191 // ... 00192 00193 protected: 00194 //##### Protected Member Functions ###################################### 00195 00197 // (PLEASE IMPLEMENT IN .cc) 00198 static void declareOptions(OptionList& ol); 00199 00200 private: 00201 //##### Private Member Functions ######################################## 00202 00204 // (PLEASE IMPLEMENT IN .cc) 00205 void build_(); 00206 00207 private: 00208 //##### Private Data Members ############################################ 00209 00210 // The rest of the private stuff goes here 00211 }; 00212 00213 // Declares a few other classes and functions related to this class 00214 DECLARE_OBJECT_PTR(LocalGaussianClassifier); 00215 00216 } // end of namespace PLearn 00217 00218 #endif 00219 00220 00221 /* 00222 Local Variables: 00223 mode:c++ 00224 c-basic-offset:4 00225 c-file-style:"stroustrup" 00226 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00227 indent-tabs-mode:nil 00228 fill-column:79 00229 End: 00230 */ 00231 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :