PLearn 0.1
StructuralLearner.h
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // StructuralLearner.h
00004 //
00005 // Copyright (C) 2006 Pierre-Antoine Manzagol 
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: Pierre-Antoine Manzagol
00040 
00044 #ifndef StructuralLearner_INC
00045 #define StructuralLearner_INC
00046 
00047 #include <plearn_learners/generic/PLearner.h>
00048 
00049 namespace PLearn {
00050 
00054 class StructuralLearner : public PLearner
00055 {
00056     typedef PLearner inherited;
00057 
00058 public:
00059     //#####  Public Build Options  ############################################
00060 
00061     real start_learning_rate, decrease_constant;
00062     VMat auxiliary_task_train_set;
00063     real lambda;
00064     real abstention_threshold;
00065     real epsilon;
00066     int index_O;
00067     int nhidden;
00068     int n_auxiliary_wordproblems;
00069     bool separate_features;
00070     int max_stage;
00071     bool use_thetas_for_output_weights;
00072     bool use_thetas_for_hidden_weights;
00073 
00074 public:
00075     //#####  Public Member Functions  #########################################
00076 
00078     StructuralLearner();
00079 
00080 
00081     //#####  PLearner Member Functions  #######################################
00082 
00085     virtual int outputsize() const;
00086 
00090     virtual void forget();
00091     
00095     virtual void train();
00096 
00098     virtual void computeOutput(const Vec& input, Vec& output) const;
00099 
00101     virtual void computeCostsFromOutputs(const Vec& input, const Vec& output, 
00102                                          const Vec& target, Vec& costs) const;
00103     
00106     virtual TVec<std::string> getTestCostNames() const;
00107 
00110     virtual TVec<std::string> getTrainCostNames() const;
00111 
00112     virtual void computeOutputWithFeatures(TVec<TVec<unsigned int> >& feats, Vec& output, bool use_theta=true, int begin_class = -1, int end_class = -1) const;
00113 
00114     void computeFeatures(const Vec& input, const Vec& target, int data_set, int
00115 index, TVec< TVec<unsigned int> >& theFeatureGroups, char featureMask = 0x1F) const;
00116 
00117 void updateFeatures(const Vec& input, const Vec& target,  TVec< TVec<unsigned int> >& theFeatureGroups, char
00118 featureMask = 0x1F) const;
00119 
00120 
00121     //virtual void updateDynamicFeatures(hash_map<int, TVec<bool> > token_prediction, int token, int prediction);
00122         
00123     virtual void test(VMat testset, PP<VecStatsCollector> test_stats, VMat testoutputs=0, VMat testcosts=0) const;
00124 
00125     // *** SUBCLASS WRITING: ***
00126     // While in general not necessary, in case of particular needs 
00127     // (efficiency concerns for ex) you may also want to overload
00128     // some of the following methods:
00129     // virtual void computeOutputAndCosts(const Vec& input, const Vec& target, Vec& output, Vec& costs) const;
00130     // virtual void computeCostsOnly(const Vec& input, const Vec& target, Vec& costs) const;
00131     // virtual int nTestCosts() const;
00132     // virtual int nTrainCosts() const;
00133     // virtual void resetInternalState();
00134     // virtual bool isStatefulLearner() const;
00135 
00136     
00137     //#####  PLearn::Object Protocol  #########################################
00138 
00139     // Declares other standard object methods.
00140     // ### If your class is not instantiatable (it has pure virtual methods)
00141     // ### you should replace this by PLEARN_DECLARE_ABSTRACT_OBJECT_METHODS 
00142     PLEARN_DECLARE_OBJECT(StructuralLearner);
00143 
00144     // Simply calls inherited::build() then build_() 
00145     virtual void build();
00146 
00148     // (PLEASE IMPLEMENT IN .cc)
00149     virtual void makeDeepCopyFromShallowCopy(CopiesMap& copies);
00150 
00151 protected:
00152     //#####  Protected Options  ###############################################
00153 
00154     mutable Mat activations_gradient;
00155     mutable Vec good_class_softmax_gradient;
00156     mutable Vec bad_class_softmax_gradient;
00157     mutable TVec<Mat> vs_times_thetas;
00158 
00159     //int ninputs_onehot; // 
00160 
00161     // For the model
00162     mutable TVec<Mat> ws, vs, thetas;
00163     mutable TVec<Mat> whids, vhids, thetahids;
00164 
00165     // Features for an example
00166     mutable TVec< TVec<unsigned int> > feats;
00167     mutable unsigned int *current_features;
00168 
00169     //hash_map<int,TVec<bool> > token_prediction_train;
00170     //TVec<hash_map<int,TVec<bool> > > token_prediction_test;
00171 
00172     mutable Mat thetas_times_x;
00173     mutable Mat thetahids_times_x;
00174     mutable Mat activations;
00175 
00176     // Bag of words features, over window of chunks, precomputed for
00177     // the training set
00178     //TVec< TVec<unsigned int> > bag_of_words_over_chunks;
00179 
00180     // For examples
00181     mutable Vec input, target, before_softmax, output, costs;
00182     mutable real weight;
00183     real learning_rate;
00184 
00185     // Temporary files for computeFeatures
00186     mutable TVec<unsigned int> currentFeatureGroup;
00187     mutable bool tag;
00188     mutable int size;
00189     mutable unsigned int fl;
00190     mutable std::string symbol;
00191 
00192     // Feature dimensions
00193     mutable TVec<unsigned int> fls;
00194 
00195     // Indices of auxiliary examples effectively used and their target ("most frequent word"-tag encoded, ie between 0 and 999)
00196     TMat< unsigned int > auxiliary_indices_current;
00197     TMat< unsigned int > auxiliary_indices_left;
00198     
00199     // Viterbi table
00200     mutable TMat< pair<real,int> > viterbi_table;
00201     Vec preds;
00202 
00203     mutable std::map<int, int> plcw_bigram_mapping;   // maps "previous label - current word" bigrams seen in train_set to an index
00204 
00205     real best_error,current_error;
00206 
00207 protected:
00208     //#####  Protected Member Functions  ######################################
00209     
00211     // (PLEASE IMPLEMENT IN .cc)
00212     static void declareOptions(OptionList& ol);
00213 
00217     virtual void initializeParams(bool set_seed = true);
00218 
00221     void initWordProblemsStructures();
00222 
00224     void initPreviousLabelCurrentWordBigramMapping();
00225 
00226     void buildTasksParameters(int nout, TVec<unsigned int> feat_lengths);
00227     void buildThetaParameters(TVec<unsigned int> feat_lengths);
00228 
00229 
00230 private: 
00231     //#####  Private Member Functions  ########################################
00232 
00234     // (PLEASE IMPLEMENT IN .cc)
00235     void build_();
00236 
00237 private:
00238     //#####  Private Data Members  ############################################
00239 
00240     // The rest of the private stuff goes here
00241 };
00242 
00243 // Declares a few other classes and functions related to this class
00244 DECLARE_OBJECT_PTR(StructuralLearner);
00245   
00246 } // end of namespace PLearn
00247 
00248 #endif
00249 
00250 
00251 /*
00252   Local Variables:
00253   mode:c++
00254   c-basic-offset:4
00255   c-file-style:"stroustrup"
00256   c-file-offsets:((innamespace . 0)(inline-open . 0))
00257   indent-tabs-mode:nil
00258   fill-column:79
00259   End:
00260 */
00261 // 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