PLearn 0.1
Func.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 
00008 // Redistribution and use in source and binary forms, with or without
00009 // modification, are permitted provided that the following conditions are met:
00010 // 
00011 //  1. Redistributions of source code must retain the above copyright
00012 //     notice, this list of conditions and the following disclaimer.
00013 // 
00014 //  2. Redistributions in binary form must reproduce the above copyright
00015 //     notice, this list of conditions and the following disclaimer in the
00016 //     documentation and/or other materials provided with the distribution.
00017 // 
00018 //  3. The name of the authors may not be used to endorse or promote
00019 //     products derived from this software without specific prior written
00020 //     permission.
00021 // 
00022 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
00023 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00024 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
00025 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00026 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00027 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00028 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00029 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00030 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00031 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00032 // 
00033 // This file is part of the PLearn library. For more information on the PLearn
00034 // library, go to the PLearn Web site at www.plearn.org
00035 
00036 /* *******************************************************      
00037  * $Id: Func.h 9093 2008-06-03 21:05:24Z plearner $
00038  * AUTHORS: Pascal Vincent & Yoshua Bengio
00039  * This file is part of the PLearn library.
00040  ******************************************************* */
00041 
00042 #ifndef FUNCTION_INC
00043 #define FUNCTION_INC
00044 
00045 #include <plearn/base/Object.h>
00046 #include <plearn/base/general.h>
00047 #include <plearn/base/PP.h>
00048 #include <plearn/math/TMat.h>
00049 #include <plearn/vmat/VMat.h>
00050 #include "Variable.h"
00051 #include "VarArray.h"
00052 
00053 namespace PLearn {
00054 using namespace std;
00055 
00056 
00057 class Function;
00058 class Func: public PP<Function>
00059 {
00060 public:
00061     Func();
00062 
00063     Func(Function* f);
00064  
00065     Func(const VarArray& the_inputs, const VarArray& the_outputs);
00066 
00067     Func(const VarArray& the_inputs, const VarArray& parameters_to_optimize,const VarArray& the_outputs);
00068 
00069     Vec operator()(const Vec& input) const;
00070 
00071     real operator()(const Vec& input1, const Vec& input2) const;
00072 
00077     VarArray operator()(const VarArray& new_inputs) const;
00078 };
00079 
00080 class Function: public Object
00081 {
00082 public:
00083 
00084     // Build options:
00085     mutable VarArray inputs;
00086     mutable VarArray parameters;  
00087     mutable VarArray outputs;
00088 
00089     // Other variables
00090     int inputsize;
00091     int outputsize;
00092     mutable VarArray fproppath;
00093     VarArray bproppath;
00094     VarArray parentspath; 
00095 
00096     Func df; 
00097 
00098 private: 
00100     // (Please implement in .cc)
00101     void build_();
00102 
00103 protected: 
00105     // (Please implement in .cc)
00106     static void declareOptions(OptionList& ol);
00107 
00108 
00109 public:
00110     Function();
00111     Function(const VarArray& the_inputs, const VarArray& the_outputs);
00112     Function(const VarArray& the_inputs, const VarArray& parameters_to_optimize,const VarArray& the_outputs);
00113     //void bprop(VarArray& parameters_to_optimize);
00114 
00115 public:
00116 
00117     // ************************
00118     // **** Object methods ****
00119     // ************************
00120 
00122     virtual void build();
00123 
00125     virtual void makeDeepCopyFromShallowCopy(CopiesMap& copies);
00126 
00127     // Declares other standard object methods
00128     //  If your class is not instantiatable (it has pure virtual methods)
00129     // you should replace this by PLEARN_DECLARE_ABSTRACT_OBJECT_METHODS 
00130     typedef Object inherited;
00131     PLEARN_DECLARE_OBJECT(Function);
00132 
00133   
00134     // **************************
00135     // **** Function methods ****
00136     // **************************
00137 
00138     void fprop(const Vec& in, const Vec& out) const;
00139     void fprop(const Array<Vec>& in, const Array<Vec>& out) const;
00140 
00141     void sizefprop(const Vec& in, const Vec& out) const;
00142     void sizefprop(const Array<Vec>& in, const Array<Vec>& out) const;
00143 
00149     void fbprop(const Vec& in, const Vec& out, const Vec& input_gradient, const Vec& output_gradient);
00150     void fbprop(const Array<Vec>& in, const Array<Vec>& out, 
00151                 const Array<Vec>& input_gradient, const Array<Vec>& output_gradient);
00152     
00153     void sizefbprop(const Vec& in, const Vec& out, const Vec& input_gradient, const Vec& output_gradient);
00154     void sizefbprop(const Array<Vec>& in, const Array<Vec>& out, 
00155                 const Array<Vec>& input_gradient, const Array<Vec>& output_gradient);
00156     
00158     void fbbprop(const Vec& in, const Vec& out, const Vec& gradient, const Mat& hessian);
00160     void fbbpropAcc(const Vec& in, const Vec& out, const Vec& gradient, const Mat& hessian);
00161   
00162     void rfprop(const Vec& in, const Vec& out, const Vec& input_rvalue, const Vec& output_rvalue, bool do_fprop=true);
00163   
00164     void recomputeParents(); 
00165 
00171     Func differentiate();
00172 
00173     Vec operator()(const Vec& input) const;
00174     real operator()(const Vec& input1, const Vec& input2) const;
00175 
00184     VarArray operator()(const VarArray& new_inputs) const;
00185 
00187     void verifyGradient(const Vec& in, real step=0.01, int which_component=0);
00188 
00189     void verifyHessian(const Vec& in, real step=0.01);
00190 
00192     void verifyGradient(real minval, real maxval, real step=0.01,  int which_component=0);
00193 
00195     void verifyGradient(real step=0.01, int which_component=0);  
00196 
00201     void verifySymbolicGradient(const Vec& in);
00202 
00203     // This is developed to make sure that the code of rfprop is correct.
00204     // The value of H * r computed by rfprop or Hessian should be the same.
00205     void verifyrfprop(const Vec& in, real step=0.01);
00206 };
00207 
00208 DECLARE_OBJECT_PTR(Function);
00209 DECLARE_OBJECT_PP(Func, Function);
00210 
00211 Func operator/(Func f, real value);
00212 
00213 template <> void deepCopyField(Func& field, CopiesMap& copies);
00214 
00215 } // end of namespace PLearn
00216 
00217 #endif
00218 
00219 
00220 /*
00221   Local Variables:
00222   mode:c++
00223   c-basic-offset:4
00224   c-file-style:"stroustrup"
00225   c-file-offsets:((innamespace . 0)(inline-open . 0))
00226   indent-tabs-mode:nil
00227   fill-column:79
00228   End:
00229 */
00230 // 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