PLearn 0.1
|
00001 // -*- C++ -*- 00002 00003 // TemporalHorizonVMatrix.cc 00004 // 00005 // PLearn (A C++ Machine Learning Library) 00006 // Copyright (C) 2003 Rejean Ducharme, Yoshua Bengio 00007 // Copyright (C) 2003 Pascal Vincent 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: TemporalHorizonVMatrix.cc 8617 2008-03-03 17:45:54Z nouiz $ 00040 ******************************************************* */ 00041 00042 #include "TemporalHorizonVMatrix.h" 00043 00044 namespace PLearn { 00045 using namespace std; 00046 00049 PLEARN_IMPLEMENT_OBJECT(TemporalHorizonVMatrix, 00050 "Delay the last targetsize entries of a source VMat", 00051 "VMat class that delay the last entries of a source" 00052 " VMat by a certain horizon.\n"); 00053 00054 TemporalHorizonVMatrix::TemporalHorizonVMatrix(bool call_build_) 00055 : inherited(call_build_) 00056 { 00057 // don't call build_() because it would do nothing 00058 /* if( call_build_ ) 00059 build_(); */ 00060 } 00061 00062 TemporalHorizonVMatrix::TemporalHorizonVMatrix(VMat the_source, 00063 int the_horizon, 00064 int target_size, 00065 bool call_build_) 00066 : inherited(the_source, 00067 the_source.length()-the_horizon, the_source->width(), 00068 call_build_), 00069 horizon(the_horizon), 00070 targetsize(target_size) 00071 { 00072 fieldinfos = source->fieldinfos; 00073 row_delay.resize(width()); 00074 for (int i=0; i<width(); i++) 00075 row_delay[i] = i<width()-targetsize ? 0 : horizon; 00076 00077 defineSizes(source->inputsize(), 00078 source->targetsize(), 00079 source->weightsize()); 00080 00081 // don't call build_() because it would do nothing 00082 /* if( call_build_ ) 00083 build_(); */ 00084 } 00085 00086 real TemporalHorizonVMatrix::get(int i, int j) const 00087 { return source->get(i+row_delay[j], j); } 00088 00089 void TemporalHorizonVMatrix::put(int i, int j, real value) 00090 { source->put(i+row_delay[j], j, value); } 00091 00092 real TemporalHorizonVMatrix::dot(int i1, int i2, int inputsize) const 00093 { 00094 real res = 0.; 00095 for(int k=0; k<inputsize; k++) 00096 res += source->get(i1+row_delay[k],k)*source->get(i2+row_delay[k],k); 00097 return res; 00098 } 00099 00100 real TemporalHorizonVMatrix::dot(int i, const Vec& v) const 00101 { 00102 real res = 0.; 00103 for(int k=0; k<v.length(); k++) 00104 res += source->get(i+row_delay[k],k)*v[k]; 00105 return res; 00106 } 00107 00108 real TemporalHorizonVMatrix::getStringVal(int col, const string & str) const 00109 { return source->getStringVal(col, str); } 00110 00111 string TemporalHorizonVMatrix::getValString(int col, real val) const 00112 { return source->getValString(col,val); } 00113 00114 string TemporalHorizonVMatrix::getString(int row, int col) const 00115 { return source->getString(row+row_delay[col],col); } 00116 00117 const map<string,real>& TemporalHorizonVMatrix::getStringToRealMapping(int col) const 00118 { return source->getStringToRealMapping(col);} 00119 00120 const map<real,string>& TemporalHorizonVMatrix::getRealToStringMapping(int col) const 00121 { return source->getRealToStringMapping(col);} 00122 00123 void TemporalHorizonVMatrix::declareOptions(OptionList &ol) 00124 { 00125 declareOption(ol, "distr", &TemporalHorizonVMatrix::source, 00126 (OptionBase::learntoption | OptionBase::nosave), 00127 "DEPRECATED - Use 'source' instead."); 00128 00129 declareOption(ol, "horizon", &TemporalHorizonVMatrix::horizon, 00130 OptionBase::buildoption, 00131 "The temporal value by which to delay the source VMat"); 00132 00133 declareOption(ol, "targetsize", &TemporalHorizonVMatrix::targetsize, 00134 OptionBase::buildoption, 00135 "The number of last entries to delay"); 00136 00137 inherited::declareOptions(ol); 00138 } 00139 00140 void TemporalHorizonVMatrix::makeDeepCopyFromShallowCopy(CopiesMap& copies) 00141 { 00142 inherited::makeDeepCopyFromShallowCopy(copies); 00143 } 00144 00146 // build // 00148 void TemporalHorizonVMatrix::build() 00149 { 00150 inherited::build(); 00151 build_(); 00152 } 00153 00155 // build_ // 00157 void TemporalHorizonVMatrix::build_() 00158 { 00159 if (source) { 00160 updateMtime(source); 00161 length_ = source->length()-horizon; 00162 width_ = source->width(); 00163 fieldinfos = source->fieldinfos; 00164 00165 row_delay.resize(width()); 00166 for (int i=0; i<width(); i++) 00167 row_delay[i] = i<width()-targetsize ? 0 : horizon; 00168 00169 defineSizes(source->inputsize(), 00170 source->targetsize(), 00171 source->weightsize()); 00172 } 00173 } 00174 00175 } // end of namespace PLearn 00176 00177 00178 /* 00179 Local Variables: 00180 mode:c++ 00181 c-basic-offset:4 00182 c-file-style:"stroustrup" 00183 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00184 indent-tabs-mode:nil 00185 fill-column:79 00186 End: 00187 */ 00188 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :