PLearn 0.1
|
00001 // -*- C++ -*- 00002 00003 // 00004 // Copyright (C) 2004-2005 University of Montreal 00005 // 00006 // Redistribution and use in source and binary forms, with or without 00007 // modification, are permitted provided that the following conditions are met: 00008 // 00009 // 1. Redistributions of source code must retain the above copyright 00010 // notice, this list of conditions and the following disclaimer. 00011 // 00012 // 2. Redistributions in binary form must reproduce the above copyright 00013 // notice, this list of conditions and the following disclaimer in the 00014 // documentation and/or other materials provided with the distribution. 00015 // 00016 // 3. The name of the authors may not be used to endorse or promote 00017 // products derived from this software without specific prior written 00018 // permission. 00019 // 00020 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 00021 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00022 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 00023 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00024 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00025 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00026 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00027 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00028 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00029 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00030 // 00031 // This file is part of the PLearn library. For more information on the PLearn 00032 // library, go to the PLearn Web site at www.plearn.org 00033 00034 00036 #include "ParzenWindow.h" 00037 00038 //#include <plearn/math/plapack.h> 00039 //#include <plearn/base/general.h> 00040 //#include <plearn/math/TMat.h> 00041 //#include <plearn/math/TMat_maths.h> 00042 //#include <plearn/math/BottomNI.h> 00043 00044 namespace PLearn { 00045 00046 PLEARN_IMPLEMENT_OBJECT(ParzenWindow, 00047 "Parzen Window density estimate.", 00048 "Standard Parzen Window algorithm. The user only needs\n" 00049 "to set the isotropic_sigma parameter" 00050 ); 00051 00052 // TODO Allow the user to specify the kernel. 00053 00055 // ParzenWindow // 00057 ParzenWindow::ParzenWindow() 00058 : isotropic_sigma(1) 00059 { 00060 nstages = 1; 00061 } 00062 00063 ParzenWindow::ParzenWindow(real the_isotropic_sigma) 00064 : isotropic_sigma(the_isotropic_sigma) 00065 { 00066 } 00067 00068 // ### Nothing to add here, simply calls build_ 00069 void ParzenWindow::build() 00070 { 00071 inherited::build(); 00072 build_(); 00073 } 00074 00075 // TODO Hide the options from GaussMix that are overwritten. 00076 // TODO Yeah that's really needed! 00077 00079 // declareOptions // 00081 void ParzenWindow::declareOptions(OptionList& ol) 00082 { 00083 declareOption(ol,"isotropic_sigma", &ParzenWindow::isotropic_sigma, OptionBase::buildoption, 00084 "Spherical standard deviation parameter (NOTE: old implementation called this sigma_square, but it really was sigma, hence the renaming)"); 00085 00086 // Now call the parent class' declareOptions 00087 inherited::declareOptions(ol); 00088 } 00089 00090 void ParzenWindow::build_() 00091 {} 00092 00093 void ParzenWindow::makeDeepCopyFromShallowCopy(CopiesMap& copies) 00094 { 00095 inherited::makeDeepCopyFromShallowCopy(copies); 00096 00097 // ### Call deepCopyField on all "pointer-like" fields 00098 // ### that you wish to be deepCopied rather than 00099 // ### shallow-copied. 00100 // ### ex: 00101 // deepCopyField(trainvec, copies); 00102 00103 // ### Remove this line when you have fully implemented this method. 00104 //PLERROR("ParzenWindow::makeDeepCopyFromShallowCopy not fully (correctly) implemented yet!"); 00105 } 00106 00107 void ParzenWindow::train() 00108 { 00109 if(stage<1) 00110 { 00111 int l = train_set.length(); 00112 type = "spherical"; 00113 L = l; 00114 D = -1; 00115 GaussMix::build(); // rebuild because options chnged 00116 resizeDataBeforeTraining(); // TODO See exactly what this does. 00117 00118 // new code proprly taking sample weights into account 00119 bool has_weights = train_set->hasWeights(); 00120 real default_weight = 1.0/l; 00121 Vec target; 00122 real weight = 0; 00123 real weight_sum = 0; 00124 00125 for(int i=0; i<l; i++) 00126 { 00127 // if(i%100==0) 00128 // cerr << "[SEQUENTIAL TRAIN: processing pattern #" << i << "/" << l << "]\n"; 00129 Vec input = center(i); 00130 train_set->getExample(i,input,target,weight); 00131 sigma[i] = isotropic_sigma; 00132 if(has_weights) 00133 { 00134 alpha[i] = weight; 00135 weight_sum += weight; 00136 } 00137 else 00138 alpha[i] = default_weight; 00139 // resizeStuffBeforeTraining(); TODO Put back? 00140 } 00141 if(has_weights) 00142 alpha /= weight_sum; 00143 GaussMix::build(); 00144 00145 stage = 1; 00146 // precomputeStuff(); TODO Put back? 00147 build(); // rebuild 00148 } 00149 } 00150 00151 } // end of namespace PLearn 00152 00153 00154 /* 00155 Local Variables: 00156 mode:c++ 00157 c-basic-offset:4 00158 c-file-style:"stroustrup" 00159 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00160 indent-tabs-mode:nil 00161 fill-column:79 00162 End: 00163 */ 00164 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :