PLearn 0.1
|
00001 // -*- C++ -*- 00002 00003 // RBMMultinomialLayer.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 "RBMMultinomialLayer.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 RBMMultinomialLayer, 00048 "Layer in an RBM, consisting in one multinomial unit", 00049 ""); 00050 00051 RBMMultinomialLayer::RBMMultinomialLayer() 00052 { 00053 } 00054 00055 RBMMultinomialLayer::RBMMultinomialLayer( int the_size ) 00056 { 00057 size = the_size; 00058 units_types = string( the_size, 'l' ); 00059 activations.resize( the_size ); 00060 sample.resize( the_size ); 00061 expectation.resize( the_size ); 00062 expectation_is_up_to_date = false; 00063 } 00064 00067 void RBMMultinomialLayer::getUnitActivations( int i, PP<RBMParameters> rbmp, 00068 int offset ) 00069 { 00070 Vec activation = activations.subVec( i, 1 ); 00071 rbmp->computeUnitActivations( i+offset, 1, activation ); 00072 expectation_is_up_to_date = false; 00073 } 00074 00075 void RBMMultinomialLayer::getAllActivations( PP<RBMParameters> rbmp, 00076 int offset ) 00077 { 00078 rbmp->computeUnitActivations( offset, size, activations ); 00079 expectation_is_up_to_date = false; 00080 } 00081 00082 void RBMMultinomialLayer::generateSample() 00083 { 00084 computeExpectation(); 00085 00086 int i = random_gen->multinomial_sample( expectation ); 00087 fill_one_hot( sample, i, 0., 1. ); 00088 } 00089 00090 void RBMMultinomialLayer::computeExpectation() 00091 { 00092 if( expectation_is_up_to_date ) 00093 return; 00094 00095 // expectation = softmax(-activations) 00096 softmaxMinus(activations, expectation); 00097 expectation_is_up_to_date = true; 00098 } 00099 00100 void RBMMultinomialLayer::bpropUpdate(const Vec& input, const Vec& output, 00101 Vec& input_gradient, 00102 const Vec& output_gradient) 00103 { 00104 PLERROR( "RBMMultinomialLayer::bpropUpdate not implemented yet." ); 00105 } 00106 00107 00108 void RBMMultinomialLayer::declareOptions(OptionList& ol) 00109 { 00110 /* 00111 declareOption(ol, "size", &RBMMultinomialLayer::size, 00112 OptionBase::buildoption, 00113 "Number of units."); 00114 */ 00115 // Now call the parent class' declareOptions 00116 inherited::declareOptions(ol); 00117 } 00118 00119 void RBMMultinomialLayer::build_() 00120 { 00121 if( size < 0 ) 00122 size = int(units_types.size()); 00123 if( size != (int) units_types.size() ) 00124 units_types = string( size, 'l' ); 00125 00126 activations.resize( size ); 00127 sample.resize( size ); 00128 expectation.resize( size ); 00129 expectation_is_up_to_date = false; 00130 } 00131 00132 void RBMMultinomialLayer::build() 00133 { 00134 inherited::build(); 00135 build_(); 00136 } 00137 00138 00139 void RBMMultinomialLayer::makeDeepCopyFromShallowCopy(CopiesMap& copies) 00140 { 00141 inherited::makeDeepCopyFromShallowCopy(copies); 00142 } 00143 00144 00145 } // end of namespace PLearn 00146 00147 00148 /* 00149 Local Variables: 00150 mode:c++ 00151 c-basic-offset:4 00152 c-file-style:"stroustrup" 00153 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00154 indent-tabs-mode:nil 00155 fill-column:79 00156 End: 00157 */ 00158 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :