PLearn 0.1
DeepFeatureExtractorNNet.h
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // DeepFeatureExtractorNNet.h
00004 //
00005 // Copyright (C) 2006 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 /* *******************************************************      
00036    * $Id: .pyskeleton_header 544 2003-09-01 00:05:31Z plearner $ 
00037    ******************************************************* */
00038 
00039 // Authors: Hugo Larochelle
00040 
00044 #ifndef DeepFeatureExtractorNNet_INC
00045 #define DeepFeatureExtractorNNet_INC
00046 
00047 #include <plearn_learners/generic/PLearner.h>
00048 #include <plearn/vmat/AppendNeighborsVMatrix.h>
00049 #include <plearn/opt/Optimizer.h>
00050 #include <plearn/var/VarArray.h>
00051 
00052 namespace PLearn {
00053 
00061 class DeepFeatureExtractorNNet : public PLearner
00062 {
00063     typedef PLearner inherited;
00064 
00065 public:
00066     //#####  Public Build Options  ############################################
00067 
00069     TVec<int> nhidden_schedule;
00071     PP<Optimizer> optimizer;
00075     PP<Optimizer> optimizer_supervised;
00077     int batch_size;
00079     int batch_size_supervised;
00081     string hidden_transfer_func;
00083     string output_transfer_func;
00085     int nhidden_schedule_position;
00087     TVec<string> cost_funcs;
00089     real weight_decay; 
00091     real bias_decay;   
00093     string penalty_type;
00096     real classification_regularizer; 
00099     real regularizer; 
00102     real margin;
00104     string initialization_method;
00106     Vec paramsvalues; 
00108     int noutputs;
00111     bool use_same_input_and_output_weights;
00115     bool always_reconstruct_input;
00118     bool use_activations_with_cubed_input;
00120     int use_n_first_as_supervised;
00122     bool use_only_supervised_part;
00127     real relative_minimum_improvement;
00133     string input_reconstruction_error;
00138     real autoassociator_regularisation_weight;
00145     real supervised_signal_weight;
00147     int k_nearest_neighbors_reconstruction;
00148 
00149 public:
00150     //#####  Public Member Functions  #########################################
00151 
00153     DeepFeatureExtractorNNet();
00154 
00155 
00156     //#####  PLearner Member Functions  #######################################
00157 
00160     virtual int outputsize() const;
00161 
00165     virtual void forget();
00166     
00170     virtual void train();
00171 
00173     virtual void computeOutput(const Vec& input, Vec& output) const;
00174 
00176     virtual void computeCostsFromOutputs(const Vec& input, const Vec& output, 
00177                                          const Vec& target, Vec& costs) const;
00178     
00181     virtual TVec<std::string> getTestCostNames() const;
00182 
00185     virtual TVec<std::string> getTrainCostNames() const;
00186 
00187 
00188     // *** SUBCLASS WRITING: ***
00189     // While in general not necessary, in case of particular needs 
00190     // (efficiency concerns for ex) you may also want to overload
00191     // some of the following methods:
00192     virtual void computeOutputAndCosts(const Vec& input, const Vec& target, Vec& output, Vec& costs) const;
00193     // virtual void computeCostsOnly(const Vec& input, const Vec& target, Vec& costs) const;
00194     // virtual void test(VMat testset, PP<VecStatsCollector> test_stats, VMat testoutputs=0, VMat testcosts=0) const;
00195     // virtual int nTestCosts() const;
00196     // virtual int nTrainCosts() const;
00197     // virtual void resetInternalState();
00198     // virtual bool isStatefulLearner() const;
00199 
00200     
00201     //#####  PLearn::Object Protocol  #########################################
00202 
00203     // Declares other standard object methods.
00204     PLEARN_DECLARE_OBJECT(DeepFeatureExtractorNNet);
00205 
00206     // Simply calls inherited::build() then build_() 
00207     virtual void build();
00208 
00210     virtual void makeDeepCopyFromShallowCopy(CopiesMap& copies);
00211 
00212 protected:
00213     //#####  Protected Options  ###############################################
00214 
00220     int nhidden_schedule_current_position;
00222     VarArray params;
00224     VarArray params_to_train;
00226     VarArray weights;
00228     VarArray reconstruction_weights;
00230     VarArray biases;
00232     VarArray invars;
00234     Var input;
00236     Var output;
00238     Var feature_vector;
00240     Var hidden_representation;
00242     Var neighbor_indices;
00244     Var target;
00246     Var unsupervised_target;
00248     Var sampleweight;
00250     VarArray costs; 
00252     VarArray penalties;
00254     Var training_cost; 
00256     Var test_costs;
00258     VMat sup_train_set;
00260     VMat unsup_train_set;
00262     PP<AppendNeighborsVMatrix> knn_train_set;
00263 
00265     mutable Func f; 
00267     mutable Func test_costf; 
00269     mutable Func output_and_target_to_cost; 
00271     mutable Func to_feature_vector;
00274     TVec<VarArray> autoassociator_params;
00277     VarArray autoassociator_training_costs;
00278 
00279 protected:
00280     //#####  Protected Member Functions  ######################################
00281     
00283     static void declareOptions(OptionList& ol);
00284 
00288     Var hiddenLayer(const Var& input, const Var& weights, string transfer_func, 
00289                     Var& before_transfer_function, bool use_cubed_value=false);
00290 
00294     Var hiddenLayer(const Var& input, const Var& weights, const Var& bias, 
00295                     bool transpose_weights, string transfer_func, 
00296                     Var& before_transfer_function, bool use_cubed_value=false);
00297 
00302     void buildOutputFromInput(const Var& the_input, Var& hidden_layer, 
00303                               Var& before_transfer_func);
00304 
00306     void buildTargetAndWeight();
00307 
00309     void buildCosts(const Var& output, const Var& target, 
00310                     const Var& unsupervised_target, 
00311                     const Var& before_transfer_func, const Var& output_sup);
00312 
00314     void buildFuncs(const Var& the_input, const Var& the_output, 
00315                     const Var& the_target, const Var& the_sampleweight);
00316 
00321     void fillWeights(const Var& weights, bool fill_first_row, 
00322                      real fill_with_this=0);
00323 
00325     virtual void buildPenalties();
00326 
00327 private: 
00328     //#####  Private Member Functions  ########################################
00329 
00331     void build_();
00332 
00333 private:
00334     //#####  Private Data Members  ############################################
00335 
00336     // The rest of the private stuff goes here
00337 };
00338 
00339 // Declares a few other classes and functions related to this class
00340 DECLARE_OBJECT_PTR(DeepFeatureExtractorNNet);
00341   
00342 } // end of namespace PLearn
00343 
00344 #endif
00345 
00346 
00347 /*
00348   Local Variables:
00349   mode:c++
00350   c-basic-offset:4
00351   c-file-style:"stroustrup"
00352   c-file-offsets:((innamespace . 0)(inline-open . 0))
00353   indent-tabs-mode:nil
00354   fill-column:79
00355   End:
00356 */
00357 // 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