PLearn 0.1
|
00001 // -*- C++ -*- 00002 00003 // SumEntropyOfBernoullis.cc 00004 // 00005 // Copyright (C) 2009 Pascal Vincent 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 Vincent 00036 00040 #include "SumEntropyOfBernoullis.h" 00041 00042 namespace PLearn { 00043 using namespace std; 00044 00047 PLEARN_IMPLEMENT_OBJECT( 00048 SumEntropyOfBernoullis, 00049 "Computes the sum of the entropies of independent Bernoulli variables whose parameters are given as input.", 00050 "i.e. if input is a matrix P, computes sum_ij - [P_ij log(P_ij) + (1-P_ij) log(1-P_ij)]" 00051 ); 00052 00053 SumEntropyOfBernoullis::SumEntropyOfBernoullis() 00054 : inherited(0, 1, 1) 00055 { 00056 } 00057 00058 // constructor from input variable. 00059 SumEntropyOfBernoullis::SumEntropyOfBernoullis(Variable* input) 00060 : inherited(input, 1, 1) 00061 { 00062 build_(); 00063 } 00064 00065 // constructor from input variable and parameters 00066 // SumEntropyOfBernoullis::SumEntropyOfBernoullis(Variable* input, param_type the_parameter,...) 00067 // ### replace with actual parameters 00068 // : inherited(input, this_variable_length, this_variable_width), 00069 // parameter(the_parameter), 00070 // ... 00071 //{ 00072 // // ### You may (or not) want to call build_() to finish building the 00073 // // ### object 00074 //} 00075 00076 void SumEntropyOfBernoullis::recomputeSize(int& l, int& w) const 00077 { 00078 l = w = 1; 00079 } 00080 00081 // ### computes value from input's value 00082 void SumEntropyOfBernoullis::fprop() 00083 { 00084 double res = 0; 00085 Mat P = input->matValue; 00086 for(int i=0; i<P.length(); i++) 00087 { 00088 const real* P_i = P[i]; 00089 for(int j=0; j<P.width(); j++) 00090 { 00091 double P_ij = P_i[j]; 00092 if( (P_ij>1e-25) && ((1-P_ij)>1e-25) ) 00093 res -= P_ij*pl_log(P_ij) + (1-P_ij)*pl_log(1-P_ij); 00094 } 00095 } 00096 valuedata[0] = (real)res; 00097 } 00098 00099 // ### computes input's gradient from gradient 00100 void SumEntropyOfBernoullis::bprop() 00101 { 00102 /* 00103 -[p log(p) + (1-p) log(1-p)]' = -[log(p) + 1 - log(1-p) - 1 ] 00104 = log(1-p) - log(p) 00105 */ 00106 real gr = gradientdata[0]; 00107 Mat P = input->matValue; 00108 Mat Pgr = input->matGradient; 00109 for(int i=0; i<Pgr.length(); i++) 00110 { 00111 const real* P_i = P[i]; 00112 real* Pgr_i = Pgr[i]; 00113 for(int j=0; j<Pgr.width(); j++) 00114 { 00115 double P_ij = P_i[j]; 00116 double one_minus_P_ij = 1-P_ij; 00117 if(P_ij<1e-25) 00118 P_ij = 1e-25; 00119 if(one_minus_P_ij<1e-25) 00120 one_minus_P_ij = 1e-25; 00121 Pgr_i[j] += gr*( pl_log(one_minus_P_ij) - pl_log(P_ij) ); 00122 } 00123 } 00124 00125 00126 } 00127 00128 // ### You can implement these methods: 00129 // void SumEntropyOfBernoullis::bbprop() {} 00130 // void SumEntropyOfBernoullis::symbolicBprop() {} 00131 // void SumEntropyOfBernoullis::rfprop() {} 00132 00133 00134 // ### Nothing to add here, simply calls build_ 00135 void SumEntropyOfBernoullis::build() 00136 { 00137 inherited::build(); 00138 build_(); 00139 } 00140 00141 void SumEntropyOfBernoullis::makeDeepCopyFromShallowCopy(CopiesMap& copies) 00142 { 00143 inherited::makeDeepCopyFromShallowCopy(copies); 00144 } 00145 00146 void SumEntropyOfBernoullis::declareOptions(OptionList& ol) 00147 { 00148 // ### Declare all of this object's options here. 00149 // ### For the "flags" of each option, you should typically specify 00150 // ### one of OptionBase::buildoption, OptionBase::learntoption or 00151 // ### OptionBase::tuningoption. If you don't provide one of these three, 00152 // ### this option will be ignored when loading values from a script. 00153 // ### You can also combine flags, for example with OptionBase::nosave: 00154 // ### (OptionBase::buildoption | OptionBase::nosave) 00155 00156 // ### ex: 00157 // declareOption(ol, "myoption", &SumEntropyOfBernoullis::myoption, 00158 // OptionBase::buildoption, 00159 // "Help text describing this option"); 00160 // ... 00161 00162 // Now call the parent class' declareOptions 00163 inherited::declareOptions(ol); 00164 } 00165 00166 void SumEntropyOfBernoullis::build_() 00167 { 00168 // ### This method should do the real building of the object, 00169 // ### according to set 'options', in *any* situation. 00170 // ### Typical situations include: 00171 // ### - Initial building of an object from a few user-specified options 00172 // ### - Building of a "reloaded" object: i.e. from the complete set of 00173 // ### all serialised options. 00174 // ### - Updating or "re-building" of an object after a few "tuning" 00175 // ### options have been modified. 00176 // ### You should assume that the parent class' build_() has already been 00177 // ### called. 00178 } 00179 00180 00181 } // end of namespace PLearn 00182 00183 00184 /* 00185 Local Variables: 00186 mode:c++ 00187 c-basic-offset:4 00188 c-file-style:"stroustrup" 00189 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00190 indent-tabs-mode:nil 00191 fill-column:79 00192 End: 00193 */ 00194 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :