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

UnfoldedSumOfVariable.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: UnfoldedSumOfVariable.cc,v 1.8 2004/07/21 16:30:54 chrish42 Exp $ 00040 * This file is part of the PLearn library. 00041 ******************************************************* */ 00042 00043 #include "UnfoldedSumOfVariable.h" 00044 #include <plearn/sys/PLMPI.h> 00045 #include <plearn/display/DisplayUtils.h> 00046 00047 namespace PLearn { 00048 using namespace std; 00049 00052 PLEARN_IMPLEMENT_OBJECT(UnfoldedSumOfVariable, "Variable that sums the value of a Func evaluated on each row of a matrix.\n", 00053 "However, unlike the SumOfVariable, it does so by unfolding the Func (up to given maximum number\n" 00054 "of times 'max_bag_size'), and it allows that number to be variable. Each of the unfolded Func\n" 00055 "is applied on a different row of the input matrix. The number of rows to sum is specified on the\n" 00056 "fly by another input, the bag_size.\n"); 00057 00058 UnfoldedSumOfVariable::UnfoldedSumOfVariable(Var inputmatrix, Var bagsize, Func the_f, int max_bagsize) 00059 : inherited(nonInputParentsOfPath(the_f->inputs,the_f->outputs) & inputmatrix & bagsize, 00060 the_f->outputs[0]->length(), 00061 the_f->outputs[0]->width()), 00062 input_matrix(inputmatrix), bag_size(bagsize), 00063 f(the_f), max_bag_size(max_bagsize) 00064 { 00065 build(); 00066 } 00067 00068 void UnfoldedSumOfVariable::build() 00069 { 00070 inherited::build(); 00071 build_(); 00072 } 00073 00074 void UnfoldedSumOfVariable::build_() 00075 { 00076 if (f) { 00077 if(f->outputs.size()!=1) 00078 PLERROR("In UnfoldedSumOfVariable: 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)"); 00079 f->inputs.setDontBpropHere(true); 00080 inputs.resize(max_bag_size); 00081 outputs.resize(max_bag_size); 00082 f_paths.resize(max_bag_size); 00083 for (int i=0;i<max_bag_size;i++) 00084 { 00085 inputs[i].resize(f->inputs.size()); 00086 for (int j = 0; j < f->inputs.size(); j++) { 00087 inputs[i][j] = Var(f->inputs[j]->length(), f->inputs[j]->width()); 00088 } 00089 outputs[i] = f(inputs[i])[0]; 00090 f_paths[i] = propagationPath(inputs[i],outputs[i]); 00091 } 00092 } 00093 } 00094 00095 void UnfoldedSumOfVariable::declareOptions(OptionList& ol) 00096 { 00097 declareOption(ol, "f", &UnfoldedSumOfVariable::f, OptionBase::buildoption, 00098 " Func that is replicated for each element of the 'bag' taken from the VMat."); 00099 00100 declareOption(ol, "input_matrix", &UnfoldedSumOfVariable::input_matrix, OptionBase::buildoption, 00101 " Var that contains the data, with multiple consecutive rows forming one bag.\n" 00102 " The last row of a bag has a non-missing target value.\n"); 00103 00104 declareOption(ol, "bag_size", &UnfoldedSumOfVariable::bag_size, OptionBase::buildoption, 00105 " Var that gives the size of the bag (number of rows of input_matrix to consider).\n"); 00106 00107 declareOption(ol, "max_bag_size", &UnfoldedSumOfVariable::max_bag_size, OptionBase::buildoption, 00108 " maximum number of examples in a bag (more than that in input_matrix will trigger a run-time error).\n"); 00109 00110 inherited::declareOptions(ol); 00111 } 00112 00113 void UnfoldedSumOfVariable::recomputeSize(int& l, int& w) const 00114 { 00115 if (f && f->outputs.size()) { 00116 l = f->outputs[0]->length(); 00117 w = f->outputs[0]->width(); 00118 } else 00119 l = w = 0; 00120 } 00121 00123 extern void varDeepCopyField(Var& field, CopiesMap& copies); 00124 00125 void UnfoldedSumOfVariable::makeDeepCopyFromShallowCopy(map<const void*, void*>& copies) 00126 { 00127 NaryVariable::makeDeepCopyFromShallowCopy(copies); 00128 varDeepCopyField(input_matrix, copies); 00129 varDeepCopyField(bag_size, copies); 00130 deepCopyField(f, copies); 00131 deepCopyField(inputs, copies); 00132 deepCopyField(outputs, copies); 00133 deepCopyField(f_paths, copies); 00134 } 00135 00136 00137 void UnfoldedSumOfVariable::fprop() 00138 { 00139 value.clear(); 00140 int bagsize = (int)bag_size->valuedata[0]; 00141 if (bagsize>max_bag_size) 00142 PLERROR("UnfoldedSumOfVariable: bag size=%d > expected max. bag size(%d)", 00143 bagsize,max_bag_size); 00144 for (int i=0;i<bagsize;i++) 00145 { 00146 inputs[i] << input_matrix->matValue(i); 00147 f_paths[i].fprop(); 00148 value += outputs[i]->value; 00149 } 00150 } 00151 00152 00153 void UnfoldedSumOfVariable::bprop() 00154 { 00155 int bagsize = (int)bag_size->valuedata[0]; 00156 for (int i=0;i<bagsize;i++) 00157 { 00158 f_paths[i].clearGradient(); 00159 outputs[i]->gradient << gradient; 00160 f_paths[i].bprop(); 00161 } 00162 } 00163 00164 00165 void UnfoldedSumOfVariable::printInfo(bool print_gradient) 00166 { 00167 for (int i=0;i<max_bag_size;i++) 00168 f_paths[i].printInfo(print_gradient); 00169 cout << info() << " : " << getName() << "[" << (void*)this << "]" 00170 << "(input_matrix=" << (void*)input_matrix << ", bag_size=" 00171 << (void*)bag_size << ", "; 00172 for(int i=0; i<max_bag_size; i++) cout << (void*)outputs[i] << " "; 00173 cout << ") = " << value; 00174 if (print_gradient) cout << " gradient=" << gradient; 00175 cout << endl; 00176 } 00177 00178 00179 00180 } // end of namespace PLearn 00181 00182

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