PLearn 0.1
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 9206 2008-07-03 17:21:25Z tihocan $
00040  * This file is part of the PLearn library.
00041  ******************************************************* */
00042 
00043 
00046 #ifndef VARARRAY_INC
00047 #define VARARRAY_INC
00048 
00049 #include "Variable.h"
00050 #include <plearn/base/general.h>
00051 #include <plearn/base/Array.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     VarArray(Variable* v1, Variable* v2, Variable* v3);
00074 
00075     void makeDeepCopyFromShallowCopy(CopiesMap& copies);
00076 
00077     operator Var()
00078     { 
00079         if(size()!=1)
00080             PLERROR("Cannot cast VarArray containing more than one variable to Var");
00081         return operator[](0);
00082     }
00083 
00084     VarArray& operator&=(const Var& v) { PLearn::operator&=(*this,v); return *this;}
00085     VarArray& operator&=(const VarArray& va) { PLearn::operator&=(*this,va); return *this; }
00086     VarArray operator&(const Var& v) const { return PLearn::operator&(*this,v); }
00087     VarArray operator&(const VarArray& va) const { return PLearn::operator&(*this,va); }
00088 
00089     int nelems() const;
00090     int sumOfLengths() const;
00091     int sumOfWidths() const;
00092     int maxWidth() const;
00093     int maxLength() const;
00094 
00096     VarArray nonNull() const;
00097     VarArray& subVarArray(int start,int len) const;
00098     void copyFrom(int start,int len,const VarArray& from);//copy Var of from in courant VarArray 
00099     void copyFrom(int start,const VarArray& from){copyFrom(start,from.size(),from);}
00100     void copyFrom(const Vec& datavec);
00101     void copyValuesFrom(const VarArray& from);
00102     void copyTo(const Vec& datavec) const;
00103     void accumulateTo(const Vec& datavec) const;
00104     void copyGradientFrom(const Vec& datavec);
00105     void copyGradientFrom(const Array<Vec>& datavec);
00106     void accumulateGradientFrom(const Vec& datavec);
00107     void copyGradientTo(const Vec& datavec);
00108     void copyGradientTo(const Array<Vec>& datavec);
00109     void accumulateGradientTo(const Vec& datavec);
00110 
00112     void copyTo(real* x, int n) const;
00113     void accumulateTo(real* x, int n) const;
00114     void copyFrom(const real* x, int n);
00115     void makeSharedValue(real* x, int n); 
00116     void makeSharedGradient(real* x, int n); 
00117 
00118     void makeSharedValue(PP<Storage<real> > storage, int offset_=0); 
00119     void makeSharedValue(Vec& v, int offset_=0);
00120     void makeSharedGradient(Vec& v, int offset_=0);
00121     void makeSharedGradient(PP<Storage<real> > storage, int offset_=0); 
00122     void copyGradientTo(real* x, int n);
00123     void copyGradientFrom(const real* x, int n);
00124     void accumulateGradientFrom(const real* x, int n);
00125     void accumulateGradientTo(real* x, int n);
00126     void copyMinValueTo(const Vec& minv);
00127     void copyMaxValueTo(const Vec& maxv);
00128     void copyMinValueTo(real* x, int n);
00129     void copyMaxValueTo(real* x, int n);
00130 
00131     void makeSharedRValue(Vec& v, int offset_=0);
00132     void makeSharedRValue(PP<Storage<real> > storage, int offset_=0);
00133     void copyRValueTo(const Vec& datavec);
00134     void copyRValueFrom(const Vec& datavec);
00135     void copyRValueTo(real* x, int n);
00136     void copyRValueFrom(const real* x, int n);
00137     void resizeRValue();
00138 
00139     void setMark() const;
00140     void clearMark() const;
00141     void markPath() const;
00142     void buildPath(VarArray& fpath) const;
00143     void setDontBpropHere(bool val);
00144 
00145     void fprop();
00146     void sizeprop();
00147     void sizefprop();
00148     void bprop();
00149     void bbprop();
00150     void rfprop();
00151     void fbprop();
00152     void sizefbprop();
00153     void fbbprop();
00154 
00155     void fillGradient(real value);
00156     void clearGradient();
00157     void clearDiagHessian();
00158     VarArray sources() const;
00159     VarArray ancestors() const;
00160     void unmarkAncestors() const;
00161 
00164     VarArray parents() const;
00165 
00167     void symbolicBprop(); 
00169     VarArray symbolicGradient(); 
00171     void clearSymbolicGradient(); 
00172 
00177     bool update(real step_size, Vec direction, real coeff = 1.0, real b = 0.0);
00178 
00184     bool update(Vec step_sizes, Vec direction, real coeff = 1.0, real b = 0.0);
00185   
00188     bool update(Vec step_sizes, Vec direction, Vec coeff);
00189 
00194     real maxUpdate(Vec direction);
00195 
00200     bool update(real step_size, bool clear=false);
00201 
00209     void updateWithWeightDecay(real step_size, real weight_decay, bool L1=false, bool clear=true);
00210 
00212     inline void updateAndClear();
00213 
00219     bool update(Vec new_value);
00220 
00221     void read(istream& in);
00222     void write(ostream& out) const;
00223     void printNames()const;
00224     void printInfo(bool print_gradient=false)
00225     {
00226         iterator array = data();
00227         for (int i=0;i<size();i++)
00228             if (!array[i].isNull())
00229                 array[i]->printInfo(print_gradient);
00230     }
00231 
00232     Var operator[](Var index);
00233     inline Var& operator[](int i) { return Array<Var>::operator[](i); }
00234     inline const Var& operator[](int i) const
00235     { return Array<Var>::operator[](i); }
00236 };
00237 
00238 inline void VarArray::updateAndClear()
00239 {
00240     iterator array = data();
00241     for(int i=0; i<size(); i++)
00242         array[i]->updateAndClear();
00243 }
00244 
00245 template <>
00246 inline void deepCopyField(VarArray& field, CopiesMap& copies)
00247 {
00248     field.makeDeepCopyFromShallowCopy(copies);
00249 }
00250 
00251 inline void operator<<(VarArray& ar, const Vec& datavec)
00252 { ar.copyFrom(datavec); }
00253 
00254 inline void operator>>(VarArray& ar, const Vec& datavec)
00255 { ar.copyTo(datavec); }
00256 
00257 void operator<<(VarArray& ar, const Array<Vec>& values);
00258 void operator>>(VarArray& ar, const Array<Vec>& values);
00259 
00267 VarArray propagationPath(const VarArray& inputs, const VarArray& outputs);
00268 
00271 //VarArray propagationPath(const VarArray& inputs, const VarArray& parameters_to_optimize,const VarArray& outputs);
00272 
00275 VarArray propagationPath(const VarArray& outputs);
00276 
00278 VarArray propagationPathToParentsOfPath(const VarArray& inputs, const VarArray& outputs);
00279 
00281 
00291 VarArray nonInputParentsOfPath(VarArray inputs, VarArray outputs);
00292 
00294 VarArray allSources(const VarArray& v);
00295 
00297 VarArray operator-(const VarArray& a, const VarArray& b);
00298 
00300 VarArray nonInputSources(const VarArray& inputs, const VarArray& outputs);
00301 
00302 void printInfo(VarArray inputs, const Var& output, bool show_gradients=true);
00303 
00304 void printInfo(VarArray& a);
00305 
00307 inline VarArray operator&(Var v1, Var v2)
00308 { return VarArray(v1,v2); }
00309 
00310 DECLARE_TYPE_TRAITS(VarArray);
00311 
00312 inline PStream &operator>>(PStream &in, VarArray &o)
00313 { in >> static_cast<Array<Var> &>(o); return in; }
00314  
00315 inline PStream &operator<<(PStream &out, const VarArray &o)
00316 { out << static_cast<const Array<Var> &>(o); return out; }
00317 
00318 
00319 } // end of namespace PLearn
00320 
00321 #endif
00322 
00323 
00324 /*
00325   Local Variables:
00326   mode:c++
00327   c-basic-offset:4
00328   c-file-style:"stroustrup"
00329   c-file-offsets:((innamespace . 0)(inline-open . 0))
00330   indent-tabs-mode:nil
00331   fill-column:79
00332   End:
00333 */
00334 // 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