PLearn 0.1
|
00001 // -*- C++ -*- 00002 00003 // RBMGaussianLayer.cc 00004 // 00005 // Copyright (C) 2006 Pascal Lamblin & 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 // Authors: Pascal Lamblin & Dan Popovici 00036 00039 #include "RBMGaussianLayer.h" 00040 #include <plearn/math/TMat_maths.h> 00041 #include "RBMParameters.h" 00042 00043 namespace PLearn { 00044 using namespace std; 00045 00046 PLEARN_IMPLEMENT_OBJECT( 00047 RBMGaussianLayer, 00048 "Layer in an RBM, consisting in Gaussian units", 00049 ""); 00050 00051 RBMGaussianLayer::RBMGaussianLayer() 00052 { 00053 } 00054 00055 RBMGaussianLayer::RBMGaussianLayer( int the_size ) 00056 { 00057 size = the_size; 00058 units_types = string( the_size, 'q' ); 00059 activations.resize( 2*the_size ); 00060 sample.resize( the_size ); 00061 expectation.resize( the_size ); 00062 expectation_is_up_to_date = false; 00063 } 00064 00067 void RBMGaussianLayer::getUnitActivations( int i, PP<RBMParameters> rbmp, 00068 int offset ) 00069 { 00070 Vec activation = activations.subVec( 2*i, 2 ); 00071 rbmp->computeUnitActivations( i+offset, 1, activation ); 00072 expectation_is_up_to_date = false; 00073 } 00074 00077 void RBMGaussianLayer::getAllActivations( PP<RBMParameters> rbmp, int offset ) 00078 { 00079 rbmp->computeUnitActivations( offset, size, activations ); 00080 /* static FILE * f = fopen("mu_sigma.txt" , "wt") ; 00081 if (activations.size() > 2) 00082 fprintf(f , "%0.8f %0.8f %0.8f %0.8f \n" , activations[0] , activations[1] , 00083 activations[2], activations[3]) ; 00084 else 00085 fprintf(f , "%0.8f %0.8f\n" , activations[0] , activations[1]) ; 00086 */ 00087 expectation_is_up_to_date = false; 00088 } 00089 00090 void RBMGaussianLayer::generateSample() 00091 { 00092 for( int i=0 ; i<size ; i++ ) 00093 sample[i] = random_gen->gaussian_mu_sigma( activations[2*i], 00094 activations[2*i + 1] ); 00095 } 00096 00097 void RBMGaussianLayer::computeExpectation() 00098 { 00099 if( expectation_is_up_to_date ) 00100 return; 00101 00102 for( int i=0 ; i<size ; i++ ) 00103 expectation[i] = activations[2*i]; 00104 00105 expectation_is_up_to_date = true; 00106 } 00107 00108 void RBMGaussianLayer::bpropUpdate(const Vec& input, const Vec& output, 00109 Vec& input_gradient, 00110 const Vec& output_gradient) 00111 { 00112 PLASSERT( input.size() == 2 * size ); 00113 PLASSERT( output.size() == size ); 00114 PLASSERT( output_gradient.size() == size ); 00115 input_gradient.resize( 2 * size ) ; 00116 00117 for( int i=0 ; i<size ; ++i ) { 00118 input_gradient[2*i] = output_gradient[i] ; 00119 input_gradient[2*i+1] = 0. ; 00120 } 00121 00122 } 00123 00124 00125 void RBMGaussianLayer::declareOptions(OptionList& ol) 00126 { 00127 /* 00128 declareOption(ol, "size", &RBMGaussianLayer::size, 00129 OptionBase::buildoption, 00130 "Number of units."); 00131 */ 00132 // Now call the parent class' declareOptions 00133 inherited::declareOptions(ol); 00134 } 00135 00136 void RBMGaussianLayer::build_() 00137 { 00138 if( size < 0 ) 00139 size = int(units_types.size()); 00140 if( size != (int) units_types.size() ) 00141 units_types = string( size, 'q' ); 00142 00143 activations.resize( 2*size ); 00144 sample.resize( size ); 00145 expectation.resize( size ); 00146 expectation_is_up_to_date = false; 00147 } 00148 00149 void RBMGaussianLayer::build() 00150 { 00151 inherited::build(); 00152 build_(); 00153 } 00154 00155 00156 void RBMGaussianLayer::makeDeepCopyFromShallowCopy(CopiesMap& copies) 00157 { 00158 inherited::makeDeepCopyFromShallowCopy(copies); 00159 } 00160 00161 00162 } // end of namespace PLearn 00163 00164 00165 /* 00166 Local Variables: 00167 mode:c++ 00168 c-basic-offset:4 00169 c-file-style:"stroustrup" 00170 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00171 indent-tabs-mode:nil 00172 fill-column:79 00173 End: 00174 */ 00175 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :