PLearn 0.1
|
00001 // -*- C++ -*- 00002 00003 // StackedFocusedAutoassociatorsNet.h 00004 // 00005 // Copyright (C) 2007 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: Pascal Lamblin 00036 00040 #ifndef StackedFocusedAutoassociatorsNet_INC 00041 #define StackedFocusedAutoassociatorsNet_INC 00042 00043 #include <plearn/vmat/ClassSubsetVMatrix.h> 00044 #include <plearn_learners/generic/PLearner.h> 00045 #include <plearn_learners/online/GradNNetLayerModule.h> 00046 #include <plearn_learners/online/OnlineLearningModule.h> 00047 #include <plearn_learners/online/CostModule.h> 00048 #include <plearn_learners/online/ModuleStackModule.h> 00049 #include <plearn_learners/online/NLLCostModule.h> 00050 #include <plearn_learners/online/ClassErrorCostModule.h> 00051 #include <plearn_learners/online/CombiningCostsModule.h> 00052 #include <plearn_learners/online/RBMClassificationModule.h> 00053 #include <plearn_learners/online/RBMLayer.h> 00054 #include <plearn_learners/online/RBMMixedLayer.h> 00055 #include <plearn_learners/online/RBMConnection.h> 00056 #include <plearn_learners/online/SoftmaxModule.h> 00057 #include <plearn/misc/PTimer.h> 00058 00059 namespace PLearn { 00060 00067 class StackedFocusedAutoassociatorsNet : public PLearner 00068 { 00069 typedef PLearner inherited; 00070 00071 public: 00072 //##### Public Build Options ############################################ 00073 00075 real cd_learning_rate; 00076 00078 real cd_decrease_ct; 00079 00081 real greedy_learning_rate; 00082 00086 real greedy_decrease_ct; 00087 00089 real supervised_greedy_learning_rate; 00090 00092 real supervised_greedy_decrease_ct; 00093 00095 real fine_tuning_learning_rate; 00096 00099 real fine_tuning_decrease_ct; 00100 00103 TVec<int> training_schedule; 00104 00106 TVec< PP<RBMLayer> > layers; 00107 00109 TVec< PP<RBMConnection> > connections; 00110 00112 TVec< PP<RBMConnection> > reconstruction_connections; 00113 00115 TVec< PP<RBMLayer> > unsupervised_layers; 00116 00118 TVec< PP<RBMConnection> > unsupervised_connections; 00119 00122 int k_neighbors; 00123 00125 int n_classes; 00126 00128 real dissimilar_example_cost_precision; 00129 00132 bool do_not_use_knn_classifier; 00133 00135 real output_weights_l1_penalty_factor; 00136 00138 real output_weights_l2_penalty_factor; 00139 00140 //##### Public Learnt Options ########################################### 00141 00143 int n_layers; 00144 00145 public: 00146 //##### Public Member Functions ######################################### 00147 00149 StackedFocusedAutoassociatorsNet(); 00150 00151 //##### PLearner Member Functions ####################################### 00152 00155 virtual int outputsize() const; 00156 00160 virtual void forget(); 00161 00165 virtual void train(); 00166 00168 virtual void computeOutput(const Vec& input, Vec& output) const; 00169 00171 virtual void computeCostsFromOutputs(const Vec& input, const Vec& output, 00172 const Vec& target, Vec& costs) const; 00173 00178 virtual void updateTrainSetRepresentations() const; 00179 00182 virtual TVec<std::string> getTestCostNames() const; 00183 00186 virtual TVec<std::string> getTrainCostNames() const; 00187 00195 virtual void setTrainingSet(VMat training_set, bool call_forget=true); 00196 00197 void greedyStep( const Vec& input, const Vec& target, int index, 00198 Vec train_costs, int stage, Vec similar_example, 00199 Vec dissimilar_example); 00200 00201 void fineTuningStep( const Vec& input, const Vec& target, 00202 Vec& train_costs, Vec similar_example, 00203 Vec dissimilar_example ); 00204 00205 void computeRepresentation( const Vec& input, 00206 Vec& representation, int layer) const; 00207 00208 //##### PLearn::Object Protocol ######################################### 00209 00210 // Declares other standard object methods. 00211 // ### If your class is not instantiatable (it has pure virtual methods) 00212 // ### you should replace this by PLEARN_DECLARE_ABSTRACT_OBJECT_METHODS 00213 PLEARN_DECLARE_OBJECT(StackedFocusedAutoassociatorsNet); 00214 00215 // Simply calls inherited::build() then build_() 00216 virtual void build(); 00217 00219 // (PLEASE IMPLEMENT IN .cc) 00220 virtual void makeDeepCopyFromShallowCopy(CopiesMap& copies); 00221 00222 protected: 00223 //##### Not Options ##################################################### 00224 00227 mutable TVec<Vec> activations; 00228 00231 mutable TVec<Vec> expectations; 00232 00236 mutable TVec<Vec> activation_gradients; 00237 00241 mutable TVec<Vec> expectation_gradients; 00242 00244 mutable Vec greedy_activation; 00245 00247 mutable Vec greedy_expectation; 00248 00251 mutable Vec greedy_activation_gradient; 00252 00255 mutable Vec greedy_expectation_gradient; 00256 00258 mutable Vec reconstruction_activations; 00259 00261 mutable Vec reconstruction_activation_gradients; 00262 00264 mutable Vec reconstruction_expectation_gradients; 00265 00267 TVec< PP<RBMLayer> > greedy_layers; 00268 00270 TVec< PP<RBMConnection> > greedy_connections; 00271 00273 Vec similar_example_representation; 00274 00276 Vec dissimilar_example_representation; 00277 00279 mutable Vec input_representation; 00280 00282 Vec previous_input_representation; 00283 00285 Vec dissimilar_gradient_contribution; 00286 00288 Vec pos_down_val; 00290 Vec pos_up_val; 00292 Vec neg_down_val; 00294 Vec neg_up_val; 00295 00297 mutable Vec final_cost_input; 00299 mutable Vec final_cost_value; 00301 mutable Vec final_cost_gradient; 00302 00304 TVec< PP<ClassSubsetVMatrix> > class_datasets; 00305 00308 Mat other_classes_proportions; 00309 00311 TMat<int> nearest_neighbors_indices; 00312 00314 mutable TVec<int> test_nearest_neighbors_indices; 00315 00317 TVec<int> test_votes; 00318 00320 mutable Mat train_set_representations; 00321 mutable VMat train_set_representations_vmat; 00322 mutable TVec<int> train_set_targets; 00323 00325 mutable bool train_set_representations_up_to_date; 00326 00328 TVec<int> greedy_stages; 00329 00332 int currently_trained_layer; 00333 00335 PP<OnlineLearningModule> final_module; 00336 00338 PP<CostModule> final_cost; 00339 00340 protected: 00341 //##### Protected Member Functions ###################################### 00342 00344 static void declareOptions(OptionList& ol); 00345 00346 private: 00347 //##### Private Member Functions ######################################## 00348 00350 void build_(); 00351 00352 void build_layers_and_connections(); 00353 00354 void build_output_layer_and_cost(); 00355 00356 void setLearningRate( real the_learning_rate ); 00357 00358 private: 00359 //##### Private Data Members ############################################ 00360 00361 // The rest of the private stuff goes here 00362 }; 00363 00364 // Declares a few other classes and functions related to this class 00365 DECLARE_OBJECT_PTR(StackedFocusedAutoassociatorsNet); 00366 00367 } // end of namespace PLearn 00368 00369 #endif 00370 00371 00372 /* 00373 Local Variables: 00374 mode:c++ 00375 c-basic-offset:4 00376 c-file-style:"stroustrup" 00377 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00378 indent-tabs-mode:nil 00379 fill-column:79 00380 End: 00381 */ 00382 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :