PLearn 0.1
|
00001 // -*- C++ -*- 00002 00003 // PrecomputedProcessedLearner.cc 00004 // 00005 // Copyright (C) 2006 Nicolas Chapados 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: Nicolas Chapados 00036 00039 #define PL_LOG_MODULE_NAME "PrecomputedProcessedLearner" 00040 00041 #include "PrecomputedProcessedLearner.h" 00042 #include <plearn/io/pl_log.h> 00043 #include <plearn/vmat/PrecomputedVMatrix.h> 00044 #include <plearn/vmat/MemoryVMatrix.h> 00045 #include <plearn/sys/procinfo.h> 00046 00047 namespace PLearn { 00048 using namespace std; 00049 00050 PLEARN_IMPLEMENT_OBJECT( 00051 PrecomputedProcessedLearner, 00052 "Identity Learner with a cached 'processDataSet' method.", 00053 "This learner is functionally the identity learner: it does not learn\n" 00054 "anything, and its computeOutput() produces the same thing as its input.\n" 00055 "HOWEVER, it implements a special function: the results of calls to\n" 00056 "processDataSet produce a CACHED VMatrix of the results, thereby making\n" 00057 "further accesses much faster.\n" 00058 "\n" 00059 "The intended use of this learner is within a chain managed by a\n" 00060 "ChainedLearner, wherein one has the pattern\n" 00061 "\n" 00062 "- 1) Preprocessing, with fairly slow Python (PythonProcessedLearner)\n" 00063 "- 2) Precomputing (this class)\n" 00064 "- 3) NNet or other PLearner which makes multiple passes over its training\n" 00065 " set.\n"); 00066 00067 00068 PrecomputedProcessedLearner::PrecomputedProcessedLearner() 00069 : m_precomp_type("memory") 00070 { } 00071 00072 void PrecomputedProcessedLearner::declareOptions(OptionList& ol) 00073 { 00074 declareOption( 00075 ol, "precomp_type", &PrecomputedProcessedLearner::m_precomp_type, 00076 OptionBase::buildoption, 00077 "Buffering type for precomputed train operations. Can be 'memory',\n" 00078 "'dmat', or 'pmat'. In the first case, a MemoryVMatrix is returned from\n" 00079 "calls to ProcessDataSet. In the last two cases, a PrecomputedVMatrix\n" 00080 "is returned, with the appropriate precompute type set. The metadatadir\n" 00081 "of the vmat is set to be the learner's expdir/ followed by\n" 00082 "'precomputed_processed.metadata'.\n"); 00083 00084 // Now call the parent class' declareOptions 00085 inherited::declareOptions(ol); 00086 } 00087 00088 void PrecomputedProcessedLearner::build_() 00089 { 00090 if (m_precomp_type != "memory" && 00091 m_precomp_type != "dmat" && 00092 m_precomp_type != "pmat" ) 00093 PLERROR("%s: unknown value for option 'precomp_type' (= '%s');\n" 00094 "must be one of: 'memory', 'dmat', 'pmat'", __FUNCTION__, 00095 m_precomp_type.c_str()); 00096 } 00097 00098 00099 // ### Nothing to add here, simply calls build_ 00100 void PrecomputedProcessedLearner::build() 00101 { 00102 inherited::build(); 00103 build_(); 00104 } 00105 00106 00107 void PrecomputedProcessedLearner::makeDeepCopyFromShallowCopy(CopiesMap& copies) 00108 { 00109 inherited::makeDeepCopyFromShallowCopy(copies); 00110 } 00111 00112 00113 int PrecomputedProcessedLearner::outputsize() const 00114 { 00115 return inputsize_; 00116 } 00117 00118 void PrecomputedProcessedLearner::forget() 00119 { 00120 inherited::forget(); 00121 } 00122 00123 void PrecomputedProcessedLearner::train() 00124 { 00125 // No-op 00126 } 00127 00128 00129 void PrecomputedProcessedLearner::computeOutput(const Vec& input, Vec& output) const 00130 { 00131 output << input; 00132 } 00133 00134 00135 void PrecomputedProcessedLearner::computeCostsFromOutputs(const Vec& input, const Vec& output, 00136 const Vec& target, Vec& costs) const 00137 { 00138 // No-op 00139 } 00140 00141 00142 TVec<string> PrecomputedProcessedLearner::getTestCostNames() const 00143 { 00144 return TVec<string>(); 00145 } 00146 00147 00148 TVec<string> PrecomputedProcessedLearner::getTrainCostNames() const 00149 { 00150 return TVec<string>(); 00151 } 00152 00153 00154 VMat PrecomputedProcessedLearner::processDataSet(VMat dataset) const 00155 { 00156 VMat raw_processed = inherited::processDataSet(dataset); 00157 00158 DBG_MODULE_LOG << "BEFORE PRECOMPUTE. Process memory: " 00159 << getProcessDataMemory() << endl; 00160 00161 MODULE_LOG << "Precomputing " 00162 << raw_processed.length() << 'x' << raw_processed.width() 00163 << " to " << m_precomp_type 00164 << endl; 00165 00166 if (m_precomp_type == "memory") { 00167 Mat precomp = raw_processed.toMat(); 00168 00169 DBG_MODULE_LOG << "AFTER PRECOMPUTE. Process memory: " 00170 << getProcessDataMemory() << endl; 00171 00172 return VMat(precomp); 00173 } 00174 else { 00175 PPath expdir = getExperimentDirectory(); 00176 if (expdir.empty()) 00177 PLERROR("%s: an experiment directory must be set in order to use precomp_type '%s'", 00178 __FUNCTION__, m_precomp_type.c_str()); 00179 00180 PP<PrecomputedVMatrix> precomp = new PrecomputedVMatrix; 00181 precomp->source = raw_processed; 00182 precomp->precomp_type = m_precomp_type; 00183 precomp->build(); 00184 precomp->setMetaDataDir(expdir / "precomputed_processed.metadata"); 00185 00186 DBG_MODULE_LOG << "AFTER PRECOMPUTE. Process memory: " 00187 << getProcessDataMemory() << endl; 00188 00189 return (VMatrix*)precomp; 00190 } 00191 } 00192 00193 00194 } // end of namespace PLearn 00195 00196 00197 /* 00198 Local Variables: 00199 mode:c++ 00200 c-basic-offset:4 00201 c-file-style:"stroustrup" 00202 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00203 indent-tabs-mode:nil 00204 fill-column:79 00205 End: 00206 */ 00207 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :