PLearn 0.1
DEPRECATED/RBMBinomialLayer.cc
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // RBMBinomialLayer.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 "RBMBinomialLayer.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     RBMBinomialLayer,
00048     "Layer in an RBM formed with binomial units",
00049     "");
00050 
00051 RBMBinomialLayer::RBMBinomialLayer()
00052 {
00053 }
00054 
00055 RBMBinomialLayer::RBMBinomialLayer( 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 RBMBinomialLayer::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 
00077 void RBMBinomialLayer::getAllActivations( PP<RBMParameters> rbmp, int offset )
00078 {
00079     rbmp->computeUnitActivations( offset, size, activations );
00080     expectation_is_up_to_date = false;
00081 }
00082 
00083 void RBMBinomialLayer::generateSample()
00084 {
00085     computeExpectation();
00086 
00087     for( int i=0 ; i<size ; i++ )
00088         sample[i] = random_gen->binomial_sample( expectation[i] );
00089 }
00090 
00091 void RBMBinomialLayer::computeExpectation()
00092 {
00093     if( expectation_is_up_to_date )
00094         return;
00095 
00096     for( int i=0 ; i<size ; i++ )
00097         expectation[i] = sigmoid( -activations[i] );
00098 
00099     expectation_is_up_to_date = true;
00100 }
00101 
00102 void RBMBinomialLayer::bpropUpdate(const Vec& input, const Vec& output,
00103                                    Vec& input_gradient,
00104                                    const Vec& output_gradient)
00105 {
00106     PLASSERT( input.size() == size );
00107     PLASSERT( output.size() == size );
00108     PLASSERT( output_gradient.size() == size );
00109     input_gradient.resize( size );
00110 
00111     for( int i=0 ; i<size ; i++ )
00112     {
00113         real output_i = output[i];
00114         input_gradient[i] = - output_i * (1-output_i) * output_gradient[i];
00115     }
00116 }
00117 
00118 
00119 
00120 void RBMBinomialLayer::declareOptions(OptionList& ol)
00121 {
00122 /*
00123     declareOption(ol, "size", &RBMBinomialLayer::size,
00124                   OptionBase::buildoption,
00125                   "Number of units.");
00126 */
00127     // Now call the parent class' declareOptions
00128     inherited::declareOptions(ol);
00129 }
00130 
00131 void RBMBinomialLayer::build_()
00132 {
00133     if( size < 0 )
00134         size = int(units_types.size());
00135     if( size != (int) units_types.size() )
00136         units_types = string( size, 'l' );
00137 
00138     activations.resize( size );
00139     sample.resize( size );
00140     expectation.resize( size );
00141     expectation_is_up_to_date = false;
00142 }
00143 
00144 void RBMBinomialLayer::build()
00145 {
00146     inherited::build();
00147     build_();
00148 }
00149 
00150 
00151 void RBMBinomialLayer::makeDeepCopyFromShallowCopy(CopiesMap& copies)
00152 {
00153     inherited::makeDeepCopyFromShallowCopy(copies);
00154 }
00155 
00156 
00157 } // end of namespace PLearn
00158 
00159 
00160 /*
00161   Local Variables:
00162   mode:c++
00163   c-basic-offset:4
00164   c-file-style:"stroustrup"
00165   c-file-offsets:((innamespace . 0)(inline-open . 0))
00166   indent-tabs-mode:nil
00167   fill-column:79
00168   End:
00169 */
00170 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines