PLearn 0.1
|
00001 // -*- C++ -*- 00002 00003 // ClassSubsetVMatrix.cc 00004 // 00005 // Copyright (C) 2004 Olivier Delalleau 00006 // Copyright (C) 2008 Jerome Louradour 00007 // 00008 // Redistribution and use in source and binary forms, with or without 00009 // modification, are permitted provided that the following conditions are met: 00010 // 00011 // 1. Redistributions of source code must retain the above copyright 00012 // notice, this list of conditions and the following disclaimer. 00013 // 00014 // 2. Redistributions in binary form must reproduce the above copyright 00015 // notice, this list of conditions and the following disclaimer in the 00016 // documentation and/or other materials provided with the distribution. 00017 // 00018 // 3. The name of the authors may not be used to endorse or promote 00019 // products derived from this software without specific prior written 00020 // permission. 00021 // 00022 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 00023 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00024 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 00025 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00026 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00027 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00028 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00029 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00030 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00031 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00032 // 00033 // This file is part of the PLearn library. For more information on the PLearn 00034 // library, go to the PLearn Web site at www.plearn.org 00035 00036 /* ******************************************************* 00037 * $Id: ClassSubsetVMatrix.cc 3761 2005-07-11 18:13:16Z tihocan $ 00038 ******************************************************* */ 00039 00040 // Authors: Hugo Larochelle 00041 00045 #include "ClassSubsetVMatrix.h" 00046 00047 namespace PLearn { 00048 using namespace std; 00049 00050 PLEARN_IMPLEMENT_OBJECT(ClassSubsetVMatrix, 00051 "A VMatrix that keeps examples for a subset of the classes (target).", 00052 "" 00053 ); 00054 00056 // ClassSubsetVMatrix // 00058 ClassSubsetVMatrix::ClassSubsetVMatrix(): 00059 redistribute_classes(false), 00060 one_vs_minus_one_classification(false) 00061 {} 00062 00063 ClassSubsetVMatrix::ClassSubsetVMatrix(VMat the_source, 00064 const TVec<int>& the_classes, 00065 bool call_build_): 00066 // There is no need to explicitely call the parent's constructor since 00067 // the whole build process will be done in build_() if necessary. 00068 classes(the_classes.copy()), 00069 redistribute_classes(false), 00070 one_vs_minus_one_classification(false) 00071 { 00072 source = the_source; 00073 if (call_build_) 00074 build_(); 00075 } 00076 00077 ClassSubsetVMatrix::ClassSubsetVMatrix(VMat the_source, int the_class, 00078 bool call_build_): 00079 // There is no need to explicitely call the parent's constructor since 00080 // the whole build process will be done in build_() if necessary. 00081 classes(TVec<int>(1, the_class)), 00082 redistribute_classes(false), 00083 one_vs_minus_one_classification(false) 00084 { 00085 source = the_source; 00086 if (call_build_) 00087 build_(); 00088 } 00089 00091 // declareOptions // 00093 void ClassSubsetVMatrix::declareOptions(OptionList& ol) 00094 { 00095 // ### Declare all of this object's options here 00096 // ### For the "flags" of each option, you should typically specify 00097 // ### one of OptionBase::buildoption, OptionBase::learntoption or 00098 // ### OptionBase::tuningoption. Another possible flag to be combined with 00099 // ### is OptionBase::nosave 00100 00101 declareOption(ol, "classes", &ClassSubsetVMatrix::classes, OptionBase::buildoption, 00102 "Classes of examples to keep.\n"); 00103 00104 declareOption(ol, "redistribute_classes", &ClassSubsetVMatrix::redistribute_classes, OptionBase::buildoption, 00105 "Indication that the class values should be redistributed between 0 and classes.length()-1,\n" 00106 "based on the order of apperance in the vector classes.\n"); 00107 00108 declareOption(ol, "one_vs_minus_one_classification", &ClassSubsetVMatrix::one_vs_minus_one_classification, OptionBase::buildoption, 00109 "Indication that, if classes contains 2 class indexes,\n" 00110 "than they should be mapped to -1 (the first index) and 1.\n"); 00111 00112 // Now call the parent class' declareOptions 00113 inherited::declareOptions(ol); 00114 } 00115 00117 // build // 00119 void ClassSubsetVMatrix::build() 00120 { 00121 inherited::build(); 00122 build_(); 00123 } 00124 00126 // build_ // 00128 void ClassSubsetVMatrix::build_() 00129 { 00130 if (source) { 00131 setMetaInfoFrom(source); 00132 input.resize(inputsize()); 00133 target.resize(targetsize()); 00134 indices.clear(); 00135 for(int i=0;i<source->length();i++) 00136 { 00137 source->getExample(i,input,target,weight); 00138 if(classes.find((int)target[0]) != -1) 00139 indices.push_back(i); 00140 } 00141 //if(indices.length() == 0) 00142 // PLERROR("In ClassSubsetVMatrix::build_(): no examples kept"); 00143 inherited::build(); 00144 if(one_vs_minus_one_classification && classes.length()!=2) 00145 PLERROR("In ClassSubsetVMatrix::build_(): no examples kept"); 00146 } 00147 } 00148 00149 real ClassSubsetVMatrix::get(int i, int j) const 00150 { 00151 if(!redistribute_classes || j != inputsize()) 00152 return source->get(indices[i], j); 00153 else 00154 { 00155 if(one_vs_minus_one_classification) 00156 return 2*classes.find((int)source->get(indices[i], j))-1; 00157 else 00158 return classes.find((int)source->get(indices[i], j)); 00159 } 00160 } 00161 00162 void ClassSubsetVMatrix::getSubRow(int i, int j, Vec v) const 00163 { 00164 source->getSubRow(indices[i], j, v); 00165 if(redistribute_classes && j+v.length() > inputsize()) 00166 { 00167 if(one_vs_minus_one_classification) 00168 v[inputsize()-j] = 2*classes.find((int)v[inputsize()-j])-1; 00169 else 00170 v[inputsize()-j] = classes.find((int)v[inputsize()-j]); 00171 } 00172 } 00173 00174 00176 // makeDeepCopyFromShallowCopy // 00178 void ClassSubsetVMatrix::makeDeepCopyFromShallowCopy(CopiesMap& copies) 00179 { 00180 inherited::makeDeepCopyFromShallowCopy(copies); 00181 deepCopyField(classes,copies); 00182 deepCopyField(input,copies); 00183 deepCopyField(target,copies); 00184 //PLERROR("ClassSubsetVMatrix::makeDeepCopyFromShallowCopy not fully (correctly) implemented yet!"); 00185 } 00186 00187 } // end of namespace PLearn 00188