PLearn 0.1
|
00001 // -*- C++ -*- 00002 00003 // RBMMatrixConnection.h 00004 // 00005 // Copyright (C) 2006 Pascal Lamblin 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: Pascal Lamblin 00036 00040 #ifndef RBMMatrixConnection_INC 00041 #define RBMMatrixConnection_INC 00042 00043 #include "RBMConnection.h" 00044 00045 namespace PLearn { 00046 using namespace std; 00047 00048 00053 class RBMMatrixConnection: public RBMConnection 00054 { 00055 typedef RBMConnection inherited; 00056 00057 public: 00058 //##### Public Build Options ############################################ 00059 00065 Vec gibbs_ma_schedule; 00066 real gibbs_ma_increment; 00067 real gibbs_initial_ma_coefficient; 00068 00069 //##### Learned Options ################################################# 00070 00072 real L1_penalty_factor; 00073 00075 real L2_penalty_factor; 00076 00077 real L2_decrease_constant; 00078 real L2_shift; 00079 string L2_decrease_type; 00080 int L2_n_updates; 00081 00083 Mat weights; 00084 00086 real gibbs_ma_coefficient; 00087 00088 00089 //##### Not Options ##################################################### 00090 00091 00093 Mat weights_pos_stats; 00094 00096 Mat weights_neg_stats; 00097 00099 Mat weights_inc; 00100 00101 public: 00102 //##### Public Member Functions ######################################### 00103 00105 RBMMatrixConnection( real the_learning_rate=0 ); 00106 00107 // Your other public member functions go here 00108 00110 virtual void accumulatePosStats( const Vec& down_values, 00111 const Vec& up_values ); 00112 00113 virtual void accumulatePosStats( const Mat& down_values, 00114 const Mat& up_values ); 00115 00117 virtual void accumulateNegStats( const Vec& down_values, 00118 const Vec& up_values ); 00119 00120 virtual void accumulateNegStats( const Mat& down_values, 00121 const Mat& up_values ); 00122 00124 virtual void update(); 00125 00128 virtual void update( const Vec& pos_down_values, 00129 const Vec& pos_up_values, 00130 const Vec& neg_down_values, 00131 const Vec& neg_up_values ); 00132 00134 virtual void update( const Mat& pos_down_values, 00135 const Mat& pos_up_values, 00136 const Mat& neg_down_values, 00137 const Mat& neg_up_values); 00138 00139 // neg_stats <-- gibbs_chain_statistics_forgetting_factor * neg_stats 00140 // +(1-gibbs_chain_statistics_forgetting_factor) 00141 // * gibbs_neg_up_values'*gibbs_neg_down_values 00142 // delta w = -lrate * ( pos_up_values'*pos_down_values 00143 // - ( background_gibbs_update_ratio*neg_stats 00144 // +(1-background_gibbs_update_ratio) 00145 // * cd_neg_up_values'*cd_neg_down_values)) 00146 virtual void updateCDandGibbs( const Mat& pos_down_values, 00147 const Mat& pos_up_values, 00148 const Mat& cd_neg_down_values, 00149 const Mat& cd_neg_up_values, 00150 const Mat& gibbs_neg_down_values, 00151 const Mat& gibbs_neg_up_values, 00152 real background_gibbs_update_ratio); 00153 00154 // neg_stats <-- gibbs_chain_statistics_forgetting_factor * neg_stats 00155 // +(1-gibbs_chain_statistics_forgetting_factor) 00156 // * gibbs_neg_up_values'*gibbs_neg_down_values 00157 // delta w = -lrate * ( pos_up_values'*pos_down_values - neg_stats ) 00158 virtual void updateGibbs( const Mat& pos_down_values, 00159 const Mat& pos_up_values, 00160 const Mat& gibbs_neg_down_values, 00161 const Mat& gibbs_neg_up_values ); 00162 00164 virtual void clearStats(); 00165 00169 virtual void computeProduct( int start, int length, 00170 const Vec& activations, 00171 bool accumulate=false ) const; 00172 00174 virtual void computeProducts(int start, int length, 00175 Mat& activations, 00176 bool accumulate=false ) const; 00177 00188 // virtual void bpropUpdate(const Vec& input, const Vec& output, 00189 // const Vec& output_gradient); 00190 00193 virtual void fprop(const Vec& input, const Mat& rbm_weights, 00194 Vec& output) const; 00195 00197 virtual void getAllWeights(Mat& rbm_weights) const; 00198 00200 virtual void setAllWeights(const Mat& rbm_weights); 00201 00204 virtual void bpropUpdate(const Vec& input, const Vec& output, 00205 Vec& input_gradient, 00206 const Vec& output_gradient, 00207 bool accumulate=false); 00208 00209 virtual void bpropUpdate(const Mat& inputs, const Mat& outputs, 00210 Mat& input_gradients, 00211 const Mat& output_gradients, 00212 bool accumulate = false); 00213 00214 virtual void bpropAccUpdate(const TVec<Mat*>& ports_value, 00215 const TVec<Mat*>& ports_gradient); 00216 00219 virtual void petiteCulotteOlivierUpdate( 00220 const Vec& input, const Mat& rbm_weights, 00221 const Vec& output, 00222 Vec& input_gradient, Mat& rbm_weights_gradient, 00223 const Vec& output_gradient, 00224 bool accumulate = false); 00225 00228 virtual void petiteCulotteOlivierCD(Mat& weights_gradient, 00229 bool accumulate = false); 00230 00233 virtual void petiteCulotteOlivierCD(const Vec& pos_down_values, 00234 const Vec& pos_up_values, 00235 const Vec& neg_down_values, 00236 const Vec& neg_up_values, 00237 Mat& weights_gradient, 00238 bool accumulate = false); 00239 00241 virtual void applyWeightPenalty(); 00242 00244 virtual void addWeightPenalty(Mat weights, Mat weight_gradients); 00245 00249 virtual void forget(); 00250 00255 // virtual void finalize(); 00256 00258 virtual int nParameters() const; 00259 00265 virtual Vec makeParametersPointHere(const Vec& global_parameters); 00266 00267 //##### PLearn::Object Protocol ######################################### 00268 00269 // Declares other standard object methods. 00270 PLEARN_DECLARE_OBJECT(RBMMatrixConnection); 00271 00272 // Simply calls inherited::build() then build_() 00273 virtual void build(); 00274 00276 virtual void makeDeepCopyFromShallowCopy(CopiesMap& copies); 00277 00278 protected: 00279 //##### Protected Member Functions ###################################### 00281 static void declareOptions(OptionList& ol); 00282 00283 private: 00284 //##### Private Member Functions ######################################## 00285 00287 void build_(); 00288 00289 private: 00290 //##### Private Data Members ############################################ 00291 00292 // The rest of the private stuff goes here 00293 }; 00294 00295 // Declares a few other classes and functions related to this class 00296 DECLARE_OBJECT_PTR(RBMMatrixConnection); 00297 00298 } // end of namespace PLearn 00299 00300 #endif 00301 00302 00303 /* 00304 Local Variables: 00305 mode:c++ 00306 c-basic-offset:4 00307 c-file-style:"stroustrup" 00308 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00309 indent-tabs-mode:nil 00310 fill-column:79 00311 End: 00312 */ 00313 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :