PLearn 0.1
|
00001 // -*- C++ -*- 00002 00003 // UniformizeVMatrix.cc 00004 // 00005 // Copyright (C) 2006 Olivier Delalleau 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: Olivier Delalleau 00036 00040 #include "UniformizeVMatrix.h" 00041 00042 namespace PLearn { 00043 using namespace std; 00044 00045 PLEARN_IMPLEMENT_OBJECT( 00046 UniformizeVMatrix, 00047 "Transforms its source VMatrix so that its features look uniform.", 00048 "This VMat transforms the features of its source that are obviously non-\n" 00049 "Gaussian, i.e. when the difference between the maximum and minimum\n" 00050 "value is too large compared to the standard deviation (the meaning of\n" 00051 "'too large' being controlled by the 'threshold_ratio' option).\n" 00052 "\n" 00053 "It uses an underlying UniformizeLearner to perform uniformization.\n" 00054 ); 00055 00057 // UniformizeVMatrix // 00059 UniformizeVMatrix::UniformizeVMatrix(): 00060 max(1), 00061 min(0), 00062 nquantiles(1000), 00063 threshold_ratio(10), 00064 uniformize_input(true), 00065 uniformize_target(false), 00066 uniformize_weight(false), 00067 uniformize_extra(false), 00068 uniformize_learner( new UniformizeLearner() ), 00069 uniformized_source( new PLearnerOutputVMatrix() ) 00070 {} 00071 00073 // declareOptions // 00075 void UniformizeVMatrix::declareOptions(OptionList& ol) 00076 { 00077 declareOption(ol, "min", &UniformizeVMatrix::min, 00078 OptionBase::buildoption, 00079 "The lower bound of the [min,max] interval values are mapped to."); 00080 00081 declareOption(ol, "max", &UniformizeVMatrix::max, 00082 OptionBase::buildoption, 00083 "The upper bound of the [min,max] interval values are mapped to."); 00084 00085 declareOption(ol, "threshold_ratio", &UniformizeVMatrix::threshold_ratio, 00086 OptionBase::buildoption, 00087 "A source's feature will be uniformized when the following holds:\n" 00088 "(max - min) / stddev > threshold_ratio."); 00089 00090 declareOption(ol, "uniformize_input", 00091 &UniformizeVMatrix::uniformize_input, 00092 OptionBase::buildoption, 00093 "Whether or not to uniformize the input part."); 00094 00095 declareOption(ol, "uniformize_target", 00096 &UniformizeVMatrix::uniformize_target, 00097 OptionBase::buildoption, 00098 "Whether or not to uniformize the target part."); 00099 00100 declareOption(ol, "uniformize_weight", 00101 &UniformizeVMatrix::uniformize_weight, 00102 OptionBase::buildoption, 00103 "Whether or not to uniformize the weight part."); 00104 00105 declareOption(ol, "uniformize_extra", 00106 &UniformizeVMatrix::uniformize_extra, 00107 OptionBase::buildoption, 00108 "Whether or not to uniformize the extra part."); 00109 00110 declareOption(ol, "nquantiles", 00111 &UniformizeVMatrix::nquantiles, 00112 OptionBase::buildoption, 00113 "Number of intervals used to divide the sorted values."); 00114 00115 declareOption(ol, "train_source", &UniformizeVMatrix::train_source, 00116 OptionBase::buildoption, 00117 "An optional VMat that will be used instead of 'source' to compute\n" 00118 "the transformation parameters from the distribution statistics."); 00119 00120 // Now call the parent class' declareOptions 00121 inherited::declareOptions(ol); 00122 } 00123 00125 // build // 00127 void UniformizeVMatrix::build() 00128 { 00129 inherited::build(); 00130 build_(); 00131 } 00132 00134 // build_ // 00136 void UniformizeVMatrix::build_() 00137 { 00138 if (!source) 00139 return; 00140 00141 PLASSERT( max >= min ); 00142 00143 if (train_source) { 00144 PLASSERT( train_source->width() == source->width() ); 00145 PLASSERT( train_source->inputsize() == source->inputsize() && 00146 train_source->targetsize() == source->targetsize() && 00147 train_source->weightsize() == source->weightsize() && 00148 train_source->extrasize() == source->extrasize() ); 00149 } 00150 00151 VMat the_source = train_source ? train_source : source; 00152 00153 PLASSERT( the_source->inputsize() >= 0 && the_source->targetsize() >= 0 && 00154 the_source->weightsize() >= 0 && the_source->extrasize() >= 0 ); 00155 00156 // Find which dimensions to uniformize. 00157 features_to_uniformize.resize(0); 00158 int col = 0; 00159 if (uniformize_input) 00160 features_to_uniformize.append( 00161 TVec<int>(col, col + the_source->inputsize() - 1, 1)); 00162 col += the_source->inputsize(); 00163 if (uniformize_target) 00164 features_to_uniformize.append( 00165 TVec<int>(col, col + the_source->targetsize() - 1, 1)); 00166 col += the_source->targetsize(); 00167 if (uniformize_weight) 00168 features_to_uniformize.append( 00169 TVec<int>(col, col + the_source->weightsize() - 1, 1)); 00170 col += the_source->weightsize(); 00171 if (uniformize_extra) 00172 features_to_uniformize.append( 00173 TVec<int>(col, col + the_source->extrasize() - 1, 1)); 00174 col += the_source->extrasize(); 00175 00176 // Build the UniformizeLearner and associated PLearnerOutputVMatrix. 00177 uniformize_learner->forget(); 00178 uniformize_learner->which_fieldnums = features_to_uniformize; 00179 uniformize_learner->nquantiles = this->nquantiles; 00180 uniformize_learner->build(); 00181 uniformize_learner->setTrainingSet(the_source); 00182 uniformize_learner->train(); 00183 TVec< PP<PLearner> > learners; 00184 learners.append((UniformizeLearner*) uniformize_learner); 00185 uniformized_source->learners = learners; 00186 uniformized_source->source = this->source; 00187 uniformized_source->build(); 00188 00189 // Obtain meta information from source. 00190 setMetaInfoFromSource(); 00191 } 00192 00194 // getNewRow // 00196 void UniformizeVMatrix::getNewRow(int i, const Vec& v) const 00197 { 00198 PLASSERT( uniformize_learner->stage > 0 ); 00199 uniformized_source->getRow(i, v); 00200 for (int j = 0; j < features_to_uniformize.length(); j++) 00201 v[j] = min + (max - min) * v[j]; 00202 } 00203 00205 // makeDeepCopyFromShallowCopy // 00207 void UniformizeVMatrix::makeDeepCopyFromShallowCopy(CopiesMap& copies) 00208 { 00209 inherited::makeDeepCopyFromShallowCopy(copies); 00210 00211 deepCopyField(train_source, copies); 00212 deepCopyField(features_to_uniformize, copies); 00213 deepCopyField(uniformize_learner, copies); 00214 deepCopyField(uniformized_source, copies); 00215 } 00216 00217 } // end of namespace PLearn 00218 00219 00220 /* 00221 Local Variables: 00222 mode:c++ 00223 c-basic-offset:4 00224 c-file-style:"stroustrup" 00225 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00226 indent-tabs-mode:nil 00227 fill-column:79 00228 End: 00229 */ 00230 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :