PLearn 0.1
RBMParameters.cc
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // RBMParameters.cc
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 
00041 #include "RBMParameters.h"
00042 #include <plearn/math/TMat_maths.h>
00043 #include <plearn/base/stringutils.h>
00044 //#include "RBMLayer.h"
00045 
00046 namespace PLearn {
00047 using namespace std;
00048 
00049 PLEARN_IMPLEMENT_ABSTRACT_OBJECT(
00050     RBMParameters,
00051     "Virtual class for the parameters between two layers of an RBM",
00052     "");
00053 
00054 RBMParameters::RBMParameters( real the_learning_rate ) :
00055     learning_rate(the_learning_rate),
00056     going_up(true),
00057     pos_count(0),
00058     neg_count(0)
00059 {
00060 }
00061 
00062 RBMParameters::RBMParameters( string down_types, string up_types,
00063                               real the_learning_rate ) :
00064     up_units_types(up_types),
00065     down_units_types(down_types),
00066     learning_rate(the_learning_rate),
00067     going_up(true),
00068     pos_count(0),
00069     neg_count(0)
00070 {
00071     // We're not sure inherited::build() has been called
00072     build();
00073 }
00074 
00075 void RBMParameters::declareOptions(OptionList& ol)
00076 {
00077     // ### Declare all of this object's options here.
00078     // ### For the "flags" of each option, you should typically specify
00079     // ### one of OptionBase::buildoption, OptionBase::learntoption or
00080     // ### OptionBase::tuningoption. If you don't provide one of these three,
00081     // ### this option will be ignored when loading values from a script.
00082     // ### You can also combine flags, for example with OptionBase::nosave:
00083     // ### (OptionBase::buildoption | OptionBase::nosave)
00084 
00085     declareOption(ol, "up_units_types", &RBMParameters::up_units_types,
00086                   OptionBase::buildoption,
00087                   "Each character of this string describes the type of an"
00088                   " up unit:\n"
00089                   "  - 'l' if the energy function of this unit is linear\n"
00090                   "    (binomial or multinomial unit),\n"
00091                   "  - 'q' if it is quadratic (for a gaussian unit).\n");
00092 
00093     declareOption(ol, "down_units_types", &RBMParameters::down_units_types,
00094                   OptionBase::buildoption,
00095                   "Same meaning as 'up_units_types', but with down units");
00096 
00097     declareOption(ol, "learning_rate", &RBMParameters::learning_rate,
00098                   OptionBase::buildoption,
00099                   "The learning rate, used both in update() and bpropUpdate() "
00100                   "methods\n");
00101 
00102     declareOption(ol, "initialization_method",
00103                   &RBMParameters::initialization_method,
00104                   OptionBase::buildoption,
00105                   "The method used to initialize the weights:\n"
00106                   "  - \"uniform_linear\" = a uniform law in [-1/d, 1/d]\n"
00107                   "  - \"uniform_sqrt\"   = a uniform law in [-1/sqrt(d),"
00108                   " 1/sqrt(d)]\n"
00109                   "  - \"zero\"           = all weights are set to 0,\n"
00110                   "where d = max( up_layer_size, down_layer_size ).\n");
00111 
00112     // Now call the parent class' declareOptions
00113     inherited::declareOptions(ol);
00114 }
00115 
00116 void RBMParameters::build_()
00117 {
00118     up_layer_size = int(up_units_types.size());
00119     down_layer_size = int(down_units_types.size());
00120     if( up_layer_size == 0 || down_layer_size == 0 )
00121         return;
00122 
00123     string im = lowerstring( initialization_method );
00124     if( im == "" || im == "uniform_sqrt" )
00125         initialization_method = "uniform_sqrt";
00126     else if( im == "uniform_linear" )
00127         initialization_method = im;
00128     else if( im == "zero" )
00129         initialization_method = im;
00130     else
00131         PLERROR( "RBMParameters::build_ - initialization_method\n"
00132                  "\"%s\" unknown.\n", initialization_method.c_str() );
00133 
00134     input_size = down_layer_size;
00135     output_size = up_layer_size;
00136 }
00137 
00138 void RBMParameters::build()
00139 {
00140     inherited::build();
00141     build_();
00142 }
00143 
00144 
00145 void RBMParameters::makeDeepCopyFromShallowCopy(CopiesMap& copies)
00146 {
00147     inherited::makeDeepCopyFromShallowCopy(copies);
00148 
00149     // ### Call deepCopyField on all "pointer-like" fields
00150     // ### that you wish to be deepCopied rather than
00151     // ### shallow-copied.
00152 
00153     deepCopyField(input_vec, copies);
00154 }
00155 
00156 void RBMParameters::setAsUpInput( const Vec& input ) const
00157 {
00158     PLASSERT( input.size() == up_layer_size );
00159     input_vec = input;
00160     going_up = false;
00161 }
00162 
00163 void RBMParameters::setAsDownInput( const Vec& input ) const
00164 {
00165     PLASSERT( input.size() == down_layer_size );
00166     input_vec = input;
00167     going_up = true;
00168 }
00169 
00170 void RBMParameters::update( const Vec& pos_down_values,
00171                             const Vec& pos_up_values,
00172                             const Vec& neg_down_values,
00173                             const Vec& neg_up_values)
00174 {
00175     // Not-so-efficient implementation
00176     accumulatePosStats( pos_down_values, pos_up_values );
00177     accumulateNegStats( neg_down_values, neg_up_values );
00178     update();
00179 }
00180 
00182 void RBMParameters::fprop(const Vec& input, Vec& output) const
00183 {
00184     // propagates the activations.
00185     setAsDownInput( input );
00186     output.resize( output_size );
00187     computeUnitActivations( 0, output_size, output );
00188 }
00189 
00190 } // end of namespace PLearn
00191 
00192 
00193 /*
00194   Local Variables:
00195   mode:c++
00196   c-basic-offset:4
00197   c-file-style:"stroustrup"
00198   c-file-offsets:((innamespace . 0)(inline-open . 0))
00199   indent-tabs-mode:nil
00200   fill-column:79
00201   End:
00202 */
00203 // 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