PLearn 0.1
|
00001 // -*- C++ -*- 00002 00003 // RBMConnection.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 RBMConnection_INC 00041 #define RBMConnection_INC 00042 00043 #include <plearn/base/Object.h> 00044 #include "OnlineLearningModule.h" 00045 00046 namespace PLearn { 00047 using namespace std; 00048 00049 // forward declaration 00050 class RBMLayer; 00051 00056 class RBMConnection: public OnlineLearningModule 00057 { 00058 typedef OnlineLearningModule inherited; 00059 00060 public: 00061 //##### Public Build Options ############################################ 00062 00064 real learning_rate; 00065 00067 real momentum; 00068 00074 string initialization_method; 00075 00076 //##### Not Options ##################################################### 00077 00079 int down_size; 00080 00082 int up_size; 00083 00084 public: 00085 //##### Public Member Functions ######################################### 00086 00088 RBMConnection(real the_learning_rate = 0, bool call_build_ = false); 00089 00090 // Your other public member functions go here 00091 00093 virtual void setLearningRate( real the_learning_rate ); 00094 00096 virtual void setMomentum( real the_momentum ); 00097 00101 virtual void setAsUpInput( const Vec& input ) const; 00102 00106 virtual void setAsUpInputs( const Mat& inputs ) const; 00107 00111 virtual void setAsDownInput( const Vec& input ) const; 00112 00116 virtual void setAsDownInputs( const Mat& inputs ) const; 00117 00119 virtual void accumulatePosStats( const Vec& down_values, 00120 const Vec& up_values ) = 0; 00121 00122 virtual void accumulatePosStats( const Mat& down_values, 00123 const Mat& up_values ) = 0; 00124 00126 virtual void accumulateNegStats( const Vec& down_values, 00127 const Vec& up_values ) = 0; 00128 00129 virtual void accumulateNegStats( const Mat& down_values, 00130 const Mat& up_values ) = 0; 00131 00133 virtual void update() = 0; 00134 00137 virtual void update( const Vec& pos_down_values, 00138 const Vec& pos_up_values, 00139 const Vec& neg_down_values, 00140 const Vec& neg_up_values); 00141 00144 virtual void update( const Mat& pos_down_values, 00145 const Mat& pos_up_values, 00146 const Mat& neg_down_values, 00147 const Mat& neg_up_values); 00148 00149 // neg_stats <-- gibbs_chain_statistics_forgetting_factor * neg_stats 00150 // +(1-gibbs_chain_statistics_forgetting_factor) 00151 // * gibbs_neg_up_values'*gibbs_neg_down_values 00152 // delta w = -lrate * ( pos_up_values'*pos_down_values 00153 // - ( background_gibbs_update_ratio*neg_stats 00154 // +(1-background_gibbs_update_ratio) 00155 // * cd_neg_up_values'*cd_neg_down_values)) 00156 virtual void updateCDandGibbs( const Mat& pos_down_values, 00157 const Mat& pos_up_values, 00158 const Mat& cd_neg_down_values, 00159 const Mat& cd_neg_up_values, 00160 const Mat& gibbs_neg_down_values, 00161 const Mat& gibbs_neg_up_values, 00162 real background_gibbs_update_ratio); 00163 00164 // neg_stats <-- gibbs_chain_statistics_forgetting_factor * neg_stats 00165 // +(1-gibbs_chain_statistics_forgetting_factor) 00166 // * gibbs_neg_up_values'*gibbs_neg_down_values 00167 // delta w = -lrate * ( pos_up_values'*pos_down_values - neg_stats ) 00168 virtual void updateGibbs( const Mat& pos_down_values, 00169 const Mat& pos_up_values, 00170 const Mat& gibbs_neg_down_values, 00171 const Mat& gibbs_neg_up_values); 00172 00174 virtual void clearStats() = 0; 00175 00179 virtual void computeProduct( int start, int length, 00180 const Vec& activations, 00181 bool accumulate=false ) const = 0; 00182 00184 virtual void computeProducts(int start, int length, 00185 Mat& activations, 00186 bool accumulate=false ) const = 0; 00187 00189 virtual void fprop(const Vec& input, Vec& output) const; 00190 virtual void fprop(const Mat& inputs, Mat& outputs); 00191 00193 virtual void getAllWeights(Mat& rbm_weights) const; 00194 00196 virtual void setAllWeights(const Mat& rbm_weights); 00197 00200 virtual void petiteCulotteOlivierUpdate( 00201 const Vec& input, const Mat& rbm_weights, 00202 const Vec& output, 00203 Vec& input_gradient, Mat& rbm_weights_gradient, 00204 const Vec& output_gradient, 00205 bool accumulate = false); 00206 00209 virtual void petiteCulotteOlivierCD(Mat& weights_gradient, 00210 bool accumulate = false); 00211 00214 virtual void petiteCulotteOlivierCD(const Vec& pos_down_values, 00215 const Vec& pos_up_values, 00216 const Vec& neg_down_values, 00217 const Vec& neg_up_values, 00218 Mat& weights_gradient, 00219 bool accumulate = false); 00220 00222 virtual const TVec<string>& getPorts(); 00223 00225 virtual const TMat<int>& getPortSizes(); 00226 00228 virtual int nParameters() const = 0; 00229 00235 virtual Vec makeParametersPointHere(const Vec& global_parameters) = 0; 00236 00237 //##### PLearn::Object Protocol ######################################### 00238 00239 // Declares other standard object methods. 00240 PLEARN_DECLARE_ABSTRACT_OBJECT(RBMConnection); 00241 00242 // Simply calls inherited::build() then build_() 00243 virtual void build(); 00244 00246 virtual void makeDeepCopyFromShallowCopy(CopiesMap& copies); 00247 00248 protected: 00249 //##### Not Options ##################################################### 00250 00252 mutable Vec input_vec; 00253 00255 mutable Mat inputs_mat; 00256 00258 mutable bool going_up; 00259 00261 int pos_count; 00262 00264 int neg_count; 00265 00267 TVec<string> ports; 00268 00269 protected: 00270 //##### Protected Member Functions ###################################### 00271 00273 static void declareOptions(OptionList& ol); 00274 00276 static void declareMethods(RemoteMethodMap& rmm); 00277 00278 private: 00279 //##### Private Member Functions ######################################## 00280 00282 void build_(); 00283 00284 private: 00285 //##### Private Data Members ############################################ 00286 00287 // The rest of the private stuff goes here 00288 }; 00289 00290 // Declares a few other classes and functions related to this class 00291 DECLARE_OBJECT_PTR(RBMConnection); 00292 00293 } // end of namespace PLearn 00294 00295 #endif 00296 00297 00298 /* 00299 Local Variables: 00300 mode:c++ 00301 c-basic-offset:4 00302 c-file-style:"stroustrup" 00303 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00304 indent-tabs-mode:nil 00305 fill-column:79 00306 End: 00307 */ 00308 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :