PLearn 0.1
KLp0p1RBMModule.h
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // KLp0p1RBMModule.h
00004 //
00005 // Copyright (C) 2007 Olivier Delalleau, 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 // Authors: Olivier Delalleau
00036 
00040 #ifndef KLp0p1RBMModule_INC
00041 #define KLp0p1RBMModule_INC
00042 
00043 #include <map>
00044 #include <plearn_learners/online/OnlineLearningModule.h>
00045 #include <plearn_learners/online/RBMConnection.h>
00046 #include <plearn_learners/online/RBMLayer.h>
00047 #include <plearn/vmat/VMat.h>
00048 
00049 namespace PLearn {
00050 
00061 class KLp0p1RBMModule : public OnlineLearningModule
00062 {
00063     typedef OnlineLearningModule inherited;
00064 
00065 public:
00066     //#####  Public Build Options  ############################################
00067 
00068     VMat training_set;
00069 
00070     PP<RBMLayer> hidden_layer;
00071     PP<RBMLayer> visible_layer;
00072     PP<RBMConnection> connection;
00073     PP<RBMConnection> reconstruction_connection;
00074 
00075     real cd_learning_rate;
00076     real grad_learning_rate;
00077     real klp0p1_learning_rate;
00078 
00079     bool compute_contrastive_divergence;
00080 
00083     int n_Gibbs_steps_CD;
00084 
00086     int min_n_Gibbs_steps;
00087     int n_Gibbs_steps_per_generated_sample;
00088 
00089     bool compute_log_likelihood;
00090     bool minimize_log_likelihood;
00091 
00092     //#####  Public Learnt Options  ############################################
00094     int Gibbs_step;
00095     real log_partition_function;
00096     bool partition_function_is_stale;
00097 
00098     bool standard_cd_grad;
00099     bool standard_cd_bias_grad;
00100     bool standard_cd_weights_grad;
00101 
00102 
00103 public:
00104     //#####  Public Member Functions  #########################################
00105 
00107     KLp0p1RBMModule();
00108 
00109     // Your other public member functions go here
00110 
00112     virtual void fprop(const Vec& input, Vec& output) const;
00113 
00114     /* Optional
00115        THE DEFAULT IMPLEMENTATION IN SUPER-CLASS JUST RAISES A PLERROR.
00124     virtual void bpropUpdate(const Vec& input, const Vec& output,
00125                              Vec& input_gradient,
00126                              const Vec& output_gradient,
00127                              bool accumulate=false);
00128     */
00129 
00130     /* Optional
00131        A DEFAULT IMPLEMENTATION IS PROVIDED IN THE SUPER-CLASS, WHICH
00132        JUST CALLS
00133             bpropUpdate(input, output, input_gradient, output_gradient)
00134        AND IGNORES INPUT GRADIENT.
00136     virtual void bpropUpdate(const Vec& input, const Vec& output,
00137                              const Vec& output_gradient);
00138     */
00139 
00140     /* Optional
00141        N.B. A DEFAULT IMPLEMENTATION IS PROVIDED IN THE SUPER-CLASS, WHICH
00142        RAISES A PLERROR.
00147     virtual void bbpropUpdate(const Vec& input, const Vec& output,
00148                               Vec& input_gradient,
00149                               const Vec& output_gradient,
00150                               Vec& input_diag_hessian,
00151                               const Vec& output_diag_hessian,
00152                               bool accumulate=false);
00153     */
00154 
00155     /* Optional
00156        N.B. A DEFAULT IMPLEMENTATION IS PROVIDED IN THE SUPER-CLASS,
00157        WHICH JUST CALLS
00158             bbpropUpdate(input, output, input_gradient, output_gradient,
00159                          out_hess, in_hess)
00160        AND IGNORES INPUT HESSIAN AND INPUT GRADIENT.
00162     virtual void bbpropUpdate(const Vec& input, const Vec& output,
00163                               const Vec& output_gradient,
00164                               const Vec& output_diag_hessian);
00165     */
00166 
00167 
00171     virtual void forget();
00172 
00173 
00174     /* Optional
00175        THE DEFAULT IMPLEMENTATION PROVIDED IN THE SUPER-CLASS DOES NOT
00176        DO ANYTHING.
00180     virtual void finalize();
00181     */
00182 
00183     /* Optional
00184        THE DEFAULT IMPLEMENTATION PROVIDED IN THE SUPER-CLASS RETURNS false
00186     virtual bool bpropDoesNothing();
00187     */
00188 
00191     virtual void setLearningRate(real dynamic_learning_rate);
00192 
00194     virtual void fprop(const TVec<Mat*>& ports_value);
00195 
00197     virtual void bpropAccUpdate(const TVec<Mat*>& ports_value,
00198                                 const TVec<Mat*>& ports_gradient);
00199 
00201     virtual const TVec<string>& getPorts();
00202 
00204     virtual const TMat<int>& getPortSizes();
00205 
00209     virtual int getPortIndex(const string& port);
00210 
00211     //#####  PLearn::Object Protocol  #########################################
00212 
00213     // Declares other standard object methods.
00214     // ### If your class is not instantiatable (it has pure virtual methods)
00215     // ### you should replace this by PLEARN_DECLARE_ABSTRACT_OBJECT
00216     PLEARN_DECLARE_OBJECT(KLp0p1RBMModule);
00217 
00218     // Simply calls inherited::build() then build_()
00219     virtual void build();
00220 
00222     virtual void makeDeepCopyFromShallowCopy(CopiesMap& copies);
00223 
00224 
00225 protected:
00226 
00227     PP<RBMLayer> conf_hidden_layer;
00228     PP<RBMLayer> conf_visible_layer;
00229 
00230     Mat* hidden_bias;
00231     Mat* weights;
00232 
00234     Mat hidden_exp_grad;
00235 
00237     Mat hidden_act_grad;
00238 
00240     Mat visible_exp_grad;
00241 
00243     Mat visible_act_grad;
00244 
00246     Vec visible_bias_grad;
00247 
00249     Mat hidden_exp_store;
00250     Mat hidden_act_store;
00251     Mat* hidden_act;
00252     bool hidden_activations_are_computed;
00253 
00255     Mat store_weights_grad;
00256 
00258     Mat store_hidden_bias_grad;
00259 
00261     TVec<string> ports;
00262 
00264     map<string, int> portname_to_index;
00265 
00267     Mat energy_inputs;
00268 
00269     //#####  Protected Member Functions  ######################################
00270 
00272     void addPortName(const string& name);
00273 
00275     void setAllLearningRates(real lr);
00276 
00278     static void declareOptions(OptionList& ol);
00279 
00284     void computeHiddenActivations(const Mat& visible);
00285 
00290     void computeVisibleActivations(const Mat& hidden,
00291                                    bool using_reconstruction_connection=false);
00292 
00299     void computePositivePhaseHiddenActivations(const Mat& visible);
00300 
00302     void sampleHiddenGivenVisible(const Mat& visible);
00303 
00305     void sampleVisibleGivenHidden(const Mat& hidden);
00306 
00311     void computeFreeEnergyOfVisible(const Mat& visible, Mat& energy,
00312                                     bool positive_phase = true);
00313 
00316     void computeFreeEnergyOfHidden(const Mat& hidden, Mat& energy);
00317 
00322     void computeEnergy(const Mat& visible, const Mat& hidden, Mat& energy,
00323                        bool positive_phase = true);
00324 
00325 private:
00326     //#####  Private Member Functions  ########################################
00327 
00329     void build_();
00330 
00331 private:
00332     //#####  Private Data Members  ############################################
00333 
00334     // The rest of the private stuff goes here
00335 };
00336 
00337 // Declares a few other classes and functions related to this class
00338 DECLARE_OBJECT_PTR(KLp0p1RBMModule);
00339 
00340 } // end of namespace PLearn
00341 
00342 #endif
00343 
00344 
00345 /*
00346   Local Variables:
00347   mode:c++
00348   c-basic-offset:4
00349   c-file-style:"stroustrup"
00350   c-file-offsets:((innamespace . 0)(inline-open . 0))
00351   indent-tabs-mode:nil
00352   fill-column:79
00353   End:
00354 */
00355 // 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