PLearn 0.1
RBMModule.h
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // RBMModule.h
00004 //
00005 // Copyright (C) 2007 Olivier Delalleau
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 RBMModule_INC
00041 #define RBMModule_INC
00042 
00043 #include <plearn_learners/online/OnlineLearningModule.h>
00044 #include <plearn_learners/online/RBMConnection.h>
00045 #include <plearn_learners/online/RBMLayer.h>
00046 #include <map>
00047 
00048 namespace PLearn {
00049 
00060 class RBMModule : public OnlineLearningModule
00061 {
00062     typedef OnlineLearningModule inherited;
00063 
00064 public:
00065     //#####  Public Build Options  ############################################
00066 
00067     PP<RBMLayer> hidden_layer;
00068     PP<RBMLayer> visible_layer;
00069     PP<RBMConnection> connection;
00070     PP<RBMConnection> reconstruction_connection;
00071 
00072     real cd_learning_rate;
00073     real grad_learning_rate;
00074     bool tied_connection_weights;
00075 
00076     bool compute_contrastive_divergence;
00077     bool compare_true_gradient_with_cd;
00078     int n_steps_compare;
00079 
00082     int n_Gibbs_steps_CD;
00083 
00085     int min_n_Gibbs_steps;
00086     int n_Gibbs_steps_per_generated_sample;
00087 
00088     bool compute_log_likelihood;
00089     bool minimize_log_likelihood;
00090 
00091     //#####  Public Learnt Options  ############################################
00093     int Gibbs_step;
00094     real log_partition_function;
00095     bool partition_function_is_stale;
00096 
00097     bool deterministic_reconstruction_in_cd;
00098     bool stochastic_reconstruction;
00099 
00100     bool standard_cd_grad;
00101     bool standard_cd_bias_grad;
00102     bool standard_cd_weights_grad;
00103 
00104 
00105 public:
00106     //#####  Public Member Functions  #########################################
00107 
00109     RBMModule();
00110 
00111     // Your other public member functions go here
00112 
00114     virtual void CDUpdate(const Mat& v_0, const Mat& h_0,
00115                           const Mat& v_k, const Mat& h_k);
00116 
00118     virtual void fprop(const Vec& input, Vec& output) const;
00119 
00120     /* Optional
00121        THE DEFAULT IMPLEMENTATION IN SUPER-CLASS JUST RAISES A PLERROR.
00130     virtual void bpropUpdate(const Vec& input, const Vec& output,
00131                              Vec& input_gradient,
00132                              const Vec& output_gradient,
00133                              bool accumulate=false);
00134     */
00135 
00136     /* Optional
00137        A DEFAULT IMPLEMENTATION IS PROVIDED IN THE SUPER-CLASS, WHICH
00138        JUST CALLS
00139             bpropUpdate(input, output, input_gradient, output_gradient)
00140        AND IGNORES INPUT GRADIENT.
00142     virtual void bpropUpdate(const Vec& input, const Vec& output,
00143                              const Vec& output_gradient);
00144     */
00145 
00146     /* Optional
00147        N.B. A DEFAULT IMPLEMENTATION IS PROVIDED IN THE SUPER-CLASS, WHICH
00148        RAISES A PLERROR.
00153     virtual void bbpropUpdate(const Vec& input, const Vec& output,
00154                               Vec& input_gradient,
00155                               const Vec& output_gradient,
00156                               Vec& input_diag_hessian,
00157                               const Vec& output_diag_hessian,
00158                               bool accumulate=false);
00159     */
00160 
00161     /* Optional
00162        N.B. A DEFAULT IMPLEMENTATION IS PROVIDED IN THE SUPER-CLASS,
00163        WHICH JUST CALLS
00164             bbpropUpdate(input, output, input_gradient, output_gradient,
00165                          out_hess, in_hess)
00166        AND IGNORES INPUT HESSIAN AND INPUT GRADIENT.
00168     virtual void bbpropUpdate(const Vec& input, const Vec& output,
00169                               const Vec& output_gradient,
00170                               const Vec& output_diag_hessian);
00171     */
00172 
00173 
00177     virtual void forget();
00178 
00179 
00180     /* Optional
00181        THE DEFAULT IMPLEMENTATION PROVIDED IN THE SUPER-CLASS DOES NOT
00182        DO ANYTHING.
00186     virtual void finalize();
00187     */
00188 
00189     /* Optional
00190        THE DEFAULT IMPLEMENTATION PROVIDED IN THE SUPER-CLASS RETURNS false
00192     virtual bool bpropDoesNothing();
00193     */
00194 
00197     virtual void setLearningRate(real dynamic_learning_rate);
00198 
00200     virtual void fprop(const TVec<Mat*>& ports_value);
00201 
00203     virtual void bpropAccUpdate(const TVec<Mat*>& ports_value,
00204                                 const TVec<Mat*>& ports_gradient);
00205 
00207     virtual const TVec<string>& getPorts();
00208 
00210     virtual const TMat<int>& getPortSizes();
00211 
00215     virtual int getPortIndex(const string& port);
00216 
00217     //#####  PLearn::Object Protocol  #########################################
00218 
00219     // Declares other standard object methods.
00220     // ### If your class is not instantiatable (it has pure virtual methods)
00221     // ### you should replace this by PLEARN_DECLARE_ABSTRACT_OBJECT
00222     PLEARN_DECLARE_OBJECT(RBMModule);
00223 
00224     // Simply calls inherited::build() then build_()
00225     virtual void build();
00226 
00228     virtual void makeDeepCopyFromShallowCopy(CopiesMap& copies);
00229 
00230 
00231 protected:
00232 
00233     Mat* hidden_bias;
00234     Mat* weights;
00235 
00237     Mat hidden_exp_grad;
00238 
00240     Mat hidden_act_grad;
00241 
00243     Mat visible_exp_grad;
00244 
00246     Mat visible_act_grad;
00247 
00249     Vec visible_bias_grad;
00250 
00252     Mat hidden_exp_store;
00253     Mat hidden_act_store;
00254     Mat* hidden_act;
00255     bool hidden_activations_are_computed;
00256 
00257     bool hidden_is_output;
00258 
00260     Mat store_weights_grad;
00261 
00263     Mat store_hidden_bias_grad;
00264 
00266     TVec<string> ports;
00267 
00269     map<string, int> portname_to_index;
00270 
00272     Mat energy_inputs;
00273 
00277     Vec all_p_visible;
00278 
00281     Mat all_hidden_cond_prob;
00282 
00285     Mat all_visible_cond_prob;
00286 
00289     Mat p_ht_given_x;
00290 
00293     Mat p_xt_given_x;
00294 
00295     //#####  Protected Member Functions  ######################################
00296 
00298     void addPortName(const string& name);
00299 
00302     void setLearningRatesOnlyForLayers(real lr);
00303 
00305     static void declareOptions(OptionList& ol);
00306 
00307 public:
00309     void setAllLearningRates(real lr);
00310 
00315     void computeHiddenActivations(const Mat& visible);
00316 
00321     void computeVisibleActivations(const Mat& hidden,
00322                                    bool using_reconstruction_connection=false);
00323 
00330     void computePositivePhaseHiddenActivations(const Mat& visible);
00331 
00333     void sampleHiddenGivenVisible(const Mat& visible);
00334 
00336     void sampleVisibleGivenHidden(const Mat& hidden);
00337 
00342     void computeFreeEnergyOfVisible(const Mat& visible, Mat& energy,
00343                                     bool positive_phase = true);
00344 
00347     void computeFreeEnergyOfHidden(const Mat& hidden, Mat& energy);
00348 
00353     void computeEnergy(const Mat& visible, const Mat& hidden, Mat& energy,
00354                        bool positive_phase = true);
00355 
00356     void computePartitionFunction();
00357 
00361     Vec computeLogLikelihoodOfVisible(const Mat& visible);
00362 
00366     void computeAllHiddenProbabilities(const Mat& visible,
00367                                        const Mat& p_hidden);
00368 
00369     void computeNegLogPVisibleGivenPHidden(Mat visible, Mat hidden, Mat* neg_log_phidden, Mat& neg_log_pvisible_given_phidden);
00370 
00371 protected:
00372     static void declareMethods(RemoteMethodMap& rmm);
00373 
00374 
00375 private:
00376     //#####  Private Member Functions  ########################################
00377 
00379     void build_();
00380 
00381 private:
00382     //#####  Private Data Members  ############################################
00383 
00384     // The rest of the private stuff goes here
00385 };
00386 
00387 // Declares a few other classes and functions related to this class
00388 DECLARE_OBJECT_PTR(RBMModule);
00389 
00390 } // end of namespace PLearn
00391 
00392 #endif
00393 
00394 
00395 /*
00396   Local Variables:
00397   mode:c++
00398   c-basic-offset:4
00399   c-file-style:"stroustrup"
00400   c-file-offsets:((innamespace . 0)(inline-open . 0))
00401   indent-tabs-mode:nil
00402   fill-column:79
00403   End:
00404 */
00405 // 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