PLearn 0.1
|
00001 // -*- C++ -*- 00002 00003 // HintonDeepBeliefNet.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 HintonDeepBeliefNet_INC 00041 #define HintonDeepBeliefNet_INC 00042 00043 #include <plearn_learners/distributions/PDistribution.h> 00044 #include <plearn/misc/PTimer.h> 00045 00046 namespace PLearn { 00047 00048 class RBMLayer; 00049 class RBMMixedLayer; 00050 class RBMMultinomialLayer; 00051 class RBMParameters; 00052 class RBMLLParameters; 00053 class RBMJointLLParameters; 00054 00062 class HintonDeepBeliefNet : public PDistribution 00063 { 00064 typedef PDistribution inherited; 00065 00066 public: 00067 //##### Public Build Options ############################################ 00068 00070 real learning_rate; 00071 00073 real fine_tuning_learning_rate; 00074 00076 real fine_tuning_decrease_ct; 00077 00079 real weight_decay; 00080 00086 string initialization_method; 00087 00090 int n_layers; 00091 00094 TVec< PP<RBMLayer> > layers; 00095 00097 PP<RBMLayer> last_layer; 00098 00100 PP<RBMMultinomialLayer> target_layer; 00101 00103 PP<RBMMixedLayer> joint_layer; 00104 00107 TVec< PP<RBMLLParameters> > params; 00108 00110 PP<RBMLLParameters> target_params; 00111 00114 PP<RBMJointLLParameters> joint_params; 00115 00118 bool sum_parallel_contributions; 00119 00122 TVec<int> training_schedule; 00123 00135 TVec<int> use_sample_or_expectation; 00136 00137 // measure training and test time 00138 PP<PTimer> ptimer; 00139 00140 public: 00141 //##### Public Member Functions ######################################### 00142 00144 // ### Make sure the implementation in the .cc 00145 // ### initializes all fields to reasonable default values. 00146 HintonDeepBeliefNet(); 00147 00148 00149 //##### PDistribution Member Functions ################################## 00150 00152 virtual real density(const Vec& y) const; 00153 00155 virtual real log_density(const Vec& y) const; 00156 00158 virtual real survival_fn(const Vec& y) const; 00159 00161 virtual real cdf(const Vec& y) const; 00162 00164 virtual void expectation(Vec& mu) const; 00165 00167 virtual void variance(Mat& cov) const; 00168 00171 virtual void generate(Vec& y) const; 00172 00173 //### Override this method if you need it (and if your distribution can 00174 //### handle it. Default version calls PLERROR. 00179 // virtual void generatePredictorGivenPredicted(Vec& x, const Vec& y); 00180 00182 //### See help in PDistribution.h. 00183 virtual bool setPredictorPredictedSizes(int the_predictor_size, 00184 int the_predicted_size, 00185 bool call_parent = true); 00186 00188 //### See help in PDistribution.h. 00189 virtual void setPredictor(const Vec& predictor, bool call_parent = true) 00190 const; 00191 00192 // ### These methods may be overridden for efficiency purpose: 00193 /* 00194 //### Default version calls setPredictorPredictedSises(0,-1) and generate 00199 virtual void generateJoint(Vec& xy); 00200 00201 //### Default version calls generateJoint and discards y 00206 virtual void generatePredictor(Vec& x); 00207 00208 //### Default version calls generateJoint and discards x 00213 virtual void generatePredicted(Vec& y); 00214 */ 00215 00216 00217 //##### PLearner Member Functions ####################################### 00218 00219 // ### Default version of inputsize returns learner->inputsize() 00220 // ### If this is not appropriate, you should uncomment this and define 00221 // ### it properly in the .cc 00222 // virtual int inputsize() const; 00223 00231 virtual void forget(); 00232 00236 virtual void train(); 00237 00241 virtual void computeCostsFromOutputs(const Vec& input, const Vec& output, 00242 const Vec& target, Vec& costs) const; 00243 00244 virtual TVec<string> getTestCostNames() const; 00245 virtual TVec<string> getTrainCostNames() const; 00246 00248 #if USING_MPI 00249 00250 00251 00252 00253 00254 00255 00256 virtual void test(VMat testset, PP<VecStatsCollector> test_stats, 00257 VMat testoutputs=0, VMat testcosts=0) const; 00258 #endif 00259 00260 //##### PLearn::Object Protocol ######################################### 00261 00262 // Declares other standard object methods. 00263 // ### If your class is not instantiatable (it has pure virtual methods) 00264 // ### you should replace this by PLEARN_DECLARE_ABSTRACT_OBJECT_METHODS 00265 PLEARN_DECLARE_OBJECT(HintonDeepBeliefNet); 00266 00267 // Simply calls inherited::build() then build_() 00268 virtual void build(); 00269 00271 // (PLEASE IMPLEMENT IN .cc) 00272 virtual void makeDeepCopyFromShallowCopy(CopiesMap& copies); 00273 00274 protected: 00275 //##### Protected Options ############################################### 00276 00277 // ### Declare protected option fields (such as learned parameters) here 00278 // ... 00279 00281 mutable TVec< Vec > activation_gradients; 00282 00284 mutable TVec< Vec > expectation_gradients; 00285 00287 mutable Vec output_gradient; 00288 00289 // store copy of positive statistics 00290 mutable Vec pos_down_values; 00291 mutable Vec pos_up_values; 00292 00293 00294 protected: 00295 //##### Protected Member Functions ###################################### 00296 00297 virtual void contrastiveDivergenceStep(const PP<RBMLayer>& down_layer, 00298 const PP<RBMParameters>& parameters, 00299 const PP<RBMLayer>& up_layer); 00300 00301 virtual void greedyStep( const Vec& predictor, int params_index ); 00302 virtual void jointGreedyStep( const Vec& input ); 00303 virtual void fineTuneByGradientDescent( const Vec& input, 00304 const Vec& train_costs ); 00305 00307 static void declareOptions(OptionList& ol); 00308 00309 private: 00310 //##### Private Member Functions ######################################## 00311 00313 void build_(); 00314 00316 void build_layers(); 00317 00319 void build_params(); 00320 00321 #if USING_MPI 00322 void shareParamsMPI(); 00323 #endif 00324 00325 private: 00326 //##### Private Data Members ############################################ 00327 00328 // The rest of the private stuff goes here 00329 00330 // ** NON-OPTION FIELDS (temporary workspace) 00331 00332 #if USING_MPI 00333 00334 Vec global_params; 00337 Vec previous_global_params; 00338 #endif 00339 }; 00340 00341 // Declares a few other classes and functions related to this class 00342 DECLARE_OBJECT_PTR(HintonDeepBeliefNet); 00343 00344 } // end of namespace PLearn 00345 00346 #endif 00347 00348 00349 /* 00350 Local Variables: 00351 mode:c++ 00352 c-basic-offset:4 00353 c-file-style:"stroustrup" 00354 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00355 indent-tabs-mode:nil 00356 fill-column:79 00357 End: 00358 */ 00359 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :