PLearn 0.1
OnlineLearningModule.h
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // OnlineLearningModule.h
00004 //
00005 // Copyright (C) 2005 Yoshua Bengio
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    * $Id: .pyskeleton_header 544 2003-09-01 00:05:31Z plearner $
00037    ******************************************************* */
00038 
00039 // Authors: Yoshua Bengio
00040 
00044 #ifndef OnlineLearningModule_INC
00045 #define OnlineLearningModule_INC
00046 
00047 #include <plearn/base/Object.h>
00048 #include <plearn/math/PRandom.h>
00049 
00050 namespace PLearn {
00051 
00063 class OnlineLearningModule : public Object
00064 {
00065     typedef Object inherited;
00066 
00067 public:
00068     // BICK UGLY HACK to let modules know if we are performing training of testing
00069     static bool during_training;
00070 
00071     //#####  Public Build Options  ############################################
00072 
00074     int input_size;
00075 
00077     int output_size;
00078 
00079     string name;
00080 
00084     bool estimate_simpler_diag_hessian;
00085 
00092     PPath expdir;
00093 
00095     PP<PRandom> random_gen;
00096 
00098     bool use_fast_approximations;
00099 
00100     int verbosity;
00101 
00102 public:
00103     //#####  Public Member Fun ctions  #########################################
00104 
00109     OnlineLearningModule(const string& the_name = "",
00110                          bool call_build_ = false);
00111 
00114     virtual void fprop(const Vec& input, Vec& output) const;
00115 
00119     virtual void fprop(const Mat& inputs, Mat& outputs);
00120 
00133     virtual void fprop(const TVec<Mat*>& ports_value);
00134 
00135     virtual map<string,Mat> namedFprop(map<string,Mat>& inputs, TVec<string> wanted_outputs);
00136     virtual map<string,Mat> namedBpropAccUpdate(map<string,Mat>& values,
00137                                                 map<string,Mat>& gradients,
00138                                                 TVec<string> additional_input_gradients);
00139 
00151     virtual void bpropUpdate(const Vec& input, const Vec& output,
00152                              const Vec& output_gradient);
00153 
00157     virtual void bpropUpdate(const Mat& inputs, const Mat& outputs,
00158                              const Mat& output_gradients);
00159 
00181     virtual void bpropAccUpdate(const TVec<Mat*>& ports_value,
00182                                 const TVec<Mat*>& ports_gradient);
00183 
00189     void bpropUpdate(const TVec<Mat*>& ports_value,
00190                      const TVec<Mat*>& ports_gradient);
00191 
00198     virtual void bpropUpdate(const Vec& input, const Vec& output,
00199                              Vec& input_gradient, const Vec& output_gradient,
00200                              bool accumulate=false);
00203     virtual void bpropUpdate(const Mat& inputs, const Mat& outputs,
00204                              Mat& input_gradients, const Mat& output_gradients,
00205                              bool accumulate=false);
00206 
00216     virtual void bbpropUpdate(const Vec& input, const Vec& output,
00217                               const Vec& output_gradient,
00218                               const Vec& output_diag_hessian);
00219 
00223     virtual void bbpropUpdate(const Vec& input, const Vec& output,
00224                               Vec& input_gradient,
00225                               const Vec& output_gradient,
00226                               Vec& input_diag_hessian,
00227                               const Vec& output_diag_hessian,
00228                               bool accumulate=false);
00229 
00233     virtual void forget() = 0;
00234 
00235 
00239     virtual void finalize() {}
00240 
00241     // in case bpropUpdate does not do anything, make it known
00242     virtual bool bpropDoesNothing() { return false; }
00243 
00244     // Allows to change the learning rate, if the derived class has one and
00245     // allows to do so.
00246     virtual void setLearningRate(real dynamic_learning_rate);
00247 
00251     virtual const TVec<string>& getPorts();
00252 
00263     virtual const TMat<int>& getPortSizes();
00264 
00268     // Default implementation performs a simple linear search in getPorts().
00269     virtual int getPortIndex(const string& port);
00270 
00272     int getPortWidth(const string& port);
00273 
00275     int getPortLength(const string& port);
00276 
00278     int nPorts();
00279 
00281     string getPortName(int i);
00282 
00287     void checkProp(const TVec<Mat*>& ports_data);
00288 
00289     //#####  PLearn::Object Protocol  #########################################
00290 
00291     // Declares other standard object methods.
00292     PLEARN_DECLARE_ABSTRACT_OBJECT(OnlineLearningModule);
00293 
00294     // Simply calls inherited::build() then build_()
00295     virtual void build();
00296 
00298     virtual void makeDeepCopyFromShallowCopy(CopiesMap& copies);
00299 
00300 protected:
00301     //#####  Protected Member Functions  ######################################
00302 
00304     static void declareOptions(OptionList& ol);
00305 
00307     static void declareMethods(RemoteMethodMap& rmm);
00308 
00309 protected:
00310 
00312     TMat<int> port_sizes;
00313 
00314     // Also used in CostModule for instance
00315     mutable Vec tmp_input_gradient;
00316     mutable Mat tmpm_input_gradient;
00317     mutable Vec tmp_input_diag_hessian;
00318 
00319 
00320 private:
00321     //#####  Private Member Functions  ########################################
00322 
00324     void build_();
00325 
00326 private:
00327     //#####  Private Data Members  ############################################
00328 
00329     // The rest of the private stuff goes here
00330 };
00331 
00332 // Declares a few other classes and functions related to this class
00333 DECLARE_OBJECT_PTR(OnlineLearningModule);
00334 
00335 } // end of namespace PLearn
00336 
00337 #endif
00338 
00339 
00340 /*
00341   Local Variables:
00342   mode:c++
00343   c-basic-offset:4
00344   c-file-style:"stroustrup"
00345   c-file-offsets:((innamespace . 0)(inline-open . 0))
00346   indent-tabs-mode:nil
00347   fill-column:79
00348   End:
00349 */
00350 // 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