PLearn 0.1
|
00001 // -*- C++ -*- 00002 00003 // DiscriminativeRBM.h 00004 // 00005 // Copyright (C) 2008 Hugo Larochelle 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: Hugo Larochelle 00036 00039 #ifndef DiscriminativeRBM_INC 00040 #define DiscriminativeRBM_INC 00041 00042 #include <plearn_learners/generic/PLearner.h> 00043 #include <plearn_learners/online/OnlineLearningModule.h> 00044 #include <plearn_learners/online/CostModule.h> 00045 #include <plearn_learners/online/CrossEntropyCostModule.h> 00046 #include <plearn_learners/online/NLLCostModule.h> 00047 #include <plearn_learners/online/RBMClassificationModule.h> 00048 #include <plearn_learners/online/RBMMultitaskClassificationModule.h> 00049 #include <plearn_learners/online/RBMLayer.h> 00050 #include <plearn_learners/online/RBMMixedLayer.h> 00051 #include <plearn_learners/online/RBMConnection.h> 00052 #include <plearn/misc/PTimer.h> 00053 #include <plearn/sys/Profiler.h> 00054 00055 namespace PLearn { 00056 using namespace std; 00057 00061 class DiscriminativeRBM : public PLearner 00062 { 00063 typedef PLearner inherited; 00064 00065 public: 00066 //##### Public Build Options ############################################ 00067 00069 real disc_learning_rate; 00070 00072 real disc_decrease_ct; 00073 00076 bool use_exact_disc_gradient; 00077 00080 real gen_learning_weight; 00081 00084 bool use_multi_conditional_learning; 00085 00088 real semi_sup_learning_weight; 00089 00091 int n_classes; 00092 00094 PP<RBMLayer> input_layer; 00095 00097 PP<RBMBinomialLayer> hidden_layer; 00098 00100 PP<RBMConnection> connection; 00101 00103 real target_weights_L1_penalty_factor; 00104 00106 real target_weights_L2_penalty_factor; 00107 00109 bool do_not_use_discriminative_learning; 00110 00112 int unlabeled_class_index_begin; 00113 00117 int n_classes_at_test_time; 00118 00121 int n_mean_field_iterations; 00122 00127 int gen_learning_every_n_samples; 00128 00129 //##### Public Learnt Options ########################################### 00131 PP<RBMClassificationModule> classification_module; 00132 00135 PP<RBMMultitaskClassificationModule> multitask_classification_module; 00136 00138 TVec<string> cost_names; 00139 00142 PP<NLLCostModule> classification_cost; 00143 00146 PP<RBMMixedLayer> joint_layer; 00147 00148 public: 00149 //##### Public Member Functions ######################################### 00150 00152 DiscriminativeRBM(); 00153 00154 00155 //##### PLearner Member Functions ####################################### 00156 00159 virtual int outputsize() const; 00160 00164 // (PLEASE IMPLEMENT IN .cc) 00165 virtual void forget(); 00166 00170 // (PLEASE IMPLEMENT IN .cc) 00171 virtual void train(); 00172 00174 // (PLEASE IMPLEMENT IN .cc) 00175 virtual void computeOutput(const Vec& input, Vec& output) const; 00176 00178 // (PLEASE IMPLEMENT IN .cc) 00179 virtual void computeCostsFromOutputs(const Vec& input, const Vec& output, 00180 const Vec& target, Vec& costs) const; 00181 00184 // (PLEASE IMPLEMENT IN .cc) 00185 virtual TVec<std::string> getTestCostNames() const; 00186 00189 // (PLEASE IMPLEMENT IN .cc) 00190 virtual TVec<std::string> getTrainCostNames() const; 00191 00192 00193 // *** SUBCLASS WRITING: *** 00194 // While in general not necessary, in case of particular needs 00195 // (efficiency concerns for ex) you may also want to overload 00196 // some of the following methods: 00197 // virtual void computeOutputAndCosts(const Vec& input, const Vec& target, 00198 // Vec& output, Vec& costs) const; 00199 // virtual void computeCostsOnly(const Vec& input, const Vec& target, 00200 // Vec& costs) const; 00201 // virtual void test(VMat testset, PP<VecStatsCollector> test_stats, 00202 // VMat testoutputs=0, VMat testcosts=0) const; 00203 // virtual int nTestCosts() const; 00204 // virtual int nTrainCosts() const; 00205 // virtual void resetInternalState(); 00206 // virtual bool isStatefulLearner() const; 00207 00208 00209 //##### PLearn::Object Protocol ######################################### 00210 00211 // Declares other standard object methods. 00212 // ### If your class is not instantiatable (it has pure virtual methods) 00213 // ### you should replace this by PLEARN_DECLARE_ABSTRACT_OBJECT_METHODS 00214 PLEARN_DECLARE_OBJECT(DiscriminativeRBM); 00215 00216 // Simply calls inherited::build() then build_() 00217 virtual void build(); 00218 00220 // (PLEASE IMPLEMENT IN .cc) 00221 virtual void makeDeepCopyFromShallowCopy(CopiesMap& copies); 00222 00223 protected: 00224 00225 //##### Not Options ##################################################### 00226 00229 PP<RBMMatrixConnection> last_to_target; 00230 00233 PP<RBMConnection> last_to_target_connection; 00234 00237 PP<RBMConnection> joint_connection; 00238 00241 PP<RBMLayer> target_layer; 00242 00244 PP<RBMClassificationModule> unlabeled_classification_module; 00245 00247 PP<RBMClassificationModule> test_time_classification_module; 00248 00250 mutable Vec target_one_hot; 00251 00252 mutable Vec disc_pos_down_val; 00253 mutable Vec disc_pos_up_val; 00254 mutable Vec disc_neg_down_val; 00255 mutable Vec disc_neg_up_val; 00256 00257 mutable Vec gen_pos_down_val; 00258 mutable Vec gen_pos_up_val; 00259 mutable Vec gen_neg_down_val; 00260 mutable Vec gen_neg_up_val; 00261 00262 mutable Vec semi_sup_pos_down_val; 00263 mutable Vec semi_sup_pos_up_val; 00264 mutable Vec semi_sup_neg_down_val; 00265 mutable Vec semi_sup_neg_up_val; 00266 00268 mutable Vec input_gradient; 00269 mutable Vec class_output; 00270 mutable Vec before_class_output; 00271 mutable Vec unlabeled_class_output; 00272 mutable Vec test_time_class_output; 00273 mutable Vec class_gradient; 00274 00276 int nll_cost_index; 00277 00279 int class_cost_index; 00280 00282 int hamming_loss_index; 00283 00284 protected: 00285 //##### Protected Member Functions ###################################### 00286 00288 static void declareOptions(OptionList& ol); 00289 00290 private: 00291 //##### Private Member Functions ######################################## 00292 00294 void build_(); 00295 00296 void build_layers_and_connections(); 00297 00298 void build_costs(); 00299 00300 void build_classification_cost(); 00301 00302 void setLearningRate( real the_learning_rate ); 00303 00304 private: 00305 //##### Private Data Members ############################################ 00306 00307 // The rest of the private stuff goes here 00308 }; 00309 00310 // Declares a few other classes and functions related to this class 00311 DECLARE_OBJECT_PTR(DiscriminativeRBM); 00312 00313 } // end of namespace PLearn 00314 00315 #endif 00316 00317 00318 /* 00319 Local Variables: 00320 mode:c++ 00321 c-basic-offset:4 00322 c-file-style:"stroustrup" 00323 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00324 indent-tabs-mode:nil 00325 fill-column:79 00326 End: 00327 */ 00328 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :