PLearn 0.1
EntropyContrast.h
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // EntropyContrast.h
00004 //
00005 // Copyright (C) 2004  Dan Popovici
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: EntropyContrast.h 3994 2005-08-25 13:35:03Z chapados $ 
00037  ******************************************************* */
00038 
00040 #ifndef EntropyContrast_INC
00041 #define EntropyContrast_INC
00042 #define PROB_TABLE_LIMIT 10000000
00043 
00044 #include <plearn/base/stringutils.h>
00045 #include <plearn_learners/generic/PLearner.h>
00046 
00047 //Continuous includes
00048 
00049 // Discrete includes
00050 #define MY_PRECISION 0.0000000000001
00051 #define MY_LOG_PRECISION 0.0001
00052 #include <plearn/var/TransposeProductVariable.h>
00053 #include <plearn/var/ConcatColumnsVariable.h>
00054 #include <plearn/var/VarRowVariable.h>
00055 #include <plearn/math/random.h>
00056 
00057 namespace PLearn {
00058 using namespace std;
00059 
00060 class EntropyContrast: public PLearner
00061 {
00062 
00063 private:
00064 //COMMON
00065     typedef PLearner inherited;
00066     int n ; // an alias for the inputsize() 
00067     int evaluate_every_n_epochs; // number of epochs after which evaluation of constraints must be done
00068     bool evaluate_first_epoch;
00069 
00070     Mat w;  // weigths from hidden to constraints 
00071     Mat v;  // weigths from input to hidden
00072 
00073   
00074     Vec x ;
00075     Vec f_x ; // the constraints 
00076     Vec grad_C_real_wrt_f_x;
00077 
00078     Vec grad_C_extra_cost_wrt_f_x;
00079 
00080     Vec x_hat;
00081     Vec f_x_hat ;
00082     Vec grad_C_generated_wrt_f_x_hat; 
00083 
00084     VMat test_set;
00085     VMat validation_set;
00086 
00087     // Continuous fields
00088 
00089     real alpha ; // parameter used, when computing the running mu & sigma
00090                  // will be changed at every t
00091 
00092     int nhidden; 
00093 
00094     Vec mu_f; // mean of the constraints , updated when a new f(x) is computed 
00095     Vec sigma_f ; // std dev of the constriants 
00096   
00097     Vec mu_f_hat ; //  mean of the constraints , updated when a new f(x_hat) is computed 
00098     Vec sigma_f_hat ; // std dev of the predicated constriants
00099 
00100     Vec mu_f_square ; 
00101     Vec sigma_f_square ;
00102     Mat grad_H_f_x_wrt_w;
00103     Mat grad_H_f_x_hat_wrt_w ;
00104     Mat grad_H_f_x_wrt_v;
00105     Mat grad_H_f_x_hat_wrt_v ;
00106 
00107     Vec grad_H_f_x_wrt_bias_output ; 
00108     Vec grad_H_f_x_wrt_bias_hidden ; 
00109     
00110     Vec grad_H_f_x_hat_wrt_bias_output ; 
00111     Vec grad_H_f_x_hat_wrt_bias_hidden ;
00112         
00113     Mat grad_H_g_wrt_w ; 
00114         
00115     Vec sigma_g ; 
00116     Vec mu_g ; 
00117     Vec g_x; 
00118 
00119     Vec bias_hidden ; 
00120     Vec z_x; // the hidden units when x is presented 
00121     
00122     Vec z_x_hat ; // the hidden_units when x_hat is presented
00123 
00124     
00125     Vec bias_output ; 
00126 
00127     Vec full_sum ; // used in the computation of grad_H_g_wrt_w ;
00128 
00129     real full ; 
00130 
00131     // constr_extra == derivative
00132     Mat df_dx ; 
00133     Mat grad_C_wrt_df_dx ; 
00134     Mat grad_extra_wrt_w, grad_extra_wrt_v ; 
00135     Vec grad_extra_wrt_bias_hidden , grad_extra_wrt_bias_output ; 
00136     
00137     // Discrete fields
00138 
00139     // Class fields
00140 
00141 public:
00142   
00143     // ************************
00144     // * public build options *
00145     // ************************
00146 
00147     string cost_real ; 
00148     string cost_gen ; 
00149     string cost_extra; 
00150     string gen_method;
00151     string evaluation_method;
00152 
00153     int nconstraints; 
00154 
00155     int inputsize ; // the number of samples in the input 
00156 
00157     real learning_rate ; 
00158     real decay_factor ; 
00159     real weight_real , weight_gen, weight_extra ;
00160     real weight_decay_output;
00161     real weight_decay_hidden;
00162     int n_seen_examples;
00163     real starting_learning_rate;
00164     // saved options
00165 
00166     // ****************
00167     // * Constructors *
00168     // ****************
00169 
00170     // Default constructor, make sure the implementation in the .cc
00171     // initializes all fields to reasonable default values.
00172     EntropyContrast();
00173     // ******************
00174     // * PLearner methods *
00175     // ******************
00176 
00177 private: 
00179     // (Please implement in .cc)
00180     void build_();
00181     string getInfo()
00182     {
00183         time_t tt;
00184         time(&tt);
00185         string time_str(ctime(&tt));
00186         vector<string> tokens = split(time_str);
00187         string info = tokens[3];
00188         info += "> ";
00189         return info;
00190     }
00191 
00192     // Continuous functions
00193     void initialize_NNcontinuous();
00194     void update_NNcontinuous();
00195     void computeNNcontinuous_hidden(const Vec& input_units,Vec &hidden_units);
00196     void computeNNcontinuous_constraints(Vec& hidden_units,Vec &output_units);
00197     void get_NNcontinuous_output(const Vec & x , Vec & f_x, Vec & z_x);
00198     void update_mu_sigma_f(const Vec &  f_x,Vec & mu,Vec & sigma) ;
00199     void update_alpha(int stage,int current_input_index) ; 
00200     void compute_diversity_cost(const Vec & f_x,const Vec & cost,Vec & grad_C_extra_cost_wrt_f_x );
00201     void get_grad_log_variance_wrt_f(Vec & grad, const Vec& f_x, const Vec& mu, const Vec& sigma); 
00202     void set_NNcontinuous_gradient(Vec &grad_C_real_wrt_f_x,Mat &grad_H_f_x_wrt_w, Mat &grad_H_f_x_wrt_v, Vec &z_x, Vec &x,
00203                                    Vec &grad_H_f_x_wrt_bias_output, Vec &grad_H_f_x_wrt_bias_hidden);
00204     void gen_normal_0_1(Vec &output) ;
00205     void set_NNcontinuous_gradient_from_extra_cost(Mat &grad_C_wrt_df_dx,const Vec &input );   // TODO:fill in the needed parameters
00206     void compute_df_dx(Mat &df_dx,const Vec & input) ; 
00207     void compute_extra_grad_wrt_df_dx(Mat &grad_C_wrt_df_dx) ; 
00208     void update_NNcontinuous_from_extra_cost() ; 
00209     // Discrete functions
00210 
00211 
00212 protected: 
00214     // (Please implement in .cc)
00215     static void declareOptions(OptionList& ol);
00216 
00217 public:
00218 
00219     // ************************
00220     // **** Object methods ****
00221     // ************************
00222 
00224     virtual void build();
00225 
00227     virtual void makeDeepCopyFromShallowCopy(CopiesMap& copies);
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(EntropyContrast);
00233 
00234 
00235     // **************************
00236     // **** PLearner methods ****
00237     // **************************
00238 
00239 
00242     // (PLEASE IMPLEMENT IN .cc)
00243     virtual int outputsize() const;
00244 
00247     // (PLEASE IMPLEMENT IN .cc)
00248     virtual void forget();
00249 
00250     
00253     // (PLEASE IMPLEMENT IN .cc)
00254     virtual void train();
00255 
00256 //    virtual Vec test(VMat test_set, const string& save_test_outputs, const string& save_test_costs);
00257 
00259     virtual void computeOutput(const Vec& input, Vec& output) const;
00260 
00262     void reconstruct(const Vec& output, Vec& input) const;
00263   
00266     virtual void computeCostsFromOutputs(const Vec& input, const Vec& output, 
00267                                          const Vec& target, Vec& costs) const;
00268                                 
00269 
00271     virtual TVec<string> getTestCostNames() const;
00272 
00273 
00275     virtual TVec<string> getTrainCostNames() const;
00276 
00277 
00278 };
00279 
00280 // Declares a few other classes and functions related to this class
00281 DECLARE_OBJECT_PTR(EntropyContrast);
00282   
00283 } // end of namespace PLearn
00284 
00285 #endif
00286 
00287 
00288 /*
00289   Local Variables:
00290   mode:c++
00291   c-basic-offset:4
00292   c-file-style:"stroustrup"
00293   c-file-offsets:((innamespace . 0)(inline-open . 0))
00294   indent-tabs-mode:nil
00295   fill-column:79
00296   End:
00297 */
00298 // 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