PLearn 0.1
MatrixSumOfVariable.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: MatrixSumOfVariable.cc 6861 2007-04-09 19:04:15Z saintmlx $
00040  * This file is part of the PLearn library.
00041  ******************************************************* */
00042 
00043 #include "MatrixSumOfVariable.h"
00044 
00045 namespace PLearn {
00046 using namespace std;
00047 
00048 
00051 PLEARN_IMPLEMENT_OBJECT(MatrixSumOfVariable,
00052                         "ONE LINE DESCR",
00053                         "NO HELP");
00054 
00055 MatrixSumOfVariable::MatrixSumOfVariable(VMat the_distr, Func the_f, int the_nsamples, int the_input_size)
00056     : inherited(nonInputParentsOfPath(the_f->inputs,the_f->outputs), the_f->outputs[0]->length(), the_f->outputs[0]->width()),
00057       distr(the_distr), f(the_f), nsamples(the_nsamples), input_size(the_input_size)
00058     //, curpos(0), input_value(the_distr->width()*nsamples), input_gradient(the_distr->width()*nsamples), output_value(the_f->outputs[0]->size())
00059 {
00060     build_();
00061 }
00062 
00063 void
00064 MatrixSumOfVariable::build()
00065 {
00066     inherited::build();
00067     build_();
00068 }
00069 
00070 void
00071 MatrixSumOfVariable::build_()
00072 {
00073     curpos = 0;
00074     if (f && distr) {
00075         if(f->outputs.size()!=1)
00076             PLERROR("In MatrixSumOfVariable: 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)");
00077 
00078         input_value.resize(distr->width() * nsamples);
00079         input_gradient.resize(distr->width() * nsamples);
00080         output_value.resize(f->outputs[0]->size());
00081 
00082         if(nsamples == -1)
00083             nsamples = distr->length();
00084         f->inputs.setDontBpropHere(true);
00085     }
00086 }
00087 
00088 void
00089 MatrixSumOfVariable::declareOptions(OptionList &ol)
00090 {
00091     declareOption(ol, "distr", &MatrixSumOfVariable::distr, OptionBase::buildoption, "");
00092     declareOption(ol, "f", &MatrixSumOfVariable::f, OptionBase::buildoption, "");
00093     declareOption(ol, "nsamples", &MatrixSumOfVariable::nsamples, OptionBase::buildoption, "");
00094     declareOption(ol, "input_size", &MatrixSumOfVariable::input_size, OptionBase::buildoption, "");
00095     inherited::declareOptions(ol);
00096 }
00097 
00098 void MatrixSumOfVariable::recomputeSize(int& l, int& w) const
00099 {
00100     if (f) {
00101         l = f->outputs[0]->length();
00102         w = f->outputs[0]->width();
00103     } else
00104         l = w = 0;
00105 }
00106 
00107 
00108 void MatrixSumOfVariable::makeDeepCopyFromShallowCopy(CopiesMap& copies)
00109 {
00110     inherited::makeDeepCopyFromShallowCopy(copies);
00111     deepCopyField(distr, copies);
00112     deepCopyField(f, copies);
00113 }
00114 
00115 
00116 void MatrixSumOfVariable::fprop()
00117 {
00118     Vec oneInput_value(distr->width());
00119     f->recomputeParents();
00120 
00121     int inputpos=0;
00122     int targetpos=nsamples*input_size;
00123     for (int i=0; i<nsamples; i++)
00124     {
00125         distr->getRow(curpos, oneInput_value);
00126         for (int j=0; j<input_size; j++,inputpos++)
00127             input_value[inputpos]=oneInput_value[j];
00128         for (int j=input_size; j<distr.width(); j++,targetpos++)
00129             input_value[targetpos] = oneInput_value[j];
00130         if(++curpos==distr.length())
00131             curpos=0;
00132     }
00133     f->fprop(input_value, value);
00134 }
00135 
00136 
00137 void MatrixSumOfVariable::bprop()
00138 { fbprop(); }
00139 
00140 
00141 void MatrixSumOfVariable::fbprop()
00142 {
00143     Vec oneInput_value(distr->width());
00144     f->recomputeParents();
00145   
00146     int inputpos=0;
00147     int targetpos=nsamples*input_size;
00148     for (int i=0; i<nsamples; i++)
00149     {
00150         distr->getRow(curpos, oneInput_value);
00151         for (int j=0; j<input_size; j++,inputpos++)
00152             input_value[inputpos]=oneInput_value[j];
00153         for (int j=input_size; j<distr.width(); j++,targetpos++)
00154             input_value[targetpos] = oneInput_value[j];
00155         if(++curpos==distr.length())
00156             curpos=0;
00157     }
00158     f->fbprop(input_value, value, input_gradient, gradient);
00159 }
00160 
00161 
00162 void MatrixSumOfVariable::symbolicBprop()
00163 {
00164     PLERROR("MatrixSumOfVariable::symbolicBprop not implemented.");
00165 }
00166 
00167 
00168 void MatrixSumOfVariable::rfprop()
00169 {
00170     PLERROR("MatrixSumOfVariable::rfprop not implemented.");
00171 }
00172 
00173 
00174 void MatrixSumOfVariable::printInfo(bool print_gradient)
00175 {
00176     PLERROR("MatrixSumOfVariable::printInfo not implemented.");
00177     Vec input_value(distr->width());
00178     Vec input_gradient(distr->width());
00179     Vec output_value(nelems());
00180 
00181     f->recomputeParents();
00182     value.clear();
00183 
00184     for(int i=0; i<nsamples; i++)
00185     {
00186         distr->getRow(curpos++,input_value);
00187         if (print_gradient)
00188             f->fbprop(input_value, output_value, input_gradient, gradient);
00189         else
00190             f->fprop(input_value, output_value);
00191         value += output_value;
00192         if(++curpos>=distr->length())
00193             curpos = 0;
00194         f->fproppath.printInfo(print_gradient);
00195     }
00196     cout << info() << " : " << getName() << " = " << value;
00197     if (print_gradient) cout << " gradient=" << gradient;
00198     cout << endl; 
00199 }
00200 
00201 
00202 
00203 } // end of namespace PLearn
00204 
00205 
00206 /*
00207   Local Variables:
00208   mode:c++
00209   c-basic-offset:4
00210   c-file-style:"stroustrup"
00211   c-file-offsets:((innamespace . 0)(inline-open . 0))
00212   indent-tabs-mode:nil
00213   fill-column:79
00214   End:
00215 */
00216 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines