PLearn 0.1
|
00001 // -*- C++ -*- 00002 00003 // LogaddOnBagsModule.cc 00004 // 00005 // Copyright (C) 2008 Jerome Louradour 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 // Author: Jerome Louradour 00036 00041 #include "LogaddOnBagsModule.h" 00042 00043 namespace PLearn { 00044 using namespace std; 00045 00046 PLEARN_IMPLEMENT_OBJECT( 00047 LogaddOnBagsModule, 00048 "For each input i, on a bag of size N, outputs:" 00049 " Logadd([ input_i(1), ..., input_i(N) ]).", 00050 "The 'input' port is typically connected to the output\n" 00051 "port of class activities (input_size = number of classes).\n" 00052 "see OnBagsModule for details on bags.\n"); 00053 00054 LogaddOnBagsModule::LogaddOnBagsModule() 00055 { 00056 output_size = -1; 00057 } 00058 00059 void LogaddOnBagsModule::declareOptions(OptionList& ol) 00060 { 00061 inherited::declareOptions(ol); 00062 00063 redeclareOption(ol, "output_size", &LogaddOnBagsModule::output_size, 00064 OptionBase::learntoption, 00065 "Size of the 'output' port (same as 'input')."); 00066 } 00067 00068 void LogaddOnBagsModule::build_() 00069 { 00070 PLASSERT( input_size > 0 ); 00071 output_size = input_size; 00072 accumulated_output.resize(output_size); 00073 inherited::build(); 00074 } 00075 00076 void LogaddOnBagsModule::build() 00077 { 00078 inherited::build(); 00079 build_(); 00080 } 00081 00082 void LogaddOnBagsModule::makeDeepCopyFromShallowCopy(CopiesMap& copies) 00083 { 00084 inherited::makeDeepCopyFromShallowCopy(copies); 00085 deepCopyField(accumulated_output, copies); 00086 } 00087 00089 // fprop // 00091 00092 void LogaddOnBagsModule::fpropInit(const Vec& input) 00093 { 00094 accumulated_output << input; 00095 } 00096 void LogaddOnBagsModule::fpropAcc(const Vec& input) 00097 { 00098 for( int i = 0; i < input_size; i++ ) 00099 accumulated_output[i] = logadd(accumulated_output[i], 00100 input[i]); 00101 } 00102 void LogaddOnBagsModule::fpropOutput(Vec& output) 00103 { 00104 output.resize( output_size ); 00105 output << accumulated_output; 00106 } 00107 00109 // bpropUpdate // 00111 00112 void LogaddOnBagsModule::bprop( const Mat& baginputs, 00113 const Vec& bagoutput_gradient, 00114 Mat& baginputs_gradients) 00115 { 00116 int nsamples = baginputs.length(); 00117 baginputs_gradients.resize( nsamples, input_size); 00118 for( int i = 0; i < input_size; i++ ) 00119 { 00120 Vec tmp_input_gradient; 00121 tmp_input_gradient.resize( nsamples ); 00122 softmax( baginputs.column(i).toVecCopy() , 00123 tmp_input_gradient ); 00124 tmp_input_gradient *= bagoutput_gradient[i]; 00125 baginputs_gradients.column(i) << tmp_input_gradient; 00126 } 00127 } 00128 00129 } // end of namespace PLearn 00130 00131 00132 /* 00133 Local Variables: 00134 mode:c++ 00135 c-basic-offset:4 00136 c-file-style:"stroustrup" 00137 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00138 indent-tabs-mode:nil 00139 fill-column:79 00140 End: 00141 */ 00142 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :