PLearn 0.1
LayerCostModule.h
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // LayerCostModule.h
00004 //
00005 // Copyright (C) 2007 Jerome Louradour
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: Jerome Louradour
00036 
00039 #ifndef LayerCostModule_INC
00040 #define LayerCostModule_INC
00041 
00042 #include <plearn_learners/online/CostModule.h>
00043 
00044 #include <map>
00045 
00046 namespace PLearn {
00047 
00051 class LayerCostModule : public CostModule
00052 {
00053     typedef CostModule inherited;
00054 
00055 public:
00056     //#####  Public Build Options  ############################################
00057 
00059     string cost_function;
00060 
00062     int nstages_max;
00063 
00065     real momentum;
00066 
00068     string optimization_strategy;
00069 
00071     real alpha;
00072 
00074     int histo_size;
00075 
00076 
00077     //#####  Public Learnt Options  ###########################################
00078 
00081     Mat inputs_histo;
00082 
00086     Vec inputs_expectation;
00087     Vec inputs_stds;         
00088 
00089     Mat inputs_cross_quadratic_mean;
00090     Mat inputs_correlations; 
00091 
00095     Vec inputs_expectation_trainMemory;
00096     Mat inputs_cross_quadratic_mean_trainMemory;
00097 
00100     string penalty_function;
00101 
00103     string cost_function_completename;
00104 
00105 public:
00106     //#####  Public Member Functions  #########################################
00107 
00109     LayerCostModule();
00110 
00112     virtual void fprop(const Vec& input, real& cost) const;
00113     virtual void fprop(const Mat& inputs, Mat& costs) const;
00114     virtual void fprop(const Mat& inputs, const Mat& targets, Mat& costs) const;
00115     virtual void fprop(const TVec<Mat*>& ports_value);
00116 
00118     virtual void bpropUpdate(const Mat& inputs, const Mat& targets,
00119                              const Vec& costs, Mat& input_gradients, bool accumulate=false);
00120     virtual void bpropUpdate(const Mat& inputs, Mat& inputs_grad);
00121     virtual void bpropAccUpdate(const TVec<Mat*>& ports_value,
00122                                 const TVec<Mat*>& ports_gradient);
00123 
00125     virtual void computeHisto(const Mat& inputs);
00126     virtual void computeHisto(const Mat& inputs, Mat& histo) const;
00127     virtual real delta_KLdivTerm(int i, int j, int index_i, real over_dq);
00128     virtual real KLdivTerm(real pi, real pj) const;
00129     virtual real computeKLdiv(bool store_in_cache);
00130     virtual real computeKLdiv(const Mat& histo) const;
00131     virtual int histo_index(real q) const;
00132     virtual real dq(real q) const;
00134     virtual void computeSafeHisto(const Mat& inputs);
00135     virtual void computeSafeHisto(const Mat& inputs, Mat& histo) const;
00136     virtual real delta_SafeKLdivTerm(int i, int j, int index_i, real over_dq);
00137 
00139     virtual void computePascalStatistics(const Mat& inputs);
00140     virtual void computePascalStatistics(const Mat& inputs,
00141                                          Vec& expectation, Mat& cross_quadratic_mean) const;
00142     virtual real func_(real correlation) const;
00143     virtual real deriv_func_(real correlation) const;
00144 
00146     virtual void computeCorrelationStatistics(const Mat& inputs);
00147     virtual void computeCorrelationStatistics(const Mat& inputs,
00148                                               Vec& expectation, Mat& cross_quadratic_mean,
00149                                               Vec& stds, Mat& correlations) const;
00151     virtual const TVec<string>& getPorts();
00152 
00154     virtual const TMat<int>& getPortSizes();
00155 
00159     virtual int getPortIndex(const string& port);
00160 
00162     virtual void setLearningRate(real dynamic_learning_rate) {}
00163 
00165     virtual TVec<string> costNames();
00166 
00167     //#####  PLearn::Object Protocol  #########################################
00168 
00169     // Declares other standard object methods.
00170     // ### If your class is not instantiatable (it has pure virtual methods)
00171     // ### you should replace this by PLEARN_DECLARE_ABSTRACT_OBJECT
00172     PLEARN_DECLARE_OBJECT(LayerCostModule);
00173 
00174     // Simply calls inherited::build() then build_()
00175     virtual void build();
00176 
00178     virtual void makeDeepCopyFromShallowCopy(CopiesMap& copies);
00179 
00180     virtual void forget();
00181 
00182 protected:
00183 
00184     bool LINEAR_FUNC;
00185     bool SQUARE_FUNC;
00186     bool POW4_FUNC;
00187     bool EXP_FUNC;
00188     bool LOG_FUNC;
00189 
00191     int stage;
00192 
00194     bool bprop_all_terms;
00195     bool random_index_during_bprop;
00196 
00199     bool is_cost_function_stochastic;
00200 
00203     real norm_factor;
00204 
00205     real average_deriv;
00206 
00210     real HISTO_STEP;
00212 
00213     mutable real one_count;
00214     TVec< TVec< Vec > > cache_differ_count_i;
00215     TVec< TVec< Vec > > cache_differ_count_j;
00216     TVec< TVec< Vec > > cache_n_differ;
00217 
00219     map<string, int> portname_to_index;
00220 
00222     TVec<string> ports;
00223 
00224     //#####  Protected Member Functions  ######################################
00225 
00227     void addPortName(const string& name);
00228 
00230     static void declareOptions(OptionList& ol);
00231 
00232 private:
00233     //#####  Private Member Functions  ########################################
00234 
00236     void build_();
00237 
00238 private:
00239     //#####  Private Data Members  ############################################
00240 
00241     // The rest of the private stuff goes here
00242 };
00243 
00244 // Declares a few other classes and functions related to this class
00245 DECLARE_OBJECT_PTR(LayerCostModule);
00246 
00247 } // end of namespace PLearn
00248 
00249 #endif
00250 
00251 
00252 /*
00253   Local Variables:
00254   mode:c++
00255   c-basic-offset:4
00256   c-file-style:"stroustrup"
00257   c-file-offsets:((innamespace . 0)(inline-open . 0))
00258   indent-tabs-mode:nil
00259   fill-column:79
00260   End:
00261 */
00262 // 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