PLearn 0.1
ConcatOfVariable.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: ConcatOfVariable.cc 6861 2007-04-09 19:04:15Z saintmlx $
00040  * This file is part of the PLearn library.
00041  ******************************************************* */
00042 
00043 #include "ConcatOfVariable.h"
00044 
00045 namespace PLearn {
00046 using namespace std;
00047 
00048 
00049 
00051 /* concatenates the results of each operation in the loop into the resulting variable */
00052 
00053 PLEARN_IMPLEMENT_OBJECT(ConcatOfVariable, "Concatenates the results of each operation in the loop into the resulting variable", "NO HELP");
00054 
00055 ConcatOfVariable::ConcatOfVariable(VMat the_distr, Func the_f)
00056     : inherited(nonInputParentsOfPath(the_f->inputs, the_f->outputs), 
00057                 the_f->outputs[0]->length() * the_distr->length(), 
00058                 the_f->outputs[0]->width()),
00059       distr(the_distr), f(the_f)
00060 {
00061     build_();
00062 }
00063 
00064 /* Old constructor
00065    ConcatOfVariable::ConcatOfVariable(Variable* the_output, const VarArray& the_inputs, VMat the_distr, const VarArray& the_parameters)
00066    :NaryVariable(nonInputParentsOfPath(the_inputs,the_output), the_output->length()*the_distr->length(), the_output->width()), inputs(the_inputs), distr(the_distr), output(the_output), parameters(the_parameters)
00067    {
00068    full_fproppath = propagationPath(inputs&parameters, output);
00069    fproppath = propagationPath(inputs, output);
00070    bproppath = propagationPath(parameters, output);
00071    }
00072 */
00073 
00074 
00075 void
00076 ConcatOfVariable::declareOptions(OptionList &ol)
00077 {
00078     declareOption(ol, "distr", &ConcatOfVariable::distr, OptionBase::buildoption, "");
00079     declareOption(ol, "f", &ConcatOfVariable::f, OptionBase::buildoption, "");
00080     inherited::declareOptions(ol);
00081 }
00082 
00083 void
00084 ConcatOfVariable::build()
00085 {
00086     inherited::build();
00087     build_();
00088 }
00089 
00090 void
00091 ConcatOfVariable::build_()
00092 {
00093     if (distr && f) {
00094         if(f->outputs.size()!=1)
00095             PLERROR("In ConcatOfVariable constructor: function must have a single output variable");
00096 
00097         input_value.resize(distr->width());
00098         input_gradient.resize(distr->width());
00099     }
00100 }
00101 
00102 
00103 void ConcatOfVariable::recomputeSize(int& l, int& w) const
00104 {
00105     if (f && distr) {
00106         l = f->outputs[0]->length() * distr->length();
00107         w = f->outputs[0]->width();
00108     } else
00109         l = w = 0;
00110 }
00111 
00112 
00113 void ConcatOfVariable::makeDeepCopyFromShallowCopy(CopiesMap& copies)
00114 {
00115     inherited::makeDeepCopyFromShallowCopy(copies);
00116     deepCopyField(distr, copies);
00117     deepCopyField(f, copies);
00118     deepCopyField(input_value, copies);
00119     deepCopyField(input_gradient, copies);
00120 }
00121 
00122 
00123 
00124 void ConcatOfVariable::fprop()
00125 {
00126     f->recomputeParents();
00127 
00128     int pos = 0;
00129     int singleoutputsize = f->outputs[0]->nelems();
00130     for(int i=0; i<distr->length(); i++, pos+=singleoutputsize)
00131     {
00132         distr->getRow(i,input_value);
00133         f->fprop(input_value, value.subVec(pos,singleoutputsize));
00134     }
00135 }
00136 
00137 
00138 void ConcatOfVariable::bprop()
00139 {
00140     fbprop();
00141 }
00142 
00143 
00144 void ConcatOfVariable::fbprop()
00145 {
00146     f->recomputeParents();
00147 
00148     int pos = 0;
00149     int singleoutputsize = f->outputs[0]->nelems();
00150     for(int i=0; i<distr->length(); i++, pos+=singleoutputsize)
00151     {
00152         distr->getRow(i, input_value);
00153         f->fbprop(input_value, value.subVec(pos,singleoutputsize), 
00154                   input_gradient, gradient.subVec(pos,singleoutputsize));
00155         // We don't use the computed input_gradients, as the input is a dummy variable.
00156         // The gradients on other (non-input) variables which we are interested in, 
00157         // have been accumulated by the call as a side effect.
00158     }
00159 }
00160 
00161 
00162 
00163 } // end of namespace PLearn
00164 
00165 
00166 /*
00167   Local Variables:
00168   mode:c++
00169   c-basic-offset:4
00170   c-file-style:"stroustrup"
00171   c-file-offsets:((innamespace . 0)(inline-open . 0))
00172   indent-tabs-mode:nil
00173   fill-column:79
00174   End:
00175 */
00176 // 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