PLearn 0.1
DynamicallyLinkedRBMsModel.h
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // DynamicallyLinkedRBMsModel.h
00004 //
00005 // Copyright (C) 2006 Stanislas Lauly
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: Stanislas Lauly
00036 
00041 #ifndef DynamicallyLinkedRBMsModel_INC
00042 #define DynamicallyLinkedRBMsModel_INC
00043 
00044 #include <plearn_learners/generic/PLearner.h>
00045 #include <plearn_learners/online/OnlineLearningModule.h>
00046 #include <plearn_learners/online/RBMClassificationModule.h>
00047 #include <plearn_learners/online/RBMLayer.h>
00048 #include <plearn_learners/online/RBMMixedLayer.h>
00049 #include <plearn_learners/online/RBMConnection.h>
00050 #include <plearn_learners/online/RBMMatrixConnection.h>
00051 #include <plearn/vmat/AutoVMatrix.h>
00052 #include <plearn_learners/online/RBMMatrixTransposeConnection.h>
00053 
00054 #include <plearn_learners/online/GradNNetLayerModule.h>
00055 
00056 namespace PLearn {
00057 
00061 class DynamicallyLinkedRBMsModel : public PLearner
00062 {
00063     typedef PLearner inherited;
00064 
00065 public:
00066     //#####  Public Build Options  ############################################
00067 
00069     //real rbm_learning_rate;
00070 
00072     real recurrent_net_learning_rate;
00073 
00075     //int rbm_nstages;
00076 
00078     Vec target_layers_weights;
00079     
00082     bool use_target_layers_masks;
00083 
00085     real end_of_sequence_symbol;
00086 
00088     PP<RBMLayer> input_layer;
00089 
00091     TVec< PP<RBMLayer> > target_layers;
00092 
00094     PP<RBMLayer> hidden_layer;
00095 
00097     PP<RBMLayer> hidden_layer2;
00098 
00100     PP<RBMConnection> dynamic_connections;
00101 
00103     PP<RBMConnection> hidden_connections;
00104 
00106     PP<RBMConnection> input_connections;
00107 
00109     TVec< PP<RBMConnection> > target_connections;
00110 
00111     //#####  Public Learnt Options  ###########################################
00112 
00115     TVec<int> target_layers_n_of_target_elements;
00116 
00118     TVec<int> input_symbol_sizes;
00119     
00121     TVec< TVec<int> > target_symbol_sizes;
00122 
00123     
00124     
00125     //#####  Not Options  #####################################################
00126 
00127 
00128 public:
00129     //#####  Public Member Functions  #########################################
00130 
00132     DynamicallyLinkedRBMsModel();
00133 
00134 
00135     //#####  PLearner Member Functions  #######################################
00136 
00139     virtual int outputsize() const;
00140 
00144     virtual void forget();
00145 
00149     virtual void train();
00150 
00152     void setLearningRate( real the_learning_rate );
00153 
00155     virtual void computeOutput(const Vec& input, Vec& output) const;
00156 
00158     virtual void computeCostsFromOutputs(const Vec& input, const Vec& output,
00159                                          const Vec& target, Vec& costs) const;
00160 
00163     virtual TVec<std::string> getTestCostNames() const;
00164 
00165     
00166 
00167 //    //! Generate music in a folder
00168     void generate(int t, int n);
00169 //
00170 //    //! Generate a part of the data in a folder
00171 //    void gen();
00172 
00175     virtual TVec<std::string> getTrainCostNames() const;
00176 
00178     void partition(TVec<double> part, TVec<double> periode, TVec<double> vel ) const;
00179     
00181     void clamp_units(const Vec layer_vector, PP<RBMLayer> layer,
00182                      TVec<int> symbol_sizes) const;
00183 
00186     void clamp_units(const Vec layer_vector, PP<RBMLayer> layer,
00187                      TVec<int> symbol_sizes, const Vec original_mask,
00188                      Vec &formated_mask) const;
00189     
00193     void recurrent_update();
00194 
00195     virtual void test(VMat testset, PP<VecStatsCollector> test_stats,
00196                       VMat testoutputs=0, VMat testcosts=0) const;
00197 
00198     
00199 
00200 
00201     // *** SUBCLASS WRITING: ***
00202     // While in general not necessary, in case of particular needs
00203     // (efficiency concerns for ex) you may also want to overload
00204     // some of the following methods:
00205     // virtual void computeOutputAndCosts(const Vec& input, const Vec& target,
00206     //                                    Vec& output, Vec& costs) const;
00207     // virtual void computeCostsOnly(const Vec& input, const Vec& target,
00208     //                               Vec& costs) const;
00209     // virtual int nTestCosts() const;
00210     // virtual int nTrainCosts() const;
00211     // virtual void resetInternalState();
00212     // virtual bool isStatefulLearner() const;
00213 
00214 
00215     //#####  PLearn::Object Protocol  #########################################
00216 
00217     // Declares other standard object methods.
00218     PLEARN_DECLARE_OBJECT(DynamicallyLinkedRBMsModel);
00219 
00220     // Simply calls inherited::build() then build_()
00221     virtual void build();
00222 
00224     virtual void makeDeepCopyFromShallowCopy(CopiesMap& copies);
00225 
00226 protected:
00227     //#####  Not Options  #####################################################
00228 
00229 
00231     AutoVMatrix*  data;
00232    
00234     mutable Vec bias_gradient;
00235     
00237     mutable Vec visi_bias_gradient;
00238 
00240     mutable Vec hidden_gradient;
00241     
00243     mutable Vec hidden_temporal_gradient;
00244         
00246     mutable TVec< Vec > hidden_list;
00247     mutable TVec< Vec > hidden_act_no_bias_list;
00248 
00250     mutable TVec< Vec > hidden2_list;
00251     mutable TVec< Vec > hidden2_act_no_bias_list;
00252 
00254     mutable TVec< TVec< Vec > > target_prediction_list;
00255     mutable TVec< TVec< Vec > > target_prediction_act_no_bias_list;
00256 
00258     mutable TVec< Vec > input_list;
00259 
00261     mutable TVec< TVec< Vec > > targets_list;
00262 
00264     mutable Mat nll_list;
00265 
00267     mutable TVec< TVec< Vec > > masks_list;
00268 
00270     mutable Vec dynamic_act_no_bias_contribution;
00271 
00272 protected:
00273     //#####  Protected Member Functions  ######################################
00274 
00276     static void declareOptions(OptionList& ol);
00277 
00278 private:
00279     //#####  Private Member Functions  ########################################
00280 
00282     void build_();
00283 
00284 private:
00285     //#####  Private Data Members  ############################################
00286 
00287     // The rest of the private stuff goes here
00288 };
00289 
00290 // Declares a few other classes and functions related to this class
00291 DECLARE_OBJECT_PTR(DynamicallyLinkedRBMsModel);
00292 
00293 } // end of namespace PLearn
00294 
00295 #endif
00296 
00297 
00298 /*
00299   Local Variables:
00300   mode:c++
00301   c-basic-offset:4
00302   c-file-style:"stroustrup"
00303   c-file-offsets:((innamespace . 0)(inline-open . 0))
00304   indent-tabs-mode:nil
00305   fill-column:79
00306   End:
00307 */
00308 // 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