PLearn 0.1
RBMWoodsLayer.h
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // RBMWoodsLayer.h
00004 //
00005 // Copyright (C) 2008 Hugo Larochelle
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: Hugo Larochelle
00036 
00040 #ifndef RBMWoodsLayer_INC
00041 #define RBMWoodsLayer_INC
00042 
00043 #include "RBMLayer.h"
00044 
00045 namespace PLearn {
00046 using namespace std;
00047 
00052 class RBMWoodsLayer: public RBMLayer
00053 {
00054     typedef RBMLayer inherited;
00055 
00056 public:
00057     //#####  Public Build Options  ############################################
00058 
00060     int n_trees;
00061 
00063     int tree_depth;
00064 
00067     bool use_signed_samples;
00068 
00069 public:
00070     //#####  Public Member Functions  #########################################
00071 
00073     RBMWoodsLayer( real the_learning_rate=0. );
00074 
00076     virtual void generateSample() ;
00077 
00079     virtual void generateSamples();
00080 
00081     virtual void computeProbabilisticClustering(Vec& prob_clusters);
00082 
00084     virtual void computeExpectation() ;
00085 
00087     virtual void computeExpectations();
00088 
00090     virtual void fprop( const Vec& input, Vec& output ) const;
00091 
00093     virtual void fprop( const Mat& inputs, Mat& outputs );
00094 
00096     virtual void fprop( const Vec& input, const Vec& rbm_bias,
00097                         Vec& output ) const;
00098 
00100     virtual void bpropUpdate(const Vec& input, const Vec& output,
00101                              Vec& input_gradient, const Vec& output_gradient,
00102                              bool accumulate=false);
00103 
00105     virtual void bpropUpdate(const Vec& input, const Vec& rbm_bias,
00106                              const Vec& output,
00107                              Vec& input_gradient, Vec& rbm_bias_gradient,
00108                              const Vec& output_gradient) ;
00109 
00111     virtual void bpropUpdate(const Mat& inputs, const Mat& outputs,
00112                              Mat& input_gradients,
00113                              const Mat& output_gradients,
00114                              bool accumulate = false);
00115 
00118     virtual real fpropNLL(const Vec& target);
00119     virtual void fpropNLL(const Mat& targets, const Mat& costs_column);
00120 
00123     virtual void bpropNLL(const Vec& target, real nll, Vec& bias_gradient);
00124     virtual void bpropNLL(const Mat& targets, const Mat& costs_column,
00125                           Mat& bias_gradients);
00126 
00128     virtual real energy(const Vec& unit_values) const;
00129 
00133     virtual real freeEnergyContribution(const Vec& unit_activations) const;
00134 
00135 
00140     virtual void freeEnergyContributionGradient(const Vec& unit_activations,
00141                                                 Vec& unit_activations_gradient,
00142                                                 real output_gradient = 1,
00143                                                 bool accumulate = false) const;
00144     virtual int getConfigurationCount();
00145 
00146     virtual void getConfiguration(int conf_index, Vec& output);
00147 
00148     //#####  PLearn::Object Protocol  #########################################
00149 
00150     // Declares other standard object methods.
00151     PLEARN_DECLARE_OBJECT(RBMWoodsLayer);
00152 
00153     // Simply calls inherited::build() then build_()
00154     virtual void build();
00155 
00157     virtual void makeDeepCopyFromShallowCopy(CopiesMap& copies);
00158 
00159 protected:
00160     //#####  Not Options  #####################################################
00161 
00162     // Probability that sampling reaches a node but samples h=0 (expectation is for h=1)
00163     Vec off_expectation;
00164     Mat off_expectations;
00165     // Ordinary RBMBinomialLayer expectation
00166     Vec local_node_expectation;
00167     Mat local_node_expectations;
00168 
00169     // Computations of the local_node_expectation free energies for h = 1
00170     Vec on_free_energy;
00171     Mat on_free_energies;
00172     // Computations of the local_node_expectation free energies for h = 0
00173     Vec off_free_energy;
00174     Mat off_free_energies;
00175 
00176     // Gradient through the local_node_expectations (after sigmoid)
00177     Vec local_node_expectation_gradient;
00178     // Gradient through the tree structure
00179     Vec on_tree_gradient;
00180     Vec off_tree_gradient;
00181     Vec on_free_energy_gradient;
00182     Vec off_free_energy_gradient;
00183 
00184     // Temporary computations, for freeEnergyContribution() and its gradient variant
00185     mutable Vec tree_free_energies;
00186     mutable Vec tree_energies;
00187     mutable Vec unit_activations_pos_gradient;
00188     mutable Vec unit_activations_neg_gradient;
00189     mutable TVec<bool> unit_activations_pos_gradient_init;
00190     mutable TVec<bool> unit_activations_neg_gradient_init;
00191     
00192 protected:
00193     //#####  Protected Member Functions  ######################################
00194 
00196     static void declareOptions(OptionList& ol);
00197 
00198 private:
00199     //#####  Private Member Functions  ########################################
00200 
00202     void build_();
00203 
00204 private:
00205     //#####  Private Data Members  ############################################
00206 
00207     // The rest of the private stuff goes here
00208 };
00209 
00210 // Declares a few other classes and functions related to this class
00211 DECLARE_OBJECT_PTR(RBMWoodsLayer);
00212 
00213 } // end of namespace PLearn
00214 
00215 #endif
00216 
00217 
00218 /*
00219   Local Variables:
00220   mode:c++
00221   c-basic-offset:4
00222   c-file-style:"stroustrup"
00223   c-file-offsets:((innamespace . 0)(inline-open . 0))
00224   indent-tabs-mode:nil
00225   fill-column:79
00226   End:
00227 */
00228 // 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