PLearn 0.1
mNNet.h
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 // mNNet.h
00003 //
00004 // Copyright (C) 2007 Yoshua Bengio
00005 //
00006 // Redistribution and use in source and binary forms, with or without
00007 // modification, are permitted provided that the following conditions are met:
00008 //
00009 //  1. Redistributions of source code must retain the above copyright
00010 //     notice, this list of conditions and the following disclaimer.
00011 //
00012 //  2. Redistributions in binary form must reproduce the above copyright
00013 //     notice, this list of conditions and the following disclaimer in the
00014 //     documentation and/or other materials provided with the distribution.
00015 //
00016 //  3. The name of the authors may not be used to endorse or promote
00017 //     products derived from this software without specific prior written
00018 //     permission.
00019 //
00020 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
00021 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00022 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
00023 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00024 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00025 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00026 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00027 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00028 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00029 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00030 //
00031 // This file is part of the PLearn library. For more information on the PLearn
00032 // library, go to the PLearn Web site at www.plearn.org
00033 
00034 // Authors: Yoshua Bengio, PAM
00035 
00039 #ifndef mNNet_INC
00040 #define mNNet_INC
00041 
00042 #include <plearn_learners/generic/PLearner.h>
00043 
00044 namespace PLearn {
00045 
00049 class mNNet : public PLearner
00050 {
00051     typedef PLearner inherited;
00052 
00053 public:
00054     //#####  Public Build Options  ############################################
00055 
00057     int noutputs;
00058 
00060     TVec<int> hidden_layer_sizes;
00061 
00063     real init_lrate;
00064 
00066     real lrate_decay;
00067 
00069     int minibatch_size;
00070 
00072     string output_type;
00073 
00075     real output_layer_L1_penalty_factor;
00076 
00077 public:
00078     //#####  Public Not Build Options  ########################################
00079 
00080 public:
00081     //#####  Public Member Functions  #########################################
00082 
00083     mNNet();
00084 
00085 
00086     //#####  PLearner Member Functions  #######################################
00087 
00090     virtual int outputsize() const;
00091 
00095     virtual void forget();
00096 
00100     virtual void train();
00101 
00103     virtual void computeOutput(const Vec& input, Vec& output) const;
00104     virtual void computeOutputs(const Mat& input, Mat& output) const;
00105     virtual void computeOutputsAndCosts(const Mat& input, const Mat& target, 
00106                                         Mat& output, Mat& costs) const;
00107 
00109     virtual void computeCostsFromOutputs(const Vec& input, const Vec& output,
00110                                          const Vec& target, Vec& costs) const;
00111 
00114     virtual TVec<std::string> getTestCostNames() const;
00115 
00118     virtual TVec<std::string> getTrainCostNames() const;
00119 
00120     // *** SUBCLASS WRITING: ***
00121     // While in general not necessary, in case of particular needs
00122     // (efficiency concerns for ex) you may also want to overload
00123     // some of the following methods: computeOutputAndCosts(),
00124     // computeCostsOnly(), test(), nTestCosts(), nTrainCosts(),
00125     // resetInternalState(), isStatefulLearner()
00126 
00127     //#####  PLearn::Object Protocol  #########################################
00128 
00129     // Declares other standard object methods.
00130     // ### If your class is not instantiatable (it has pure virtual methods)
00131     // ### you should replace this by PLEARN_DECLARE_ABSTRACT_OBJECT_METHODS
00132     PLEARN_DECLARE_OBJECT(mNNet);
00133 
00134     // Simply calls inherited::build() then build_()
00135     virtual void build();
00136 
00138     // (PLEASE IMPLEMENT IN .cc)
00139     virtual void makeDeepCopyFromShallowCopy(CopiesMap& copies);
00140 
00141 protected:
00142     //#####  Protected Options  ###############################################
00143 
00144     // ### Declare protected option fields (such as learned parameters) here
00145 
00147     int n_layers;
00148 
00150     TVec<int> layer_sizes;
00151 
00153     Vec all_params;
00155     TVec<Mat> biases;
00156     TVec<Mat> weights;
00160     TVec<Mat> layer_params;
00161 
00163     Vec all_params_gradient; 
00164     TVec<Mat> layer_params_gradient;
00165 
00167     Mat neuron_gradients;   // one row per example of a minibatch, has
00168                             // concatenation of layer 0, layer 1, ... gradients.
00169     TVec<Mat> neuron_gradients_per_layer;   // pointing into neuron_gradients
00170                                             // (one row per example of a minibatch)
00171 
00173     mutable Mat neuron_extended_outputs;
00174     mutable TVec<Mat> neuron_extended_outputs_per_layer;    // with 1's in the
00175                                                             // first pseudo-neuron, for doing biases
00176     mutable TVec<Mat> neuron_outputs_per_layer;  
00177 
00178     Mat targets; // one target row per example in a minibatch
00179     Vec example_weights; // one element per example in a minibatch
00180     Mat train_costs; // one row per example in a minibatch
00181 
00183     real cumulative_training_time;
00184 
00185 protected:
00186     //#####  Protected Member Functions  ######################################
00187 
00189     static void declareOptions(OptionList& ol);
00190 
00192     virtual void onlineStep(int t, const Mat& targets, Mat& train_costs, Vec example_weights);
00193 
00196     virtual void fpropNet(int n_examples) const;
00197 
00200     virtual void fbpropLoss(const Mat& output, const Mat& target, const Vec& example_weights, Mat& train_costs) const;
00201 
00202     virtual void bpropUpdateNet(const int t);
00203     virtual void bpropNet(const int t);
00204     
00205     void l1regularizeOutputs();
00206 
00207 private:
00208     //#####  Private Member Functions  ########################################
00209 
00211     // (PLEASE IMPLEMENT IN .cc)
00212     void build_();
00213 
00214 private:
00215     //#####  Private Data Members  ############################################
00216 
00217 };
00218 
00219 // Declares a few other classes and functions related to this class
00220 DECLARE_OBJECT_PTR(mNNet);
00221 
00222 } // end of namespace PLearn
00223 
00224 #endif
00225 
00226 
00227 /*
00228   Local Variables:
00229   mode:c++
00230   c-basic-offset:4
00231   c-file-style:"stroustrup"
00232   c-file-offsets:((innamespace . 0)(inline-open . 0))
00233   indent-tabs-mode:nil
00234   fill-column:79
00235   End:
00236 */
00237 // 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