PLearn 0.1
|
00001 // -*- C++ -*- 00002 00003 // RBMLateralBinomialLayer.h 00004 // 00005 // Copyright (C) 2006 Hugo Larochelle 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: Hugo Larochelle 00036 00040 #ifndef RBMLateralBinomialLayer_INC 00041 #define RBMLateralBinomialLayer_INC 00042 00043 #include "RBMLayer.h" 00044 00045 namespace PLearn { 00046 using namespace std; 00047 00052 class RBMLateralBinomialLayer: public RBMLayer 00053 { 00054 typedef RBMLayer inherited; 00055 00056 public: 00057 //##### Public Build Options ############################################ 00058 00060 int n_lateral_connections_passes; 00061 00064 real dampening_factor; 00065 00070 real mean_field_precision_threshold; 00071 00073 int topographic_length; 00074 00076 int topographic_width; 00077 00079 int topographic_patch_vradius; 00080 00082 int topographic_patch_hradius; 00083 00085 real topographic_lateral_weights_init_value; 00086 00089 bool do_not_learn_topographic_lateral_weights; 00090 00092 Mat lateral_weights; 00093 00095 TVec< Vec > topographic_lateral_weights; 00096 00098 Mat lateral_weights_pos_stats; 00099 00101 Mat lateral_weights_neg_stats; 00102 00105 bool use_parametric_mean_field; 00106 00108 Mat mean_field_output_weights; 00109 00111 Vec mean_field_output_bias; 00112 00113 public: 00114 //##### Public Member Functions ######################################### 00115 00117 RBMLateralBinomialLayer( real the_learning_rate=0. ); 00118 00120 virtual void reset(); 00121 00123 virtual void clearStats(); 00124 00126 virtual void forget(); 00127 00129 virtual void generateSample() ; 00130 00132 virtual void generateSamples(); 00133 00135 virtual void computeExpectation() ; 00136 00138 virtual void computeExpectations(); 00139 00141 virtual void fprop( const Vec& input, Vec& output ) const; 00142 00144 virtual void fprop( const Mat& inputs, Mat& outputs ); 00145 00147 virtual void fprop( const Vec& input, const Vec& rbm_bias, 00148 Vec& output ) const; 00149 00151 virtual void bpropUpdate(const Vec& input, const Vec& output, 00152 Vec& input_gradient, const Vec& output_gradient, 00153 bool accumulate=false); 00154 00156 virtual void bpropUpdate(const Vec& input, const Vec& rbm_bias, 00157 const Vec& output, 00158 Vec& input_gradient, Vec& rbm_bias_gradient, 00159 const Vec& output_gradient) ; 00160 00162 virtual void bpropUpdate(const Mat& inputs, const Mat& outputs, 00163 Mat& input_gradients, 00164 const Mat& output_gradients, 00165 bool accumulate = false); 00166 00169 virtual real fpropNLL(const Vec& target); 00170 virtual void fpropNLL(const Mat& targets, const Mat& costs_column); 00171 00177 virtual void bpropNLL(const Vec& target, real nll, Vec& bias_gradient); 00178 virtual void bpropNLL(const Mat& targets, const Mat& costs_column, 00179 Mat& bias_gradients); 00180 00182 virtual void accumulatePosStats( const Vec& pos_values ); 00183 virtual void accumulatePosStats( const Mat& ps_values); 00184 00186 virtual void accumulateNegStats( const Vec& neg_values ); 00187 virtual void accumulateNegStats( const Mat& neg_values ); 00188 00191 virtual void update(); 00192 00194 virtual void update( const Vec& grad ); 00195 00198 virtual void update( const Vec& pos_values, const Vec& neg_values ); 00199 00202 virtual void update( const Mat& pos_values, const Mat& neg_values ); 00203 00204 // neg_stats <-- gibbs_chain_statistics_forgetting_factor * neg_stats 00205 // +(1-gibbs_chain_statistics_forgetting_factor) 00206 // * gibbs_neg_values 00207 // delta w = -lrate * ( pos_values 00208 // - ( background_gibbs_update_ratio*neg_stats 00209 // +(1-background_gibbs_update_ratio) 00210 // * cd_neg_values ) ) 00211 virtual void updateCDandGibbs( const Mat& pos_values, 00212 const Mat& cd_neg_values, 00213 const Mat& gibbs_neg_values, 00214 real background_gibbs_update_ratio ); 00215 00216 // neg_stats <-- gibbs_chain_statistics_forgetting_factor * neg_stats 00217 // +(1-gibbs_chain_statistics_forgetting_factor) 00218 // * \sum_i gibbs_neg_values_i / minibatch_size 00219 // delta bias = -lrate * \sum_i (pos_values_i - neg_stats) / minibatch_size 00220 virtual void updateGibbs( const Mat& pos_values, 00221 const Mat& gibbs_neg_values ); 00222 00224 virtual real energy(const Vec& unit_values) const; 00225 00227 virtual real freeEnergyContribution(const Vec& unit_activations) const; 00228 00229 virtual int getConfigurationCount(); 00230 00231 virtual void getConfiguration(int conf_index, Vec& output); 00232 00233 //##### PLearn::Object Protocol ######################################### 00234 00235 // Declares other standard object methods. 00236 PLEARN_DECLARE_OBJECT(RBMLateralBinomialLayer); 00237 00238 // Simply calls inherited::build() then build_() 00239 virtual void build(); 00240 00242 virtual void makeDeepCopyFromShallowCopy(CopiesMap& copies); 00243 00244 protected: 00245 //##### Not Options ##################################################### 00246 00247 mutable Vec dampening_expectation; 00248 mutable Mat dampening_expectations; 00249 00250 mutable Vec mean_field_input; 00251 mutable Vec pre_sigmoid_mean_field_output; 00252 00253 mutable TVec<Vec> temp_output; 00254 mutable TVec<Mat> temp_outputs; 00255 00256 mutable Vec current_temp_output, previous_temp_output; 00257 mutable Mat current_temp_outputs, previous_temp_outputs; 00258 00259 mutable Vec bias_plus_input; 00260 mutable Mat bias_plus_inputs; 00261 00262 Vec temp_input_gradient; 00263 Vec temp_mean_field_gradient; 00264 Vec temp_mean_field_gradient2; 00265 00266 Mat lateral_weights_gradient; 00267 Mat lateral_weights_inc; 00268 00269 TVec< Vec > topographic_lateral_weights_gradient; 00270 00271 protected: 00272 //##### Protected Member Functions ###################################### 00273 00275 static void declareOptions(OptionList& ol); 00276 00278 void externalSymetricProductAcc(const Mat& mat, const Vec& v1, 00279 const Vec& v2); 00280 00281 void productTopoLateralWeights( const Vec& result, const Vec& input ) const; 00282 00283 void productTopoLateralWeightsGradients( const Vec& input, const Vec& input_gradient, 00284 const Vec& result_gradient, 00285 const TVec< Vec >& weights_gradient ); 00286 00287 void updateTopoLateralWeightsCD( const Vec& pos_values, const Vec& neg_values ); 00288 00289 private: 00290 //##### Private Member Functions ######################################## 00291 00293 void build_(); 00294 00295 private: 00296 //##### Private Data Members ############################################ 00297 00298 // The rest of the private stuff goes here 00299 }; 00300 00301 // Declares a few other classes and functions related to this class 00302 DECLARE_OBJECT_PTR(RBMLateralBinomialLayer); 00303 00304 } // end of namespace PLearn 00305 00306 #endif 00307 00308 00309 /* 00310 Local Variables: 00311 mode:c++ 00312 c-basic-offset:4 00313 c-file-style:"stroustrup" 00314 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00315 indent-tabs-mode:nil 00316 fill-column:79 00317 End: 00318 */ 00319 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :