PLearn 0.1
|
00001 // -*- C++ -*- 00002 00003 // SupervisedDBN.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 SupervisedDBN_INC 00041 #define SupervisedDBN_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 RBMJointLLParameters; 00053 class OnlineLearningModule; 00054 00063 class SupervisedDBN : public PDistribution 00064 { 00065 typedef PDistribution inherited; 00066 00067 public: 00068 //##### Public Build Options ############################################ 00069 00071 bool regression; 00072 00074 real learning_rate; 00075 00077 Vec supervised_learning_rates; 00078 00080 real fine_tuning_learning_rate; 00081 00083 real initial_momentum; 00084 00086 real final_momentum; 00087 00090 int momentum_switch_time; 00091 00093 real weight_decay; 00094 00100 string initialization_method; 00101 00104 int n_layers; 00105 00108 TVec< PP<RBMLayer> > layers; 00109 00111 // PP<RBMLayer> last_layer; 00112 00114 // PP<RBMMultinomialLayer> target_layer; 00115 00117 // PP<RBMMixedLayer> joint_layer; 00118 00121 TVec< PP<RBMLLParameters> > params; 00122 00124 PP<RBMLLParameters> target_params; 00125 00128 // PP<RBMJointLLParameters> joint_params; 00129 00132 TVec< PP<OnlineLearningModule> > regressors; 00133 00138 int parallelization_minibatch_size; 00139 00142 bool sum_parallel_contributions; 00143 00146 TVec<int> training_schedule; 00147 00154 string fine_tuning_method; 00155 00156 // bool use_sample_rather_than_expectation_in_positive_phase_statistics; 00157 00169 TVec<int> use_sample_or_expectation; 00170 00171 public: 00172 //##### Public Member Functions ######################################### 00173 00175 // ### Make sure the implementation in the .cc 00176 // ### initializes all fields to reasonable default values. 00177 SupervisedDBN(); 00178 00179 00180 //##### PDistribution Member Functions ################################## 00181 00183 virtual real density(const Vec& y) const; 00184 00186 virtual real log_density(const Vec& y) const; 00187 00189 virtual real survival_fn(const Vec& y) const; 00190 00192 virtual real cdf(const Vec& y) const; 00193 00195 virtual void expectation(Vec& mu) const; 00196 00198 virtual void variance(Mat& cov) const; 00199 00202 virtual void generate(Vec& y) const; 00203 00204 //### Override this method if you need it (and if your distribution can 00205 //### handle it. Default version calls PLERROR. 00210 // virtual void generatePredictorGivenPredicted(Vec& x, const Vec& y); 00211 00213 //### See help in PDistribution.h. 00214 virtual bool setPredictorPredictedSizes(int the_predictor_size, 00215 int the_predicted_size, 00216 bool call_parent = true); 00217 00219 //### See help in PDistribution.h. 00220 virtual void setPredictor(const Vec& predictor, bool call_parent = true) 00221 const; 00222 00223 // ### These methods may be overridden for efficiency purpose: 00224 /* 00225 //### Default version calls setPredictorPredictedSises(0,-1) and generate 00230 virtual void generateJoint(Vec& xy); 00231 00232 //### Default version calls generateJoint and discards y 00237 virtual void generatePredictor(Vec& x); 00238 00239 //### Default version calls generateJoint and discards x 00244 virtual void generatePredicted(Vec& y); 00245 */ 00246 00247 00248 //##### PLearner Member Functions ####################################### 00249 00250 // ### Default version of inputsize returns learner->inputsize() 00251 // ### If this is not appropriate, you should uncomment this and define 00252 // ### it properly in the .cc 00253 // virtual int inputsize() const; 00254 00262 virtual void forget(); 00263 00267 virtual void train(); 00268 00272 virtual void computeCostsFromOutputs(const Vec& input, const Vec& output, 00273 const Vec& target, Vec& costs) const; 00274 00275 virtual TVec<string> getTestCostNames() const; 00276 virtual TVec<string> getTrainCostNames() const; 00277 00279 #if USING_MPI 00280 00281 00282 00283 00284 00285 00286 00287 virtual void test(VMat testset, PP<VecStatsCollector> test_stats, 00288 VMat testoutputs=0, VMat testcosts=0) const; 00289 #endif 00290 00291 00292 //##### PLearn::Object Protocol ######################################### 00293 00294 // Declares other standard object methods. 00295 // ### If your class is not instantiatable (it has pure virtual methods) 00296 // ### you should replace this by PLEARN_DECLARE_ABSTRACT_OBJECT_METHODS 00297 PLEARN_DECLARE_OBJECT(SupervisedDBN); 00298 00299 // Simply calls inherited::build() then build_() 00300 virtual void build(); 00301 00303 // (PLEASE IMPLEMENT IN .cc) 00304 virtual void makeDeepCopyFromShallowCopy(CopiesMap& copies); 00305 00306 protected: 00307 //##### Protected Options ############################################### 00308 00309 // ### Declare protected option fields (such as learned parameters) here 00310 // ... 00311 00313 mutable TVec< Vec > activation_gradients; 00314 00316 mutable TVec< Vec > expectation_gradients; 00317 00318 mutable Vec supervised_input; 00319 00320 mutable Vec store_costs; 00321 00323 // mutable Vec output_gradient; 00324 00325 00326 protected: 00327 //##### Protected Member Functions ###################################### 00328 00329 virtual void contrastiveDivergenceStep( 00330 const PP<RBMLayer>& down_layer, 00331 const PP<RBMParameters>& parameters, 00332 const PP<RBMLayer>& up_layer ); 00333 00334 virtual real supervisedContrastiveDivergenceStep( 00335 const PP<RBMLayer>& down_layer, 00336 const PP<RBMParameters>& parameters, 00337 const PP<RBMLayer>& up_layer, 00338 const Vec& target, 00339 int index ); 00340 00341 virtual real greedyStep( const Vec& predictor, int params_index ); 00342 // virtual real jointGreedyStep( const Vec& input ); 00343 virtual void fineTuneByGradientDescent( const Vec& input, 00344 Vec& train_costs ); 00345 00347 static void declareOptions(OptionList& ol); 00348 00349 private: 00350 //##### Private Member Functions ######################################## 00351 00353 void build_(); 00354 00356 void build_layers(); 00357 00359 void build_params(); 00360 00362 void build_regressors(); 00363 00364 #if USING_MPI 00365 void shareParamsMPI(); 00366 #endif 00367 00368 private: 00369 //##### Private Data Members ############################################ 00370 00371 // The rest of the private stuff goes here 00372 00373 #if USING_MPI 00374 00375 Vec global_params; 00378 Vec previous_global_params; 00379 #endif 00380 }; 00381 00382 // Declares a few other classes and functions related to this class 00383 DECLARE_OBJECT_PTR(SupervisedDBN); 00384 00385 } // end of namespace PLearn 00386 00387 #endif 00388 00389 00390 /* 00391 Local Variables: 00392 mode:c++ 00393 c-basic-offset:4 00394 c-file-style:"stroustrup" 00395 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00396 indent-tabs-mode:nil 00397 fill-column:79 00398 End: 00399 */ 00400 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :