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

UnfoldedFuncVariable.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: UnfoldedFuncVariable.cc,v 1.10 2004/04/27 16:05:33 morinf Exp $ 00040 * This file is part of the PLearn library. 00041 ******************************************************* */ 00042 00043 #include "UnfoldedFuncVariable.h" 00044 //#include "PLMPI.h" 00045 //#include "DisplayUtils.h" 00046 00047 namespace PLearn { 00048 using namespace std; 00049 00050 00051 00054 PLEARN_IMPLEMENT_OBJECT(UnfoldedFuncVariable, "Variable that puts in the rows of its output matrix the value\n" 00055 "of a Func evaluated on each row of an input matrix.\n", 00056 "The input_matrix and output matrix have n_unfold rows. A separate propagation path\n" 00057 "is created that maps (using the Func as a template) each input row to each output row.\n" 00058 "The parents of this variable include the non-input parents of the Func.\n"); 00059 00060 UnfoldedFuncVariable::UnfoldedFuncVariable() 00061 : transpose(0) 00062 {} 00063 00064 UnfoldedFuncVariable::UnfoldedFuncVariable(Var inputmatrix, Func the_f, bool the_transpose) 00065 : inherited(nonInputParentsOfPath(the_f->inputs,the_f->outputs) & inputmatrix, 00066 the_transpose ? the_f->outputs[0]->length()*the_f->outputs[0]->width() : inputmatrix->length(), 00067 the_transpose ? inputmatrix->width() : the_f->outputs[0]->length()*the_f->outputs[0]->width()), 00068 input_matrix(inputmatrix), 00069 f(the_f), 00070 transpose(the_transpose) 00071 { 00072 build(); 00073 } 00074 00075 void UnfoldedFuncVariable::build() 00076 { 00077 inherited::build(); 00078 build_(); 00079 } 00080 00081 void UnfoldedFuncVariable::build_() 00082 { 00083 if (f) { 00084 if(f->outputs.size()!=1) 00085 PLERROR("In UnfoldedFuncVariable: function must have a single variable output (maybe you can vconcat the vars into a single one prior to calling sumOf, if this is really what you want)"); 00086 f->inputs.setDontBpropHere(true); 00087 int n_unfold = transpose ? input_matrix->width() : input_matrix->length(); 00088 inputs.resize(n_unfold); 00089 outputs.resize(n_unfold); 00090 f_paths.resize(n_unfold); 00091 for (int i=0;i<n_unfold;i++) 00092 { 00093 inputs[i].resize(f->inputs.size()); 00094 for (int j = 0; j < f->inputs.size(); j++) { 00095 inputs[i][j] = Var(f->inputs[j]->length(), f->inputs[j]->width()); 00096 } 00097 outputs[i] = f(inputs[i])[0]; 00098 f_paths[i] = propagationPath(inputs[i],outputs[i]); 00099 } 00100 } 00101 } 00102 00103 void UnfoldedFuncVariable::declareOptions(OptionList& ol) 00104 { 00105 declareOption(ol, "f", &UnfoldedFuncVariable::f, OptionBase::buildoption, 00106 " Func that is replicated for each element of the 'bag' taken from the VMat."); 00107 00108 declareOption(ol, "input_matrix", &UnfoldedFuncVariable::input_matrix, OptionBase::buildoption, 00109 " Var that contains the data, with multiple consecutive rows forming one bag.\n"); 00110 00111 declareOption(ol, "transpose", &UnfoldedFuncVariable::transpose, OptionBase::buildoption, 00112 " If set to 1, then instead puts in the columns of the output matrix the values\n" 00113 " of f at the columns of the input matrix."); 00114 00115 inherited::declareOptions(ol); 00116 } 00117 00118 00119 void UnfoldedFuncVariable::recomputeSize(int& l, int& w) const 00120 { 00121 if (f && f->outputs.size()) { 00122 w = f->outputs[0]->length()*f->outputs[0]->width(); 00123 if (transpose) { 00124 l = w; 00125 w = input_matrix->width(); 00126 } else { 00127 l = input_matrix->length(); 00128 } 00129 } else 00130 l = w = 0; 00131 } 00132 00133 00134 void UnfoldedFuncVariable::makeDeepCopyFromShallowCopy(map<const void*, void*>& copies) 00135 { 00136 NaryVariable::makeDeepCopyFromShallowCopy(copies); 00137 deepCopyField(input_matrix, copies); 00138 deepCopyField(f, copies); 00139 deepCopyField(inputs, copies); 00140 deepCopyField(outputs, copies); 00141 deepCopyField(f_paths, copies); 00142 } 00143 00144 00145 void UnfoldedFuncVariable::fprop() 00146 { 00147 int n_unfold = transpose ? input_matrix->width() : input_matrix->length(); 00148 for (int i=0;i<n_unfold;i++) { 00149 if (transpose) { 00150 Vec tmp = input_matrix->matValue.column(i).toVecCopy(); // TODO something more efficient 00151 inputs[i] << tmp; 00152 } else { 00153 inputs[i] << input_matrix->matValue(i); 00154 } 00155 f_paths[i].fprop(); 00156 if (transpose) { 00157 matValue.column(i) << outputs[i]->value; 00158 } else { 00159 matValue(i) << outputs[i]->value; 00160 } 00161 } 00162 } 00163 00164 00165 void UnfoldedFuncVariable::bprop() 00166 { 00167 int n_unfold = transpose ? input_matrix->width() : input_matrix->length(); 00168 for (int i=0;i<n_unfold;i++) 00169 { 00170 f_paths[i].clearGradient(); 00171 if (transpose) { 00172 Vec tmp = matGradient.column(i).toVecCopy(); // TODO more efficient + check while it compiled without tmp = toVecCopy 00173 outputs[i]->gradient << tmp; 00174 } else { 00175 outputs[i]->gradient << matGradient(i); 00176 } 00177 f_paths[i].bprop(); 00178 } 00179 } 00180 00181 00182 void UnfoldedFuncVariable::printInfo(bool print_gradient) 00183 { 00184 int n_unfold = transpose ? input_matrix->width() : input_matrix->length(); 00185 for (int i=0;i<n_unfold;i++) 00186 f_paths[i].printInfo(print_gradient); 00187 cout << info() << " : " << getName() << "[" << (void*)this << "]" 00188 << "(input_matrix=" << (void*)input_matrix << " "; 00189 for(int i=0; i<n_unfold; i++) cout << (void*)outputs[i] << " "; 00190 cout << ") = " << value; 00191 if (print_gradient) cout << " gradient=" << gradient; 00192 cout << endl; 00193 } 00194 00195 00196 } // end of namespace PLearn 00197 00198

Generated on Tue Aug 17 16:09:38 2004 for PLearn by doxygen 1.3.7