PLearn 0.1
|
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: ConcatColumnsVariable.cc 8956 2008-05-08 19:19:03Z tihocan $ 00040 * This file is part of the PLearn library. 00041 ******************************************************* */ 00042 00043 #include "ConcatColumnsVariable.h" 00044 #include "SubMatVariable.h" 00045 00046 namespace PLearn { 00047 using namespace std; 00048 00049 00052 PLEARN_IMPLEMENT_OBJECT( 00053 ConcatColumnsVariable, 00054 "Concatenation of the columns of several variables", 00055 "" 00056 ); 00057 00059 // ConcatColumnsVariable // 00061 ConcatColumnsVariable::ConcatColumnsVariable(const VarArray& vararray, 00062 bool call_build_): 00063 inherited(vararray.nonNull(), vararray.maxLength(), vararray.sumOfWidths(), 00064 call_build_) 00065 { 00066 if (call_build_) 00067 build_(); 00068 } 00069 00071 // build // 00073 void ConcatColumnsVariable::build() 00074 { 00075 inherited::build(); 00076 build_(); 00077 } 00078 00080 // build_ // 00082 void ConcatColumnsVariable::build_() 00083 { 00084 if (varray->length()) { 00085 int l = varray[0]->length(); 00086 for (int i = 1; i < varray.size(); i++) 00087 if (l != varray[i]->length()) 00088 PLERROR("ConcatColumnsVariable: all non-null variables must have the same length (%d != %d)", l, varray[i]->length()); 00089 } 00090 } 00091 00092 void ConcatColumnsVariable::recomputeSize(int& l, int& w) const 00093 { 00094 if (varray) { 00095 l = varray.maxLength(); 00096 w = varray.sumOfWidths(); 00097 } else 00098 l = w = 0; 00099 } 00100 00101 void ConcatColumnsVariable::fprop() 00102 { 00103 int n_rows = matValue.length(); 00104 int m_start = 0; 00105 int mod = matValue.mod(); 00106 for (int m=0;m<varray.size();m++) 00107 { 00108 real* mp = varray[m]->valuedata; 00109 int n_cols = varray[m]->matValue.width(); 00110 real* p = &valuedata[m_start]; 00111 for (int i=0;i<n_rows;i++,p+=mod) 00112 for (int j=0;j<n_cols;j++,mp++) 00113 p[j] = *mp; 00114 m_start+=n_cols; 00115 } 00116 } 00117 00118 00119 void ConcatColumnsVariable::bprop() 00120 { 00121 int n_rows = matValue.length(); 00122 int m_start = 0; 00123 int mod = matValue.mod(); 00124 for (int m=0;m<varray.size();m++) 00125 { 00126 real* mp = varray[m]->gradientdata; 00127 int n_cols = varray[m]->matGradient.width(); 00128 real* p = &gradientdata[m_start]; 00129 for (int i=0;i<n_rows;i++,p+=mod) 00130 for (int j=0;j<n_cols;j++,mp++) 00131 *mp += p[j]; 00132 m_start+=n_cols; 00133 } 00134 } 00135 00136 00137 void ConcatColumnsVariable::symbolicBprop() 00138 { 00139 int k=0; 00140 for (int n=0; n<varray.size(); n++) { 00141 Var vn = varray[n]; 00142 vn->accg(new SubMatVariable(g, 0, k, length(), vn->width())); 00143 k += vn->width(); 00144 } 00145 } 00146 00147 00148 00149 } // end of namespace PLearn 00150 00151 00152 /* 00153 Local Variables: 00154 mode:c++ 00155 c-basic-offset:4 00156 c-file-style:"stroustrup" 00157 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00158 indent-tabs-mode:nil 00159 fill-column:79 00160 End: 00161 */ 00162 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :