PLearn 0.1
GaussPartSupervisedDBN.h
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // GaussPartSupervisedDBN.h
00004 //
00005 // Copyright (C) 2006 Pascal Lamblin
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: Pascal Lamblin
00036 
00040 #ifndef GaussPartSupervisedDBN_INC
00041 #define GaussPartSupervisedDBN_INC
00042 
00043 #include <plearn_learners/distributions/PDistribution.h>
00044 
00045 namespace PLearn {
00046 
00047 class RBMLayer;
00048 class RBMMixedLayer;
00049 class RBMMultinomialLayer;
00050 class RBMParameters;
00051 class RBMLLParameters;
00052 class RBMQLParameters;
00053 class RBMJointLLParameters;
00054 class OnlineLearningModule;
00055 
00063 class GaussPartSupervisedDBN : public PDistribution
00064 {
00065     typedef PDistribution inherited;
00066 
00067 public:
00068     //#####  Public Build Options  ############################################
00069 
00071     real learning_rate;
00072 
00074     Vec supervised_learning_rates;
00075 
00077     real fine_tuning_learning_rate;
00078 
00080     real initial_momentum;
00081 
00083     real final_momentum;
00084 
00087     int momentum_switch_time;
00088 
00090     real weight_decay;
00091 
00097     string initialization_method;
00098 
00101     int n_layers;
00102 
00105     TVec< PP<RBMLayer> > layers;
00106 
00108     PP<RBMLayer> last_layer;
00109 
00111     PP<RBMMultinomialLayer> target_layer;
00112 
00114     PP<RBMMixedLayer> joint_layer;
00115 
00118     TVec< PP<RBMLLParameters> > params;
00119     
00122     PP<RBMQLParameters>  input_params;
00123 
00125     PP<RBMLLParameters> target_params;
00126 
00129     PP<RBMJointLLParameters> joint_params;
00130 
00133     TVec< PP<OnlineLearningModule> > regressors;
00134 
00139     int parallelization_minibatch_size;
00140 
00143     bool sum_parallel_contributions;
00144 
00147     TVec<int> training_schedule;
00148 
00155     string fine_tuning_method;
00156 
00157 //    bool use_sample_rather_than_expectation_in_positive_phase_statistics;
00158 
00170     TVec<int> use_sample_or_expectation;
00171 
00172 public:
00173     //#####  Public Member Functions  #########################################
00174 
00176     // ### Make sure the implementation in the .cc
00177     // ### initializes all fields to reasonable default values.
00178     GaussPartSupervisedDBN();
00179 
00180 
00181     //#####  PDistribution Member Functions  ##################################
00182 
00184     virtual real density(const Vec& y) const;
00185 
00187     virtual real log_density(const Vec& y) const;
00188 
00190     virtual real survival_fn(const Vec& y) const;
00191 
00193     virtual real cdf(const Vec& y) const;
00194 
00196     virtual void expectation(Vec& mu) const;
00197 
00199     virtual void variance(Mat& cov) const;
00200 
00203     virtual void generate(Vec& y) const;
00204 
00205     //### Override this method if you need it (and if your distribution can
00206     //### handle it. Default version calls PLERROR.
00211     // virtual void generatePredictorGivenPredicted(Vec& x, const Vec& y);
00212 
00214     //### See help in PDistribution.h.
00215     virtual bool setPredictorPredictedSizes(int the_predictor_size,
00216                                             int the_predicted_size,
00217                                             bool call_parent = true);
00218 
00220     //### See help in PDistribution.h.
00221     virtual void setPredictor(const Vec& predictor, bool call_parent = true)
00222                               const;
00223 
00224     // ### These methods may be overridden for efficiency purpose:
00225     /*
00226     //### Default version calls setPredictorPredictedSises(0,-1) and generate
00231     virtual void generateJoint(Vec& xy);
00232 
00233     //### Default version calls generateJoint and discards y
00238     virtual void generatePredictor(Vec& x);
00239 
00240     //### Default version calls generateJoint and discards x
00245     virtual void generatePredicted(Vec& y);
00246     */
00247 
00248 
00249     //#####  PLearner Member Functions  #######################################
00250 
00251     // ### Default version of inputsize returns learner->inputsize()
00252     // ### If this is not appropriate, you should uncomment this and define
00253     // ### it properly in the .cc
00254     // virtual int inputsize() const;
00255 
00263     virtual void forget();
00264 
00268     virtual void train();
00269 
00273     virtual void computeCostsFromOutputs(const Vec& input, const Vec& output,
00274                                          const Vec& target, Vec& costs) const;
00275 
00276     virtual TVec<string> getTestCostNames() const;
00277     virtual TVec<string> getTrainCostNames() const;
00278 
00280 #if USING_MPI
00281 
00282 
00283 
00284 
00285 
00286 
00287 
00288     virtual void test(VMat testset, PP<VecStatsCollector> test_stats,
00289                       VMat testoutputs=0, VMat testcosts=0) const;
00290 #endif
00291 
00292 
00293     //#####  PLearn::Object Protocol  #########################################
00294 
00295     // Declares other standard object methods.
00296     // ### If your class is not instantiatable (it has pure virtual methods)
00297     // ### you should replace this by PLEARN_DECLARE_ABSTRACT_OBJECT_METHODS
00298     PLEARN_DECLARE_OBJECT(GaussPartSupervisedDBN);
00299 
00300     // Simply calls inherited::build() then build_()
00301     virtual void build();
00302 
00304     // (PLEASE IMPLEMENT IN .cc)
00305     virtual void makeDeepCopyFromShallowCopy(CopiesMap& copies);
00306 
00307 protected:
00308     //#####  Protected Options  ###############################################
00309 
00310     // ### Declare protected option fields (such as learned parameters) here
00311     // ...
00312 
00314     mutable TVec< Vec > activation_gradients;
00315 
00317     mutable TVec< Vec > expectation_gradients;
00318 
00320     mutable Vec output_gradient;
00321 
00322 
00323 protected:
00324     //#####  Protected Member Functions  ######################################
00325 
00326     virtual void contrastiveDivergenceStep(
00327         const PP<RBMLayer>& down_layer,
00328         const PP<RBMParameters>& parameters,
00329         const PP<RBMLayer>& up_layer );
00330 
00331     virtual real supervisedContrastiveDivergenceStep(
00332         const PP<RBMLayer>& down_layer,
00333         const PP<RBMParameters>& parameters,
00334         const PP<RBMLayer>& up_layer,
00335         const Vec& target,
00336         int index );
00337 
00338     virtual real greedyStep( const Vec& predictor, int params_index );
00339     virtual real jointGreedyStep( const Vec& input );
00340     virtual void fineTuneByGradientDescent( const Vec& input,
00341                                             const Vec& train_costs );
00342 
00344     static void declareOptions(OptionList& ol);
00345 
00346 private:
00347     //#####  Private Member Functions  ########################################
00348 
00350     void build_();
00351 
00353     void build_layers();
00354 
00356     void build_params();
00357 
00359     void build_regressors();
00360 
00361 #if USING_MPI
00362     void shareParamsMPI();
00363 #endif
00364 
00365 private:
00366     //#####  Private Data Members  ############################################
00367 
00368     // The rest of the private stuff goes here
00369 
00370 #if USING_MPI
00371 
00372     Vec global_params;
00375     Vec previous_global_params;
00376 #endif
00377 };
00378 
00379 // Declares a few other classes and functions related to this class
00380 DECLARE_OBJECT_PTR(GaussPartSupervisedDBN);
00381 
00382 } // end of namespace PLearn
00383 
00384 #endif
00385 
00386 
00387 /*
00388   Local Variables:
00389   mode:c++
00390   c-basic-offset:4
00391   c-file-style:"stroustrup"
00392   c-file-offsets:((innamespace . 0)(inline-open . 0))
00393   indent-tabs-mode:nil
00394   fill-column:79
00395   End:
00396 */
00397 // 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