PLearn 0.1
DeepReconstructorNet.h
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // DeepReconstructorNet.h
00004 //
00005 // Copyright (C) 2007 Simon Lemieux, Pascal Vincent
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: Simon Lemieux, Pascal Vincent
00036 
00040 #ifndef DeepReconstructorNet_INC
00041 #define DeepReconstructorNet_INC
00042 
00043 #include <plearn_learners/generic/PLearner.h>
00044 #include <plearn/var/Variable.h>
00045 #include <plearn/opt/Optimizer.h>
00046 #include <plearn/var/SumOfVariable.h>
00047 #include <plearn/vmat/FileVMatrix.h>
00048 #include <plearn/var/SourceVariable.h>
00049 
00050 namespace PLearn {
00051 
00062 class DeepReconstructorNet : public PLearner
00063 {
00064     typedef PLearner inherited;
00065 
00066 public:
00067     //#####  Public Build Options  ############################################
00068 
00071     
00072     TVec< pair<int,int> > unsupervised_nepochs;
00073     Vec unsupervised_min_improvement_rate;
00074 
00075     pair<int,int> supervised_nepochs;    
00076     real supervised_min_improvement_rate;
00077 
00078     // layers[0] is the input variable
00079     // last layer is final output layer
00080     VarArray layers;
00081 
00082     // reconstruction_costs[k] is the reconstruction cost for layers[k]
00083     VarArray reconstruction_costs;
00084     
00085     // The names to be given to each of the elements of a vector cost 
00086     TVec<string> reconstruction_costs_names;
00087 
00088     // reconstructed_layers[k] is the reconstruction of layer k from layers[k+1]
00089     VarArray reconstructed_layers;
00090 
00091     // hidden_for_reconstruction[k] is the hidden representation used to reconstruct reconstructed_layers[k] 
00092     // i.e. it is the representation at layer k+1 but possibly obtained from a corrupted input (contrary to layers[k+1]).
00093     VarArray hidden_for_reconstruction;
00094 
00095     // optimizers if we use different ones for each layer
00096     TVec< PP<Optimizer> > reconstruction_optimizers;
00097     
00098     // if we use always the same optimizer
00099     PP<Optimizer> reconstruction_optimizer;
00100 
00101 
00102     Var target;
00103     //TVec<Var> supervised_costs;
00104     VarArray supervised_costs;
00105     Var supervised_costvec; // hconcat(supervised_costs)
00106 
00107     TVec<string> supervised_costs_names;
00108 
00109     Var fullcost;
00110     
00111     VarArray parameters;
00112 
00113     int minibatch_size;
00114 
00115 
00116     PP<Optimizer> supervised_optimizer;
00117 
00118     PP<Optimizer> fine_tuning_optimizer;
00119 
00120 
00121     TVec<int> group_sizes;
00122 
00123 protected:
00124     // protected members (not options)
00125 
00126     TVec<Func> compute_layer;
00127     Func compute_output;
00128     Func output_and_target_to_cost;
00129     TVec<VMat> outmat;
00130     
00131 
00132 public:
00133     //#####  Public Member Functions  #########################################
00134 
00136     // ### Make sure the implementation in the .cc
00137     // ### initializes all fields to reasonable default values.
00138     DeepReconstructorNet();
00139 
00140 
00141     //#####  PLearner Member Functions  #######################################
00142 
00145     // (PLEASE IMPLEMENT IN .cc)
00146     virtual int outputsize() const;
00147 
00151     // (PLEASE IMPLEMENT IN .cc)
00152     virtual void forget();
00153 
00157     // (PLEASE IMPLEMENT IN .cc)
00158     virtual void train();
00159 
00161     // (PLEASE IMPLEMENT IN .cc)
00162     virtual void computeOutput(const Vec& input, Vec& output) const;
00163 
00165     // (PLEASE IMPLEMENT IN .cc)
00166     virtual void computeCostsFromOutputs(const Vec& input, const Vec& output,
00167                                          const Vec& target, Vec& costs) const;
00168 
00171     // (PLEASE IMPLEMENT IN .cc)
00172     virtual TVec<std::string> getTestCostNames() const;
00173 
00176     // (PLEASE IMPLEMENT IN .cc)
00177     virtual TVec<std::string> getTrainCostNames() const;
00178 
00179 
00180     virtual void initializeParams(bool set_seed=true);
00181 
00183     Mat getParameterValue(const string& varname);
00184 
00186     Vec getParameterRow(const string& varname, int n);
00187 
00189     TVec<string> listParameterNames();
00190 
00192     TVec<Mat> listParameter();
00193 
00194     void prepareForFineTuning();
00195     void fineTuningFor1Epoch();
00196     // void fineTuningFullOld();
00197 
00198     void trainSupervisedLayer(VMat inputs, VMat targets);
00199 
00200     TVec<Mat> computeRepresentations(Mat input);
00201     void reconstructInputFromLayer(int layer);
00202     TVec<Mat> computeReconstructions(Mat input);
00203 
00204     Mat getMatValue(int layer);
00205     void setMatValue(int layer, Mat values);
00206     Mat fpropOneLayer(int layer);
00207     Mat reconstructOneLayer(int layer);
00208        
00209     void computeAndSaveLayerActivationStats(VMat dataset, int which_layer, const string& filebasename, int nfirstunits=10, int notherunits=10);
00210 
00211     // *** SUBCLASS WRITING: ***
00212     // While in general not necessary, in case of particular needs
00213     // (efficiency concerns for ex) you may also want to overload
00214     // some of the following methods:
00215     // virtual void computeOutputAndCosts(const Vec& input, const Vec& target,
00216     //                                    Vec& output, Vec& costs) const;
00217     // virtual void computeCostsOnly(const Vec& input, const Vec& target,
00218     //                               Vec& costs) const;
00219     // virtual void test(VMat testset, PP<VecStatsCollector> test_stats,
00220     //                   VMat testoutputs=0, VMat testcosts=0) const;
00221     // virtual int nTestCosts() const;
00222     // virtual int nTrainCosts() const;
00223     // virtual void resetInternalState();
00224     // virtual bool isStatefulLearner() const;
00225 
00226 
00227     //#####  PLearn::Object Protocol  #########################################
00228 
00229     // Declares other standard object methods.
00230     // ### If your class is not instantiatable (it has pure virtual methods)
00231     // ### you should replace this by PLEARN_DECLARE_ABSTRACT_OBJECT_METHODS
00232     PLEARN_DECLARE_OBJECT(DeepReconstructorNet);
00233 
00234     // Simply calls inherited::build() then build_()
00235     virtual void build();
00236 
00238     // (PLEASE IMPLEMENT IN .cc)
00239     virtual void makeDeepCopyFromShallowCopy(CopiesMap& copies);
00240 
00241 protected:
00242     //#####  Protected Options  ###############################################
00243 
00244     // ### Declare protected option fields (such as learned parameters) here
00245     // ...
00246 
00247 protected:
00248     //#####  Protected Member Functions  ######################################
00249 
00251     // (PLEASE IMPLEMENT IN .cc)
00252     static void declareOptions(OptionList& ol);
00253     static void declareMethods(RemoteMethodMap& rmm);
00254 
00255     void trainHiddenLayer(int which_input_layer, VMat inputs);
00256     void buildHiddenLayerOutputs(int which_input_layer, VMat inputs, VMat outputs);
00257 
00258 private:
00259     //#####  Private Member Functions  ########################################
00260 
00262     // (PLEASE IMPLEMENT IN .cc)
00263     void build_();
00264 
00265 private:
00266     //#####  Private Data Members  ############################################
00267 
00268     // The rest of the private stuff goes here
00269     int nout;
00270 
00271 };
00272 
00273 // Declares a few other classes and functions related to this class
00274 DECLARE_OBJECT_PTR(DeepReconstructorNet);
00275 
00276 } // end of namespace PLearn
00277 
00278 #endif
00279 
00280 
00281 /*
00282   Local Variables:
00283   mode:c++
00284   c-basic-offset:4
00285   c-file-style:"stroustrup"
00286   c-file-offsets:((innamespace . 0)(inline-open . 0))
00287   indent-tabs-mode:nil
00288   fill-column:79
00289   End:
00290 */
00291 // 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