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

VarArray.h

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 and University of Montreal 00006 // 00007 // Redistribution and use in source and binary forms, with or without 00008 // modification, are permitted provided that the following conditions are met: 00009 // 00010 // 1. Redistributions of source code must retain the above copyright 00011 // notice, this list of conditions and the following disclaimer. 00012 // 00013 // 2. Redistributions in binary form must reproduce the above copyright 00014 // notice, this list of conditions and the following disclaimer in the 00015 // documentation and/or other materials provided with the distribution. 00016 // 00017 // 3. The name of the authors may not be used to endorse or promote 00018 // products derived from this software without specific prior written 00019 // permission. 00020 // 00021 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 00022 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00023 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 00024 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00025 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00026 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00027 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00028 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00029 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00030 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00031 // 00032 // This file is part of the PLearn library. For more information on the PLearn 00033 // library, go to the PLearn Web site at www.plearn.org 00034 00035 00036 00037 00038 /* ******************************************************* 00039 * $Id: VarArray.h,v 1.17 2004/07/21 16:30:54 chrish42 Exp $ 00040 * This file is part of the PLearn library. 00041 ******************************************************* */ 00042 00043 00046 #ifndef VARARRAY_INC 00047 #define VARARRAY_INC 00048 00049 #include <plearn/base/general.h> 00050 #include <plearn/base/Array.h> 00051 #include "Variable.h" 00052 00053 namespace PLearn { 00054 using namespace std; 00055 00056 class VarArray: public Array<Var> 00057 { 00058 public: 00059 00060 typedef Var* iterator; 00061 00062 VarArray(); 00063 explicit VarArray(int n,int n_extra=10); 00064 VarArray(int n,int initial_length, int n_extra); 00065 VarArray(int n,int initial_length, int initial_width, int n_extra); 00066 VarArray(const Array<Var>& va): Array<Var>(va) {} 00067 VarArray(Array<Var>& va): Array<Var>(va) {} 00068 VarArray(const VarArray& va): Array<Var>(va) {} 00069 VarArray(const Var& v); 00070 VarArray(const Var& v1, const Var& v2); 00071 VarArray(Variable* v); 00072 VarArray(Variable* v1, Variable* v2); 00073 00074 void makeDeepCopyFromShallowCopy(map<const void*, void*>& copies); 00075 00076 operator Var() 00077 { 00078 if(size()!=1) 00079 PLERROR("Cannot cast VarArray containing more than one variable to Var"); 00080 return operator[](0); 00081 } 00082 00083 VarArray& operator&=(const Var& v) { PLearn::operator&=(*this,v); return *this;} 00084 VarArray& operator&=(const VarArray& va) { PLearn::operator&=(*this,va); return *this; } 00085 VarArray operator&(const Var& v) const { return PLearn::operator&(*this,v); } 00086 VarArray operator&(const VarArray& va) const { return PLearn::operator&(*this,va); } 00087 00088 int nelems() const; 00089 int sumOfLengths() const; 00090 int sumOfWidths() const; 00091 int maxWidth() const; 00092 int maxLength() const; 00093 00095 VarArray nonNull() const; 00096 VarArray& subVarArray(int start,int len) const; 00097 void copyFrom(int start,int len,const VarArray& from);//copy Var of from in courant VarArray 00098 void copyFrom(int start,const VarArray& from){copyFrom(start,from.size(),from);} 00099 void copyFrom(const Vec& datavec); 00100 void copyValuesFrom(const VarArray& from); 00101 void copyTo(const Vec& datavec) const; 00102 void accumulateTo(const Vec& datavec) const; 00103 void copyGradientFrom(const Vec& datavec); 00104 void copyGradientFrom(const Array<Vec>& datavec); 00105 void accumulateGradientFrom(const Vec& datavec); 00106 void copyGradientTo(const Vec& datavec); 00107 void copyGradientTo(const Array<Vec>& datavec); 00108 void accumulateGradientTo(const Vec& datavec); 00109 00111 void copyTo(real* x, int n) const; 00112 void accumulateTo(real* x, int n) const; 00113 void copyFrom(const real* x, int n); 00114 void makeSharedValue(real* x, int n); 00115 void makeSharedGradient(real* x, int n); 00116 00117 void makeSharedValue(PP<Storage<real> > storage, int offset_=0); 00118 void makeSharedValue(Vec& v, int offset_=0); 00119 void makeSharedGradient(Vec& v, int offset_=0); 00120 void makeSharedGradient(PP<Storage<real> > storage, int offset_=0); 00121 void copyGradientTo(real* x, int n); 00122 void copyGradientFrom(const real* x, int n); 00123 void accumulateGradientFrom(const real* x, int n); 00124 void accumulateGradientTo(real* x, int n); 00125 void copyMinValueTo(const Vec& minv); 00126 void copyMaxValueTo(const Vec& maxv); 00127 void copyMinValueTo(real* x, int n); 00128 void copyMaxValueTo(real* x, int n); 00129 00130 void makeSharedRValue(Vec& v, int offset_=0); 00131 void makeSharedRValue(PP<Storage<real> > storage, int offset_=0); 00132 void copyRValueTo(const Vec& datavec); 00133 void copyRValueFrom(const Vec& datavec); 00134 void copyRValueTo(real* x, int n); 00135 void copyRValueFrom(const real* x, int n); 00136 void resizeRValue(); 00137 00138 void setMark() const; 00139 void clearMark() const; 00140 void markPath() const; 00141 void buildPath(VarArray& fpath) const; 00142 void setDontBpropHere(bool val); 00143 00144 void fprop(); 00145 void sizeprop(); 00146 void sizefprop(); 00147 void bprop(); 00148 void bbprop(); 00149 void rfprop(); 00150 void fbprop(); 00151 void fbbprop(); 00152 00153 void fillGradient(real value); 00154 void clearGradient(); 00155 void clearDiagHessian(); 00156 VarArray sources() const; 00157 VarArray ancestors() const; 00158 void unmarkAncestors() const; 00159 00162 VarArray parents() const; 00163 00165 void symbolicBprop(); 00167 VarArray symbolicGradient(); 00169 void clearSymbolicGradient(); 00170 00175 bool update(real step_size, Vec direction, real coeff = 1.0, real b = 0.0); 00176 00182 bool update(Vec step_sizes, Vec direction, real coeff = 1.0, real b = 0.0); 00183 00186 bool update(Vec step_sizes, Vec direction, Vec coeff); 00187 00192 real maxUpdate(Vec direction); 00193 00198 bool update(real step_size); 00199 00201 inline void updateAndClear(); 00202 00208 bool update(Vec new_value); 00209 00210 void read(istream& in); 00211 void write(ostream& out) const; 00212 void printNames()const; 00213 void printInfo(bool print_gradient=false) 00214 { 00215 iterator array = data(); 00216 for (int i=0;i<size();i++) 00217 if (!array[i].isNull()) 00218 array[i]->printInfo(print_gradient); 00219 } 00220 00221 Var operator[](Var index); 00222 inline Var& operator[](int i) { return Array<Var>::operator[](i); } 00223 inline const Var& operator[](int i) const 00224 { return Array<Var>::operator[](i); } 00225 }; 00226 00227 inline void VarArray::updateAndClear() 00228 { 00229 iterator array = data(); 00230 for(int i=0; i<size(); i++) 00231 array[i]->updateAndClear(); 00232 } 00233 00234 template <> 00235 inline void deepCopyField(VarArray& field, CopiesMap& copies) 00236 { 00237 field.makeDeepCopyFromShallowCopy(copies); 00238 } 00239 00240 inline void operator<<(VarArray& ar, const Vec& datavec) 00241 { ar.copyFrom(datavec); } 00242 00243 inline void operator>>(VarArray& ar, const Vec& datavec) 00244 { ar.copyTo(datavec); } 00245 00246 void operator<<(VarArray& ar, const Array<Vec>& values); 00247 void operator>>(VarArray& ar, const Array<Vec>& values); 00248 00256 VarArray propagationPath(const VarArray& inputs, const VarArray& outputs); 00257 00260 //VarArray propagationPath(const VarArray& inputs, const VarArray& parameters_to_optimize,const VarArray& outputs); 00261 00264 VarArray propagationPath(const VarArray& outputs); 00265 00267 VarArray propagationPathToParentsOfPath(const VarArray& inputs, const VarArray& outputs); 00268 00270 00280 VarArray nonInputParentsOfPath(VarArray inputs, VarArray outputs); 00281 00283 VarArray allSources(const VarArray& v); 00284 00286 VarArray operator-(const VarArray& a, const VarArray& b); 00287 00289 VarArray nonInputSources(const VarArray& inputs, const VarArray& outputs); 00290 00291 void printInfo(VarArray inputs, const Var& output, bool show_gradients=true); 00292 00293 void printInfo(VarArray& a); 00294 00296 inline VarArray operator&(Var v1, Var v2) 00297 { return VarArray(v1,v2); } 00298 00299 DECLARE_TYPE_TRAITS(VarArray); 00300 00301 inline PStream &operator>>(PStream &in, VarArray &o) 00302 { in >> static_cast<Array<Var> &>(o); return in; } 00303 00304 inline PStream &operator<<(PStream &out, const VarArray &o) 00305 { out << static_cast<const Array<Var> &>(o); return out; } 00306 00307 00308 } // end of namespace PLearn 00309 00310 #endif 00311 00312 00313

Generated on Tue Aug 17 16:10:02 2004 for PLearn by doxygen 1.3.7