PLearn 0.1
|
00001 // -*- C++ -*- 00002 00003 // RandomSamplesFromVMatrix.cc 00004 // 00005 // Copyright (C) 2006 Hugo Larochelle 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: Hugo Larochelle 00036 00040 #include "RandomSamplesFromVMatrix.h" 00041 00042 namespace PLearn { 00043 using namespace std; 00044 00045 00046 PLEARN_IMPLEMENT_OBJECT( 00047 RandomSamplesFromVMatrix, 00048 "VMatrix that contains random samples from a VMatrix", 00049 "The samples are ALWAYS random, i.e. they are not pre-defined at building time.\n" 00050 "The only exception is when the same row is accessed many consecutive times,\n" 00051 "in which case the same example is returned. The default length of this VMatrix\n" 00052 "is the length of the source VMatrix. The user can either specify directly the\n" 00053 "derised length or specify what should be the ratio of this VMatrix length and \n" 00054 "the source VMatrix length." 00055 ); 00056 00057 RandomSamplesFromVMatrix::RandomSamplesFromVMatrix(): 00058 flength(-1), 00059 seed(0), 00060 rgen(new PRandom()) 00061 { 00062 } 00063 00064 void RandomSamplesFromVMatrix::getNewRow(int i, const Vec& v) const 00065 { 00066 source->getRow(rgen->uniform_multinomial_sample(source->length()),v); 00067 } 00068 00069 void RandomSamplesFromVMatrix::declareOptions(OptionList& ol) 00070 { 00071 declareOption(ol, "source", &RandomSamplesFromVMatrix::source, 00072 OptionBase::buildoption, 00073 "VMatrix from which the samples are taken"); 00074 00075 declareOption(ol, "flength", &RandomSamplesFromVMatrix::flength, 00076 OptionBase::buildoption, 00077 "If provided, will overwrite length by flength * source->length()"); 00078 declareOption(ol, "seed", &RandomSamplesFromVMatrix::seed, 00079 OptionBase::buildoption, 00080 "The initial seed for the random number generator used in this\n" 00081 "VMatrix, for sample selection.\n" 00082 "If -1 is provided, then a 'random' seed is chosen based on time\n" 00083 "of day, ensuring that different experiments run differently.\n" 00084 "If 0 is provided, no (re)initialization of the random number\n" 00085 "generator is performed.\n" 00086 "With a given positive seed, this VMatrix should always select\n" 00087 "the same sequence of samples."); 00088 00089 00090 00091 // Now call the parent class' declareOptions 00092 inherited::declareOptions(ol); 00093 } 00094 00095 void RandomSamplesFromVMatrix::build_() 00096 { 00097 if(source) 00098 { 00099 updateMtime(source); 00100 if(flength > 0) 00101 length_ = int(flength * source->length()); 00102 if(length_ < 0) 00103 length_ = source->length(); 00104 if(seed != 0) 00105 rgen->manual_seed(seed); 00106 00107 width_ = source->width(); 00108 if(inputsize_ < 0) inputsize_ = source->inputsize(); 00109 if(targetsize_ < 0) targetsize_ = source->targetsize(); 00110 if(weightsize_ < 0) weightsize_ = source->weightsize(); 00111 fieldinfos = source->fieldinfos; 00112 } 00113 } 00114 00115 // ### Nothing to add here, simply calls build_ 00116 void RandomSamplesFromVMatrix::build() 00117 { 00118 inherited::build(); 00119 build_(); 00120 } 00121 00122 void RandomSamplesFromVMatrix::makeDeepCopyFromShallowCopy(CopiesMap& copies) 00123 { 00124 inherited::makeDeepCopyFromShallowCopy(copies); 00125 00126 deepCopyField(source, copies); 00127 deepCopyField(rgen, copies); 00128 } 00129 00130 00131 PP<Dictionary> RandomSamplesFromVMatrix::getDictionary(int col) const 00132 { 00133 return source->getDictionary(col); 00134 } 00135 00136 void RandomSamplesFromVMatrix::getValues(int row, int col, Vec& values) const 00137 { 00138 PLERROR("In RandomSamplesFromVMatrix::getValues(): Cannot give possible values given a row index because samples are independent of the row index."); 00139 } 00140 00141 void RandomSamplesFromVMatrix::getValues(const Vec& input, int col, Vec& values) const 00142 { 00143 source->getValues(input,col,values); 00144 } 00145 00146 } // end of namespace PLearn 00147 00148 /* 00149 Local Variables: 00150 mode:c++ 00151 c-basic-offset:4 00152 c-file-style:"stroustrup" 00153 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00154 indent-tabs-mode:nil 00155 fill-column:79 00156 End: 00157 */ 00158 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :