PLearn 0.1
|
00001 // -*- C++ -*- 00002 00003 // PLearn (A C++ Machine Learning Library) 00004 // Copyright (C) 1998 Pascal Vincent 00005 // Copyright (C) 1999-2001 Pascal Vincent, Yoshua Bengio, Rejean Ducharme and University of Montreal 00006 // Copyright (C) 2002 Pascal Vincent, Julien Keable, Xavier Saint-Mleux 00007 // Copyright (C) 2003 Olivier Delalleau 00008 // 00009 // Redistribution and use in source and binary forms, with or without 00010 // modification, are permitted provided that the following conditions are met: 00011 // 00012 // 1. Redistributions of source code must retain the above copyright 00013 // notice, this list of conditions and the following disclaimer. 00014 // 00015 // 2. Redistributions in binary form must reproduce the above copyright 00016 // notice, this list of conditions and the following disclaimer in the 00017 // documentation and/or other materials provided with the distribution. 00018 // 00019 // 3. The name of the authors may not be used to endorse or promote 00020 // products derived from this software without specific prior written 00021 // permission. 00022 // 00023 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 00024 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00025 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 00026 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00027 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00028 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00029 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00030 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00031 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00032 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00033 // 00034 // This file is part of the PLearn library. For more information on the PLearn 00035 // library, go to the PLearn Web site at www.plearn.org 00036 00037 00038 /* ******************************************************* 00039 * $Id: SelectRowsVMatrix.cc 9470 2008-09-22 16:00:56Z tihocan $ 00040 ******************************************************* */ 00041 00042 #include "SelectRowsVMatrix.h" 00043 00044 namespace PLearn { 00045 using namespace std; 00046 00049 PLEARN_IMPLEMENT_OBJECT(SelectRowsVMatrix, 00050 "Selects samples from its source according to given vector of indices.", 00051 "" 00052 ); 00053 00054 SelectRowsVMatrix::SelectRowsVMatrix() 00055 : obtained_inputsize_from_source(false), 00056 obtained_targetsize_from_source(false), 00057 obtained_weightsize_from_source(false), 00058 obtained_extrasize_from_source(false), 00059 warn_if_all_rows_selected(true), 00060 rows_to_remove(false) 00061 {} 00062 00063 SelectRowsVMatrix::SelectRowsVMatrix(VMat the_source, TVec<int> the_indices, bool the_rows_to_remove, bool warn) 00064 : obtained_inputsize_from_source(false), 00065 obtained_targetsize_from_source(false), 00066 obtained_weightsize_from_source(false), 00067 obtained_extrasize_from_source(false), 00068 warn_if_all_rows_selected(warn), 00069 indices(the_indices), 00070 rows_to_remove(the_rows_to_remove) 00071 { 00072 source = the_source; 00073 build_(); 00074 } 00075 00077 SelectRowsVMatrix::SelectRowsVMatrix(VMat the_source, Vec the_indices, bool the_rows_to_remove, bool warn) 00078 : obtained_inputsize_from_source(false), 00079 obtained_targetsize_from_source(false), 00080 obtained_weightsize_from_source(false), 00081 obtained_extrasize_from_source(false), 00082 warn_if_all_rows_selected(warn), 00083 rows_to_remove(the_rows_to_remove) 00084 { 00085 source = the_source; 00086 indices.resize(the_indices.length()); 00087 indices << the_indices; // copy to integer indices 00088 build_(); 00089 } 00090 00091 real SelectRowsVMatrix::get(int i, int j) const 00092 { return source->get(selected_indices[i], j); } 00093 00094 void SelectRowsVMatrix::getSubRow(int i, int j, Vec v) const 00095 { source->getSubRow(selected_indices[i], j, v); } 00096 00097 real SelectRowsVMatrix::dot(int i1, int i2, int inputsize) const 00098 { return source->dot(int(selected_indices[i1]), int(selected_indices[i2]), inputsize); } 00099 00100 real SelectRowsVMatrix::dot(int i, const Vec& v) const 00101 { return source->dot(selected_indices[i],v); } 00102 00103 string SelectRowsVMatrix::getString(int row, int col) const 00104 { return source->getString(selected_indices[row], col); } 00105 00106 const map<string,real>& SelectRowsVMatrix::getStringToRealMapping(int col) const 00107 { return source->getStringToRealMapping(col);} 00108 00109 const map<real,string>& SelectRowsVMatrix::getRealToStringMapping(int col) const 00110 { return source->getRealToStringMapping(col);} 00111 00112 void SelectRowsVMatrix::declareOptions(OptionList &ol) 00113 { 00114 // Build options. 00115 00116 declareOption(ol, "indices", &SelectRowsVMatrix::indices, OptionBase::buildoption, 00117 "The array of row indices to extract"); 00118 00119 declareOption(ol, "indices_vmat", &SelectRowsVMatrix::indices_vmat, OptionBase::buildoption, 00120 "If provided, will override the 'indices' option: the indices will be taken\n" 00121 "from the first column of the given VMatrix (taking the closest integer)."); 00122 00123 declareOption(ol, "rows_to_remove", &SelectRowsVMatrix::rows_to_remove, OptionBase::buildoption, 00124 "Indication that the rows specified in indices or indices_vmat\n" 00125 "should be removed, not selected from the source VMatrix."); 00126 00127 // Learnt options. 00128 00129 declareOption(ol, "obtained_inputsize_from_source", &SelectRowsVMatrix::obtained_inputsize_from_source, OptionBase::learntoption, 00130 "Set to 1 if the inputsize was obtained from the source VMat."); 00131 00132 declareOption(ol, "obtained_targetsize_from_source", &SelectRowsVMatrix::obtained_targetsize_from_source, OptionBase::learntoption, 00133 "Set to 1 if the targetsize was obtained from the source VMat."); 00134 00135 declareOption(ol, "obtained_weightsize_from_source", &SelectRowsVMatrix::obtained_weightsize_from_source, OptionBase::learntoption, 00136 "Set to 1 if the weightsize was obtained from the source VMat."); 00137 00138 declareOption(ol, "obtained_extrasize_from_source", &SelectRowsVMatrix::obtained_extrasize_from_source, OptionBase::learntoption, 00139 "Set to 1 if the extrasize was obtained from the source VMat."); 00140 00141 declareOption(ol, "warn_if_all_rows_selected", &SelectRowsVMatrix::warn_if_all_rows_selected, OptionBase::buildoption, 00142 "If true, we generate a warning if we select all row."); 00143 00144 inherited::declareOptions(ol); 00145 00146 // Hide unused options. 00147 // Note: it is not obvious that all the options below are useless, one may 00148 // want to un-hide some in the future. However, the more hidden, the 00149 // simpler for the beginner. 00150 00151 redeclareOption(ol, "writable", &SelectRowsVMatrix::writable, 00152 OptionBase::nosave, "Not used."); 00153 00154 redeclareOption(ol, "length", &SelectRowsVMatrix::length_, 00155 OptionBase::nosave, "Not used."); 00156 00157 redeclareOption(ol, "width", &SelectRowsVMatrix::width_, 00158 OptionBase::nosave, "Not used."); 00159 00160 } 00161 00162 void SelectRowsVMatrix::makeDeepCopyFromShallowCopy(CopiesMap& copies) 00163 { 00164 inherited::makeDeepCopyFromShallowCopy(copies); 00165 deepCopyField(selected_indices, copies); 00166 deepCopyField(indices, copies); 00167 deepCopyField(indices_vmat, copies); 00168 } 00169 00171 // build // 00173 void SelectRowsVMatrix::build() 00174 { 00175 inherited::build(); 00176 build_(); 00177 } 00178 00180 // build_ // 00182 void SelectRowsVMatrix::build_() 00183 { 00184 if (indices_vmat) { 00185 int n = indices_vmat->length(); 00186 indices.resize(n); 00187 for (int i = 0; i < n; i++) 00188 indices[i] = int(round(indices_vmat->get(i,0))); 00189 } 00190 00191 if(rows_to_remove) 00192 { 00193 TVec<bool> tag(source->length()); 00194 tag.fill(true); 00195 int count_to_remove = 0; 00196 for(int i=0; i<indices.length(); i++) { 00197 tag[indices[i]] = false; 00198 count_to_remove++; 00199 } 00200 // Allocate enough memory. 00201 selected_indices.resize(source->length() - count_to_remove); 00202 selected_indices.resize(0); 00203 for(int i=0; i<source->length(); i++) 00204 if(tag[i]) selected_indices.push_back(i); 00205 00206 } 00207 else 00208 { 00209 selected_indices.resize(indices.length()); 00210 selected_indices << indices; 00211 } 00212 //we don't display the warning for SortRowsVMatrix as it always select all row! 00213 if(warn_if_all_rows_selected && selected_indices.length()==source.length() 00214 && source.length()>0) 00215 PLWARNING("In SelectRowsVMatrix::build_() - " 00216 "All rows have been selected!"); 00217 00218 length_ = selected_indices.length(); 00219 if (source) { 00220 string error_msg = 00221 "In SelectRowsVMatrix::build_ - For safety reasons, it is forbidden to " 00222 "re-use sizes obtained from a previous source VMatrix with a new source " 00223 "VMatrix having different sizes"; 00224 width_ = source->width(); 00225 if(inputsize_<0) { 00226 inputsize_ = source->inputsize(); 00227 obtained_inputsize_from_source = true; 00228 } else if (obtained_inputsize_from_source && inputsize_ != source->inputsize()) 00229 PLERROR(error_msg.c_str()); 00230 if(targetsize_<0) { 00231 targetsize_ = source->targetsize(); 00232 obtained_targetsize_from_source = true; 00233 } else if (obtained_targetsize_from_source && targetsize_ != source->targetsize()) 00234 PLERROR(error_msg.c_str()); 00235 if(weightsize_<0) { 00236 weightsize_ = source->weightsize(); 00237 obtained_weightsize_from_source = true; 00238 } else if (obtained_weightsize_from_source && weightsize_ != source->weightsize()) 00239 PLERROR(error_msg.c_str()); 00240 if(extrasize_ == 0) { 00241 extrasize_ = source->extrasize(); 00242 obtained_extrasize_from_source = true; 00243 } else if (obtained_extrasize_from_source && extrasize_ != source->extrasize()) 00244 PLERROR(error_msg.c_str()); 00245 fieldinfos = source->fieldinfos; 00246 } else { 00247 // Restore the original undefined sizes if the current one had been obtained 00248 // from the source VMatrix. 00249 if (obtained_inputsize_from_source) { 00250 inputsize_ = -1; 00251 obtained_inputsize_from_source = false; 00252 } 00253 if (obtained_targetsize_from_source) { 00254 targetsize_ = -1; 00255 obtained_targetsize_from_source = false; 00256 } 00257 if (obtained_weightsize_from_source) { 00258 weightsize_ = -1; 00259 obtained_weightsize_from_source = false; 00260 } 00261 } 00262 } 00263 00264 real SelectRowsVMatrix::getStringVal(int col, const string & str) const 00265 { return source->getStringVal(col, str); } 00266 00267 string SelectRowsVMatrix::getValString(int col, real val) const 00268 { return source->getValString(col,val); } 00269 00270 PP<Dictionary> SelectRowsVMatrix::getDictionary(int col) const 00271 { 00272 #ifdef BOUNDCHECK 00273 if(col>=width_) 00274 PLERROR("access out of bound. Width=%i accessed col=%i",width_,col); 00275 #endif 00276 return source->getDictionary(col); 00277 } 00278 00279 00280 void SelectRowsVMatrix::getValues(int row, int col, Vec& values) const 00281 { 00282 #ifdef BOUNDCHECK 00283 if(col>=width_) 00284 PLERROR("access out of bound. Width=%i accessed col=%i",width_,col); 00285 #endif 00286 source->getValues(selected_indices[row],col,values); 00287 } 00288 00289 void SelectRowsVMatrix::getValues(const Vec& input, int col, Vec& values) const 00290 { 00291 #ifdef BOUNDCHECK 00292 if(col>=width_) 00293 PLERROR("access out of bound. Width=%i accessed col=%i",width_,col); 00294 #endif 00295 source->getValues(input, col, values); 00296 } 00297 00298 } // end of namespace PLearn 00299 00300 00301 /* 00302 Local Variables: 00303 mode:c++ 00304 c-basic-offset:4 00305 c-file-style:"stroustrup" 00306 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00307 indent-tabs-mode:nil 00308 fill-column:79 00309 End: 00310 */ 00311 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :