PLearn 0.1
TreeDBNModule.h
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // TreeDBNModule.h
00004 //
00005 // Copyright (C) 2007 Vytenis Sakenas
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 // Authors: Vytenis Sakenas
00036 
00040 #ifndef TreeDBNModule_INC
00041 #define TreeDBNModule_INC
00042 
00043 #include <plearn_learners/online/OnlineLearningModule.h>
00044 #include <plearn_learners/online/RBMModule.h>
00045 #include <plearn/sys/Profiler.h>
00046 
00047 namespace PLearn {
00048 
00049 
00050 class TreeDBNModule : public OnlineLearningModule
00051 {
00052     typedef OnlineLearningModule inherited;
00053 
00054 public:
00055     //#####  Public Build Options  ############################################
00056 
00058         TVec <PP<RBMModule> > modules;
00059         
00061         int n_parents_per_node;
00062         
00065         int n_shared_parents;
00066         
00068         real gradient_multiplier;
00069         
00071         bool propagate_gradient;
00072 
00074         bool propagate_energy_gradient;
00075         
00077         bool propagate_full_gradient;
00078         
00081         TVec< pair<string, string > > ports;
00082         
00083 public:
00084     //#####  Public Member Functions  #########################################
00085 
00087     // ### Make sure the implementation in the .cc
00088     // ### initializes all fields to reasonable default values.
00089     TreeDBNModule();
00090 
00091     // Your other public member functions go here
00092 
00105     void fprop(const TVec<Mat*>& ports_value);
00106 
00128     virtual void bpropAccUpdate(const TVec<Mat*>& ports_value,
00129                                 const TVec<Mat*>& ports_gradient);
00130 
00131     /* Optional
00132 
00135     virtual void fprop(const Vec& input, Vec& output) const;
00136 
00139     virtual void fprop(const Mat& inputs, Mat& outputs);
00140     */
00141 
00142     /* Optional
00153     virtual void bpropUpdate(const Vec& input, const Vec& output,
00154                              Vec& input_gradient,
00155                              const Vec& output_gradient,
00156                              bool accumulate=false);
00157 
00161     virtual void bpropUpdate(const Mat& inputs, const Mat& outputs,
00162                              Mat& input_gradients,
00163                              const Mat& output_gradients,
00164                              bool accumulate=false);
00165     */
00166 
00167     /* Optional
00170        A DEFAULT IMPLEMENTATION IS PROVIDED IN THE SUPER-CLASS, WHICH
00171        JUST CALLS
00172             bpropUpdate(input, output, input_gradient, output_gradient)
00173        AND IGNORES INPUT GRADIENT.
00175     virtual void bpropUpdate(const Vec& input, const Vec& output,
00176                              const Vec& output_gradient);
00177 
00181     virtual void bpropUpdate(const Mat& inputs, const Mat& outputs,
00182                              const Mat& output_gradients);
00183     */
00184 
00185     /* Optional
00186        N.B. A DEFAULT IMPLEMENTATION IS PROVIDED IN THE SUPER-CLASS, WHICH
00187        RAISES A PLERROR.
00192     virtual void bbpropUpdate(const Vec& input, const Vec& output,
00193                               Vec& input_gradient,
00194                               const Vec& output_gradient,
00195                               Vec& input_diag_hessian,
00196                               const Vec& output_diag_hessian,
00197                               bool accumulate=false);
00198     */
00199 
00200     /* Optional
00201        N.B. A DEFAULT IMPLEMENTATION IS PROVIDED IN THE SUPER-CLASS,
00202        WHICH JUST CALLS
00203             bbpropUpdate(input, output, input_gradient, output_gradient,
00204                          out_hess, in_hess)
00205        AND IGNORES INPUT HESSIAN AND INPUT GRADIENT.
00207     virtual void bbpropUpdate(const Vec& input, const Vec& output,
00208                               const Vec& output_gradient,
00209                               const Vec& output_diag_hessian);
00210     */
00211 
00212 
00216     virtual void forget();
00217 
00218 
00219     /* Optional
00220        THE DEFAULT IMPLEMENTATION PROVIDED IN THE SUPER-CLASS DOES NOT
00221        DO ANYTHING.
00225     virtual void finalize();
00226     */
00227 
00228     /* Optional
00229        THE DEFAULT IMPLEMENTATION PROVIDED IN THE SUPER-CLASS RETURNS false
00231     virtual bool bpropDoesNothing();
00232     */
00233 
00234     /* Optional
00235        Default implementation prints a warning and does nothing
00238     virtual void setLearningRate(real dynamic_learning_rate);
00239     */
00240 
00244     virtual const TVec<string>& getPorts();
00245         
00248         void initSampling(int gibbsTop);
00249         
00252         Vec sample(int nGibbs);
00253 
00255         void clearCache();
00256 
00257 
00258     /* Optional
00269     virtual const TMat<int>& getPortSizes();
00270 
00274     //  ### Default implementation performs a simple linear search in
00275     //  ### getPorts().
00276     virtual int getPortIndex(const string& port);
00277     */
00278 
00279     //#####  PLearn::Object Protocol  #########################################
00280 
00281     // Declares other standard object methods.
00282     // ### If your class is not instantiatable (it has pure virtual methods)
00283     // ### you should replace this by PLEARN_DECLARE_ABSTRACT_OBJECT
00284     PLEARN_DECLARE_OBJECT(TreeDBNModule);
00285 
00286     // Simply calls inherited::build() then build_()
00287     virtual void build();
00288 
00290     virtual void makeDeepCopyFromShallowCopy(CopiesMap& copies);
00291 
00292 
00293 protected:
00294     //#####  Protected Member Functions  ######################################
00295 
00297     static void declareOptions(OptionList& ol);
00298         
00299         static void declareMethods(RemoteMethodMap& rmm);
00300         
00301         // Number of layers.
00302         int n_layers;
00303         
00304         TVec<string> port_names;
00305         TVec<int> port_rbms;            // Index of rbm that writes to this port, or -1 if none does.
00306         TVec<int> port_index;           // Index of rbms port that writes to this port.
00307         
00308         // Number of rbms in layer.
00309         TVec<int> layer_sizes;
00310         
00311         // Steps after which current rbm output will be reused.
00312         // Same as count of lowest layer inputs that this rbm depends on.
00313         TVec <int> step_size;
00314         
00315         // Last input provided to the module. Is keeped for determining if input is shifted
00316         // during subsequent fprop calls.
00317         Vec last_full_input;
00318         
00319         // Fprop and bprop data that is used when calling individual
00320         // rbm methods
00321         TVec < TVec<Mat*> > fprop_data;
00322         
00323         TVec < TVec<Mat*> > bprop_data;
00324         
00325         // Same as above but saved for reuse between subsequent
00326         // calls to fprop
00327         TVec < TVec<Mat*> > fprop_data_cache;
00328         
00329         TVec < TVec<Mat*> > bprop_data_cache;
00330         
00331         int time;               // Current pseudo-time of module
00332 
00333         TVec <int> mbi_time;    // Pseudo-time for each sample in the minibatch.
00334         
00335         // In ..[i][j] Keeps length of fprop_data for layer i rbm after processing j samples in the minibatch
00336         TMat <int> mod_batch_length;    
00337         
00338         // Storage for actual matrices
00339         TVec <Mat> mats;
00340         TVec <Mat> cache_mats;
00341 
00342 private:
00343     //#####  Private Member Functions  ########################################
00344 
00346     void build_();
00347         
00349         bool check_shift(Vec &a, Vec &b, int k);
00350         
00352         int hash(int t, int k, int i);
00353 
00354         void appendPort(string name, int rbm_index, string port_name, int port_width);
00355         
00357         Mat* createMatrix(int length, int width, TVec <Mat> &mats);
00358 
00360         void updateCache();
00361 
00363         void full_fprop(const TVec<Mat*>& ports_value);
00364         
00365 private:
00366     //#####  Private Data Members  ############################################
00367 
00368     // The rest of the private stuff goes here
00369 };
00370 
00371 // Declares a few other classes and functions related to this class
00372 DECLARE_OBJECT_PTR(TreeDBNModule);
00373 
00374 } // end of namespace PLearn
00375 
00376 #endif
00377 
00378 
00379 /*
00380   Local Variables:
00381   mode:c++
00382   c-basic-offset:4
00383   c-file-style:"stroustrup"
00384   c-file-offsets:((innamespace . 0)(inline-open . 0))
00385   indent-tabs-mode:nil
00386   fill-column:79
00387   End:
00388 */
00389 // 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