PLearn 0.1
|
00001 // -*- C++ -*- 00002 00003 // MemoryCachedKernel.cc 00004 // 00005 // Copyright (C) 2007 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 00040 #include "MemoryCachedKernel.h" 00041 00042 namespace PLearn { 00043 using namespace std; 00044 00045 PLEARN_IMPLEMENT_ABSTRACT_OBJECT( 00046 MemoryCachedKernel, 00047 "Provide some memory-management utilities for kernels.", 00048 "This class is intended as a base class to provide some memory management\n" 00049 "utilities for the data-matrix set with setDataForKernelMatrix function. In\n" 00050 "particular, it provides a single (inline, non-virtual) function to access a\n" 00051 "given input vector of the data matrix. If the data VMatrix passed to\n" 00052 "setDataForKernelMatrix is within a certain size threshold, the VMatrix is\n" 00053 "converted to a Mat and cached to memory (without requiring additional space\n" 00054 "if the VMatrix is actually a MemoryVMatrix), and all further element access\n" 00055 "are done without requiring virtual function calls.\n" 00056 "\n" 00057 "IMPORTANT NOTE: the 'cache_gram_matrix' option is enabled automatically by\n" 00058 "default for this class. This makes the computation of the Gram matrix\n" 00059 "derivatives (with respect to kernel hyperparameters) quite faster in many\n" 00060 "cases. If you really don't want this caching to occur, just set it\n" 00061 "explicitly to false.\n" 00062 "\n" 00063 "This class also provides utility functions to derived classes to compute\n" 00064 "the Gram matrix and its derivative (with respect to kernel hyperparameters)\n" 00065 "without requiring virtual function calls in data access or evaluation\n" 00066 "function.\n" 00067 ); 00068 00069 00070 //##### MemoryCachedKernel::MemoryCachedKernel ############################## 00071 00072 MemoryCachedKernel::MemoryCachedKernel() 00073 : m_cache_threshold(1000000) 00074 { 00075 cache_gram_matrix = true; 00076 } 00077 00078 00079 //##### declareOptions ###################################################### 00080 00081 void MemoryCachedKernel::declareOptions(OptionList& ol) 00082 { 00083 declareOption( 00084 ol, "cache_threshold", &MemoryCachedKernel::m_cache_threshold, 00085 OptionBase::buildoption, 00086 "Threshold on the number of elements to cache the data VMatrix into a\n" 00087 "real matrix. Above this threshold, the VMatrix is left as-is, and\n" 00088 "element access remains virtual. (Default value = 1000000)\n"); 00089 00090 // Now call the parent class' declareOptions 00091 inherited::declareOptions(ol); 00092 } 00093 00094 00095 //##### build ############################################################### 00096 00097 void MemoryCachedKernel::build() 00098 { 00099 // ### Nothing to add here, simply calls build_ 00100 inherited::build(); 00101 build_(); 00102 } 00103 00104 00105 //##### build_ ############################################################## 00106 00107 void MemoryCachedKernel::build_() 00108 { } 00109 00110 00111 //##### makeDeepCopyFromShallowCopy ######################################### 00112 00113 void MemoryCachedKernel::makeDeepCopyFromShallowCopy(CopiesMap& copies) 00114 { 00115 inherited::makeDeepCopyFromShallowCopy(copies); 00116 00117 deepCopyField(m_data_cache, copies); 00118 } 00119 00120 00121 //##### setDataForKernelMatrix ############################################## 00122 00123 void MemoryCachedKernel::setDataForKernelMatrix(VMat the_data) 00124 { 00125 inherited::setDataForKernelMatrix(the_data); 00126 00127 if (the_data.width() * the_data.length() <= m_cache_threshold && 00128 the_data.isNotNull()) 00129 { 00130 m_data_cache = the_data.toMat(); 00131 00132 // Update row cache 00133 const int N = m_data_cache.length(); 00134 m_row_cache.resize(N); 00135 for (int i=0 ; i<N ; ++i) 00136 dataRow(i, m_row_cache[i]); 00137 } 00138 else { 00139 m_data_cache = Mat(); 00140 m_row_cache.resize(0); 00141 } 00142 } 00143 00144 00145 //##### addDataForKernelMatrix ############################################## 00146 00147 void MemoryCachedKernel::addDataForKernelMatrix(const Vec& newrow) 00148 { 00149 inherited::addDataForKernelMatrix(newrow); 00150 00151 if (m_data_cache.isNotNull()) { 00152 const int OLD_N = m_data_cache.length(); 00153 PLASSERT( m_data_cache.length() == m_row_cache.size() ); 00154 m_data_cache.appendRow(newrow); 00155 00156 // Update row cache 00157 m_row_cache.push_back(Vec()); 00158 dataRow(OLD_N, m_row_cache[OLD_N]); 00159 } 00160 } 00161 00162 } // end of namespace PLearn 00163 00164 00165 /* 00166 Local Variables: 00167 mode:c++ 00168 c-basic-offset:4 00169 c-file-style:"stroustrup" 00170 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00171 indent-tabs-mode:nil 00172 fill-column:79 00173 End: 00174 */ 00175 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :