Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members

LocallyWeightedDistribution.cc

Go to the documentation of this file.
00001 // -*- C++ -*- 00002 00003 // Distribution.cc 00004 // 00005 // Copyright (C) 2002 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: LocallyWeightedDistribution.cc,v 1.9 2004/07/21 20:23:38 tihocan Exp $ 00037 ******************************************************* */ 00038 00040 #include "LocallyWeightedDistribution.h" 00041 #include <plearn/vmat/ConcatColumnsVMatrix.h> 00042 00043 namespace PLearn { 00044 using namespace std; 00045 00046 LocallyWeightedDistribution::LocallyWeightedDistribution() 00047 {} 00048 00049 00050 PLEARN_IMPLEMENT_OBJECT(LocallyWeightedDistribution, "ONE LINE DESCR", "NO HELP"); 00051 00052 void LocallyWeightedDistribution::declareOptions(OptionList& ol) 00053 { 00054 declareOption(ol, "weighting_kernel", &LocallyWeightedDistribution::weighting_kernel, OptionBase::buildoption, 00055 "The kernel that will be used to locally weigh the samples"); 00056 00057 declareOption(ol, "localdistr", &LocallyWeightedDistribution::localdistr, OptionBase::buildoption, 00058 "The distribution that will be trianed with local weights"); 00059 00060 // Now call the parent class' declareOptions 00061 inherited::declareOptions(ol); 00062 } 00063 00064 void LocallyWeightedDistribution::build_() 00065 { 00066 // ### This method should do the real building of the object, 00067 // ### according to set 'options', in *any* situation. 00068 // ### Typical situations include: 00069 // ### - Initial building of an object from a few user-specified options 00070 // ### - Building of a "reloaded" object: i.e. from the complete set of all serialised options. 00071 // ### - Updating or "re-building" of an object after a few "tuning" options have been modified. 00072 // ### You should assume that the parent class' build_() has already been called. 00073 00074 if(weightsize()!=0 && weightsize()!=1) 00075 PLERROR("In LocallyWeightedDistribution::build_, weightsize must be 0 or 1"); 00076 00077 localdistr->inputsize_ = inputsize_; 00078 localdistr->weightsize_ = 1; 00079 localdistr->build(); 00080 } 00081 00082 // ### Nothing to add here, simply calls build_ 00083 void LocallyWeightedDistribution::build() 00084 { 00085 inherited::build(); 00086 build_(); 00087 } 00088 00089 00090 void LocallyWeightedDistribution::train(VMat training_set) 00091 { 00092 if(training_set.width() != inputsize()+weightsize()) 00093 PLERROR("In LocallyWeightedDistribution::train width of training set is different from inputsize()+weightsize()"); 00094 setTrainingSet(training_set); 00095 } 00096 00097 00098 void LocallyWeightedDistribution::makeDeepCopyFromShallowCopy(map<const void*, void*>& copies) 00099 { 00100 Distribution::makeDeepCopyFromShallowCopy(copies); 00101 00102 // ### Call deepCopyField on all "pointer-like" fields 00103 // ### that you wish to be deepCopied rather than 00104 // ### shallow-copied. 00105 // ### ex: 00106 // deepCopyField(trainvec, copies); 00107 00108 // ### Remove this line when you have fully implemented this method. 00109 PLERROR("LocallyWeightedDistribution::makeDeepCopyFromShallowCopy not fully (correctly) implemented yet!"); 00110 } 00111 00112 00113 double LocallyWeightedDistribution::log_density(const Vec& x) const 00114 { 00115 int l = train_set.length(); 00116 int w = inputsize(); 00117 weights.resize(l); 00118 // 'weights' will contain the "localization" weights for the current test point. 00119 trainsample.resize(w+weightsize()); 00120 Vec input = trainsample.subVec(0,w); 00121 00122 for(int i=0; i<l; i++) 00123 { 00124 train_set->getRow(i,trainsample); 00125 real weight = weighting_kernel(x,input); 00126 if(weightsize()==1) 00127 weight *= trainsample[w]; 00128 weights[i] = weight; 00129 } 00130 00131 VMat weight_column(columnmatrix(weights)); 00132 00133 VMat weighted_trainset; 00134 if(weightsize()==0) // append weight column 00135 weighted_trainset = hconcat(train_set, weight_column); 00136 else // replace last column by weight column 00137 weighted_trainset = hconcat(train_set.subMatColumns(0,inputsize()), weight_column); 00138 00139 localdistr->forget(); 00140 localdistr->train(weighted_trainset); 00141 return localdistr->log_density(x); 00142 } 00143 00144 00145 } // end of namespace PLearn

Generated on Tue Aug 17 15:57:49 2004 for PLearn by doxygen 1.3.7