Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members

SubMatVariable.cc

Go to the documentation of this file.
00001 // -*- C++ -*- 00002 00003 // PLearn (A C++ Machine Learning Library) 00004 // Copyright (C) 1998 Pascal Vincent 00005 // Copyright (C) 1999-2002 Pascal Vincent, Yoshua Bengio, Rejean Ducharme and University of Montreal 00006 // Copyright (C) 2001-2002 Nicolas Chapados, Ichiro Takeuchi, Jean-Sebastien Senecal 00007 // Copyright (C) 2002 Xiangdong Wang, Christian Dorion 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: SubMatVariable.cc,v 1.8 2004/05/26 20:13:50 tihocan Exp $ 00040 * This file is part of the PLearn library. 00041 ******************************************************* */ 00042 00043 #include "ExtendedVariable.h" 00044 #include "SubMatVariable.h" 00045 //#include "Var_utils.h" 00046 00047 namespace PLearn { 00048 using namespace std; 00049 00050 00053 PLEARN_IMPLEMENT_OBJECT(SubMatVariable, 00054 "ONE LINE DESCR", 00055 "NO HELP"); 00056 00057 SubMatVariable::SubMatVariable(Variable* v, int i, int j, int the_length, int the_width) 00058 : inherited(v, the_length, the_width), 00059 startk(i*v->width()+j), 00060 length_(the_length), 00061 width_(the_width), 00062 i_(i), 00063 j_(j) 00064 { 00065 build_(); 00066 } 00067 00068 void SubMatVariable::build() 00069 { 00070 inherited::build(); 00071 build_(); 00072 } 00073 00074 void SubMatVariable::build_() 00075 { 00076 if (input) { 00077 // input is v from constructor 00078 if(i_ < 0 || i_ + length() > input->length() || j_ < 0 || j_ + width() > input->width()) 00079 PLERROR("In SubMatVariable: requested sub-matrix is out of matrix bounds"); 00080 startk = i_ * input->width() + j_; 00081 } 00082 } 00083 00084 void 00085 SubMatVariable::declareOptions(OptionList &ol) 00086 { 00087 declareOption(ol, "length_", &SubMatVariable::length_, OptionBase::buildoption, ""); 00088 declareOption(ol, "width_", &SubMatVariable::width_, OptionBase::buildoption, ""); 00089 declareOption(ol, "startk", &SubMatVariable::startk, OptionBase::buildoption, ""); 00090 declareOption(ol, "i_", &SubMatVariable::i_, OptionBase::buildoption, ""); 00091 declareOption(ol, "j_", &SubMatVariable::j_, OptionBase::buildoption, ""); 00092 inherited::declareOptions(ol); 00093 } 00094 00095 void SubMatVariable::recomputeSize(int& l, int& w) const 00096 { l=length_; w=width_; } 00097 00098 void SubMatVariable::fprop() 00099 { 00100 if(width()==input->width()) // optimized version for this special case 00101 { 00102 real* inputdata = input->valuedata+startk; 00103 for(int k=0; k<nelems(); k++) 00104 valuedata[k] = inputdata[k]; 00105 } 00106 else // general version 00107 { 00108 real* inputrowdata = input->valuedata+startk; 00109 int thisk=0; 00110 for(int i=0; i<length(); i++) 00111 { 00112 for(int j=0; j<width(); j++) 00113 valuedata[thisk++] = inputrowdata[j]; 00114 inputrowdata += input->width(); 00115 } 00116 } 00117 } 00118 00119 00120 void SubMatVariable::bprop() 00121 { 00122 if(width()==input->width()) // optimized version for this special case 00123 { 00124 real* inputgradient = input->gradientdata+startk; 00125 for(int k=0; k<nelems(); k++) 00126 inputgradient[k] += gradientdata[k]; 00127 } 00128 else // general version 00129 { 00130 real* inputrowgradient = input->gradientdata+startk; 00131 int thisk=0; 00132 for(int i=0; i<length(); i++) 00133 { 00134 for(int j=0; j<width(); j++) 00135 inputrowgradient[j] += gradientdata[thisk++]; 00136 inputrowgradient += input->width(); 00137 } 00138 } 00139 } 00140 00141 00142 void SubMatVariable::bbprop() 00143 { 00144 if (input->diaghessian.length()==0) 00145 input->resizeDiagHessian(); 00146 if(width()==input->width()) // optimized version for this special case 00147 { 00148 real* inputdiaghessian = input->diaghessian.data()+startk; 00149 for(int k=0; k<nelems(); k++) 00150 inputdiaghessian[k] += diaghessiandata[k]; 00151 } 00152 else // general version 00153 { 00154 real* inputrowdiaghessian = input->diaghessiandata+startk; 00155 int thisk=0; 00156 for(int i=0; i<length(); i++) 00157 { 00158 for(int j=0; j<width(); j++) 00159 inputrowdiaghessian[j] += diaghessiandata[thisk++]; 00160 inputrowdiaghessian += input->width(); 00161 } 00162 } 00163 } 00164 00165 00166 void SubMatVariable::symbolicBprop() 00167 { 00168 int i = startk/input->width(); 00169 int j = startk%input->width(); 00170 int topextent = i; 00171 int bottomextent = input->length()-(i+length()); 00172 int leftextent = j; 00173 int rightextent = input->width()-(j+width()); 00174 input->accg(extend(g,topextent,bottomextent,leftextent,rightextent)); 00175 } 00176 00177 00178 void SubMatVariable::rfprop() 00179 { 00180 if (rValue.length()==0) resizeRValue(); 00181 if(width()==input->width()) // optimized version for this special case 00182 { 00183 real* inputrdata = input->rvaluedata+startk; 00184 for(int k=0; k<nelems(); k++) 00185 rvaluedata[k] = inputrdata[k]; 00186 } 00187 else // general version 00188 { 00189 real* inputrowrdata = input->rvaluedata+startk; 00190 int thisk=0; 00191 for(int i=0; i<length(); i++) 00192 { 00193 for(int j=0; j<width(); j++) 00194 rvaluedata[thisk++] = inputrowrdata[j]; 00195 inputrowrdata += input->width(); 00196 } 00197 } 00198 } 00199 00200 } // end of namespace PLearn 00201

Generated on Tue Aug 17 16:07:45 2004 for PLearn by doxygen 1.3.7