PLearn 0.1
|
00001 // -*- C++ -*- 00002 00003 // SelectRowsMultiInstanceVMatrix.cc 00004 // 00005 // Copyright (C) 2005 Benoit Cromp 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: .pyskeleton_header 544 2003-09-01 00:05:31Z plearner $ 00037 ******************************************************* */ 00038 00039 // Authors: Benoit Cromp 00040 00042 #include <plearn/math/PRandom.h> 00043 #include <plearn_learners/generic/PLearner.h> 00044 #include "SelectRowsMultiInstanceVMatrix.h" 00045 00046 namespace PLearn { 00047 using namespace std; 00048 00049 PLEARN_IMPLEMENT_OBJECT( 00050 SelectRowsMultiInstanceVMatrix, 00051 "Inside a multi-instance vmat, select randomly a fraction of the bag.", 00052 "source_select and multi_nnet should be compatible, as to produce \n" 00053 " a selection of the best conformation from each bag.If source_select \n" 00054 " is not provided, source is used instead. If source_select is provided,\n" 00055 " we consider that source is a ConcatColumnVMatrix of some VMatrix to pass \n" 00056 " along and a VMatrix of the same SIZES as source_select. We get rid of this last \n" 00057 " part after computing the best conformation of a bag.\n" 00058 "Then, we randomly select a fraction of the remaining conformation for each bag.\n" 00059 "If we don't provide source_select(i.e. 0*) then source_ is used\n" ); 00060 00062 // SelectRowsMultiInstanceVMatrix // 00064 SelectRowsMultiInstanceVMatrix::SelectRowsMultiInstanceVMatrix() : 00065 /* ### Initialize all fields to their default value */ 00066 seed(-1), 00067 frac(0.5), 00068 random_generator(new PRandom()) 00069 { 00070 // ... 00071 // ### You may (or not) want to call build_() to finish building the object 00072 // ### (doing so assumes the parent classes' build_() have been called too 00073 // ### in the parent classes' constructors, something that you must ensure) 00074 } 00075 00077 // declareOptions // 00079 void SelectRowsMultiInstanceVMatrix::declareOptions(OptionList& ol) 00080 { 00081 // ### Declare all of this object's options here 00082 // ### For the "flags" of each option, you should typically specify 00083 // ### one of OptionBase::buildoption, OptionBase::learntoption or 00084 // ### OptionBase::tuningoption. Another possible flag to be combined with 00085 // ### is OptionBase::nosave 00086 00087 declareOption(ol, "seed", &SelectRowsMultiInstanceVMatrix::seed, OptionBase::buildoption, "Random generator seed (>0) (exceptions : -1 = initialized from clock, 0 = no initialization)."); 00088 declareOption(ol, "multi_nnet", &SelectRowsMultiInstanceVMatrix::multi_nnet, OptionBase::buildoption, "MultiNNet from which you select instances"); 00089 declareOption(ol, "source_select", &SelectRowsMultiInstanceVMatrix::source_select, OptionBase::buildoption, "The VMat from which we compute the example to be selected."); 00090 declareOption(ol, "frac", &SelectRowsMultiInstanceVMatrix::frac, OptionBase::buildoption, "Fraction of the bag to be randomly chosen (Note: this is the fraction of the bag to be randomly chosen in addition of the best instance, which is always added"); 00091 // Now call the parent class' declareOptions 00092 inherited::declareOptions(ol); 00093 00094 } 00095 00097 // build // 00099 void SelectRowsMultiInstanceVMatrix::build() 00100 { 00101 // ### Nothing to add here, simply calls build_ 00102 inherited::build(); 00103 build_(); 00104 } 00105 00107 // build_ // 00109 void SelectRowsMultiInstanceVMatrix::build_() 00110 { 00111 // ### This method should do the real building of the object, 00112 // ### according to set 'options', in *any* situation. 00113 // ### Typical situations include: 00114 // ### - Initial building of an object from a few user-specified options 00115 // ### - Building of a "reloaded" object: i.e. from the complete set of all serialised options. 00116 // ### - Updating or "re-building" of an object after a few "tuning" options have been modified. 00117 // ### You should assume that the parent class' build_() has already been called. 00118 00119 random_generator->manual_seed(seed); 00120 00121 if (!source) 00122 return; 00123 00124 // VMat source_of_indices = source_select ? source_select : source; 00125 00126 // if (!source_of_indices) 00127 // return; 00128 00129 if (source_select && source && source_select->width() >= source->width()) 00130 PLERROR("In SelectRowsMultiInstanceVMatrix::build_ - VMats 'source_select'" 00131 "should be completely present inside 'source'. Width does not work."); 00132 00133 // Generating 'indices' and 'mi_info' vector. 00134 00135 int bag_signal_column = source->targetsize() - 1; 00136 int first_row = 0; 00137 int x_max; 00138 00139 indices.resize(0); // This get rid of the user's build option value. 00140 mi_info.resize(0); 00141 TVec<int> bag_indices; 00142 TVec<double> bag_prob; 00143 bag_indices.resize(0); 00144 bag_prob.resize(0); 00145 // Vec prob; 00146 Vec input,target,output(1); 00147 real weight; 00148 int indices_size; 00149 for(int row=0; row<source->length(); row++) 00150 { 00151 source->getExample(row,input,target,weight); 00152 if (source_select) { 00153 int minnet_inputsize = source->width() - source_select->inputsize() - source_select->targetsize() - source_select->weightsize(); 00154 input = input.subVec(minnet_inputsize-1,source_select->inputsize()); 00155 } 00156 switch(int(target[bag_signal_column])) 00157 { 00158 case 0: 00159 multi_nnet->computeOutput(input,output); 00160 bag_prob.push_back(output[0]); 00161 break; 00162 case 1: 00163 first_row = row; 00164 multi_nnet->computeOutput(input, output); 00165 bag_prob.push_back(output[0]); 00166 break; 00167 case 2: 00168 multi_nnet->computeOutput(input, output); 00169 bag_prob.push_back(output[0]); 00170 x_max=argmax(bag_prob); 00171 bag_indices.resize(0); 00172 for(int i=0;i<=row-first_row;i++) { 00173 if(i!=x_max) bag_indices.push_back(i); 00174 } 00175 random_generator->shuffleElements(bag_indices); 00176 // Append retained elements to indices 00177 indices_size = indices.length(); 00178 indices.push_back(first_row+x_max); 00179 for(int i=0;i<frac*bag_indices.length();i++) { 00180 indices.push_back(first_row+bag_indices[i]); 00181 } 00182 if(indices.length()-indices_size ==1) mi_info.push_back(3); 00183 if(indices.length()-indices_size > 1) { 00184 mi_info.push_back(1); 00185 for(int i=0;i<indices.length()-indices_size-2;i++) mi_info.push_back(0); 00186 mi_info.push_back(2); 00187 } 00188 bag_prob.resize(0); 00189 break; 00190 case 3: 00191 indices.push_back(row); 00192 mi_info.push_back(3); 00193 break; 00194 }; 00195 } 00196 00197 // ?? Modify the width, length, (targetsize, inputsize and weight) size attribute. 00198 // We suppose that the source and source_select have the same targetsize and weightsize 00199 length_ = indices.length(); 00200 if(!source_select) { 00201 inputsize_ = source->inputsize(); 00202 } else { 00203 inputsize_ = source->width() - source_select->inputsize()- (2 * source_select->targetsize()) - (2 * source_select->weightsize()); 00204 } 00205 targetsize_ = source->targetsize(); 00206 weightsize_ = source->weightsize(); 00207 width_ = inputsize() + targetsize() + weightsize(); 00208 00209 // ### In a SourceVMatrix, you will typically end build_() with: 00210 setMetaInfoFromSource(); 00211 updateMtime(source_select); 00212 } 00213 00215 // getNewRow // 00217 void SelectRowsMultiInstanceVMatrix::getNewRow(int i, const Vec& v) const 00218 { 00219 int bag_signal_column = inputsize_ + targetsize_ - 1; 00220 source->getRow(indices[i], v); 00221 v[bag_signal_column]=mi_info[i]; 00222 } 00223 00225 // makeDeepCopyFromShallowCopy // 00227 void SelectRowsMultiInstanceVMatrix::makeDeepCopyFromShallowCopy(CopiesMap& copies) 00228 { 00229 inherited::makeDeepCopyFromShallowCopy(copies); 00230 00231 deepCopyField(indices, copies); 00232 deepCopyField(mi_info, copies); 00233 deepCopyField(multi_nnet, copies); 00234 deepCopyField(source_select, copies); 00235 deepCopyField(random_generator, copies); 00236 } 00237 00238 } // end of namespace PLearn 00239 00240 00241 /* 00242 Local Variables: 00243 mode:c++ 00244 c-basic-offset:4 00245 c-file-style:"stroustrup" 00246 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00247 indent-tabs-mode:nil 00248 fill-column:79 00249 End: 00250 */ 00251 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :