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

MatrixElementsVariable.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: MatrixElementsVariable.cc,v 1.8 2004/05/04 21:15:53 nova77 Exp $ 00040 * This file is part of the PLearn library. 00041 ******************************************************* */ 00042 00043 #include "MatrixElementsVariable.h" 00044 00045 namespace PLearn { 00046 using namespace std; 00047 00048 00049 00051 /* fills the elements of a matrix using the given scalar Variable expression, 00052 that depends on index variables i and j, that loop from 00053 0 to ni-1 and 0 to nj-1 respectively. */ 00054 00055 PLEARN_IMPLEMENT_OBJECT(MatrixElementsVariable, 00056 "Fills the elements of a matrix using the given scalar Variable " 00057 "expression, that depends on index variables i and j, that loop from " 00058 "0 to ni-1 and 0 to nj-1 respectively.", 00059 "NO HELP"); 00060 00061 00062 MatrixElementsVariable::MatrixElementsVariable(Variable* the_expression, const Var& i_index, 00063 const Var& j_index, int number_of_i_values, 00064 int number_of_j_values, const VarArray& the_parameters) 00065 : inherited(the_parameters, number_of_i_values, number_of_j_values), i(i_index), 00066 j(j_index), ni(number_of_i_values), nj(number_of_j_values), expression(the_expression), 00067 parameters(the_parameters) 00068 { 00069 build_(); 00070 } 00071 00072 void 00073 MatrixElementsVariable::build() 00074 { 00075 inherited::build(); 00076 } 00077 00078 void 00079 MatrixElementsVariable::build_() 00080 { 00081 if (i && j && (parameters.size() > 0) && expression) { 00082 if (!i->isScalar()) 00083 PLERROR("MatrixElementsVariable expect a scalar index Var i_index"); 00084 if (!j->isScalar()) 00085 PLERROR("MatrixElementsVariable expect a scalar index Var j_index"); 00086 00087 full_fproppath = propagationPath(parameters&(VarArray)i&(VarArray)j, expression); 00088 fproppath = propagationPath(i&j, expression); 00089 bproppath = propagationPath(parameters, expression); 00090 } 00091 } 00092 00093 void 00094 MatrixElementsVariable::declareOptions(OptionList &ol) 00095 { 00096 declareOption(ol, "i", &MatrixElementsVariable::i, OptionBase::buildoption, ""); 00097 declareOption(ol, "j", &MatrixElementsVariable::j, OptionBase::buildoption, ""); 00098 declareOption(ol, "ni", &MatrixElementsVariable::ni, OptionBase::buildoption, ""); 00099 declareOption(ol, "nj", &MatrixElementsVariable::nj, OptionBase::buildoption, ""); 00100 declareOption(ol, "expression", &MatrixElementsVariable::expression, OptionBase::buildoption, ""); 00101 declareOption(ol, "parameters", &MatrixElementsVariable::parameters, OptionBase::buildoption, ""); 00102 inherited::declareOptions(ol); 00103 } 00104 00105 void MatrixElementsVariable::recomputeSize(int& l, int& w) const 00106 { l=ni; w=nj; } 00107 00108 00109 extern void varDeepCopyField(Var& field, CopiesMap& copies); 00110 00111 void MatrixElementsVariable::makeDeepCopyFromShallowCopy(map<const void*, void*>& copies) 00112 { 00113 NaryVariable::makeDeepCopyFromShallowCopy(copies); 00114 varDeepCopyField(i, copies); 00115 varDeepCopyField(j, copies); 00116 varDeepCopyField(expression, copies); 00117 deepCopyField(parameters, copies); 00118 deepCopyField(full_fproppath, copies); 00119 deepCopyField(fproppath, copies); 00120 deepCopyField(bproppath, copies); 00121 } 00122 00123 00124 void MatrixElementsVariable::fprop() 00125 { 00126 int ii,jj; 00127 00128 for (ii=0;ii<ni;ii++) 00129 { 00130 i->value[0]=ii; 00131 for (jj=0;jj<nj;jj++) 00132 { 00133 j->value[0]=jj; 00134 if (ii==0 && jj==0) 00135 // effect of parameters is computed only once 00136 full_fproppath.fprop(); 00137 else 00138 fproppath.fprop(); 00139 matValue(ii,jj) = expression->value[0]; 00140 } 00141 } 00142 } 00143 00144 00145 void MatrixElementsVariable::bprop() 00146 { 00147 int ii,jj; 00148 00149 for (ii=0;ii<ni;ii++) 00150 { 00151 i->value[0]=ii; 00152 for (jj=0;jj<nj;jj++) 00153 { 00154 j->value[0]=jj; 00155 if (ii==0 && jj==0) 00156 // effect of parameters is computed only once 00157 full_fproppath.fprop(); 00158 else 00159 fproppath.fprop(); 00160 bproppath.clearGradient(); 00161 // PASCAL: peut-etre qu'ici on devrait avoir += ? 00162 // (si l'expression est une Var utilisee ailleurs!) 00163 // REPONSE: Non! (expression ne devrait etre utilisee 00164 // nulle part ailleurs. To do: utilisation de Func 00165 // pour le garantir...) 00166 expression->gradient[0] = matGradient(ii,jj); 00167 bproppath.bprop(); 00168 } 00169 } 00170 } 00171 00172 00173 void MatrixElementsVariable::fbprop() 00174 { 00175 int ii,jj; 00176 00177 for (ii=0;ii<ni;ii++) 00178 { 00179 i->value[0]=ii; 00180 for (jj=0;jj<nj;jj++) 00181 { 00182 j->value[0]=jj; 00183 if (ii==0 && jj==0) 00184 // effect of parameters is computed only once 00185 full_fproppath.fprop(); 00186 else 00187 fproppath.fprop(); 00188 matValue(ii,jj) = expression->value[0]; 00189 bproppath.clearGradient(); 00190 // PASCAL: peut-etre qu'ici on devrait avoir += ? 00191 // (si l'expression est une Var utilisee ailleurs!) 00192 // REPONSE: Non! (expression ne devrait etre utilisee 00193 // nulle part ailleurs. To do: utilisation de Func 00194 // pour le garantir...) 00195 expression->gradient[0] = matGradient(ii,jj); 00196 bproppath.bprop(); 00197 } 00198 } 00199 } 00200 00201 } // end of namespace PLearn

Generated on Tue Aug 17 15:58:13 2004 for PLearn by doxygen 1.3.7