PLearn 0.1
|
00001 // -*- C++ -*- 00002 00003 // LogSoftSoftMaxVariable.cc 00004 // 00005 // Copyright (C) 2007 Simon Lemieux, 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: Simon Lemieux, Pascal Vincent 00036 00040 #include "LogSoftSoftMaxVariable.h" 00041 00042 namespace PLearn { 00043 using namespace std; 00044 00047 PLEARN_IMPLEMENT_OBJECT( 00048 LogSoftSoftMaxVariable, 00049 "Kind of softmax variable", 00050 "Let X:=input1, A:=input2\nThen output(n,k) = X(n,k) - log(sum_j{ exp[ X(n,j) + A(k,j) ] })" 00051 ); 00052 00053 00054 // constructor from input variables. 00055 LogSoftSoftMaxVariable::LogSoftSoftMaxVariable(Variable* input1, Variable* input2) 00056 : inherited(input1, input2, input1->length(), input1->width()) 00057 { 00058 build_(); 00059 } 00060 00061 00062 void LogSoftSoftMaxVariable::recomputeSize(int& l, int& w) const 00063 { 00064 if (input1) { 00065 l = input1->length(); // the computed length of this Var 00066 w = input1->width(); // the computed width 00067 } else 00068 l = w = 0; 00069 } 00070 00071 // ### computes value from input1 and input2 values 00072 void LogSoftSoftMaxVariable::fprop() 00073 { 00074 Mat X = input1->matValue, 00075 A = input2->matValue; 00076 00077 real sum; 00078 00079 for (int n=0; n<X.length(); n++) 00080 for (int k=0; k<X.width(); k++) 00081 { 00082 sum=0; 00083 for (int i=0; i<X.width(); i++) 00084 sum += safeexp(X(n,i) + A(k,i)); 00085 matValue(n,k) = X(n,k) - safelog(sum); 00086 } 00087 } 00088 00089 // ### computes input1 and input2 gradients from gradient 00090 void LogSoftSoftMaxVariable::bprop() 00091 { 00092 Mat X = input1->matValue, 00093 A = input2->matValue, 00094 grad_X = input1->matGradient, 00095 grad_A = input2->matGradient; 00096 00097 real temp; 00098 00099 //chacun des exemples de X 00100 for (int n=0; n<X.length(); n++) 00101 //chaque coordonné dun exemple //correspond au gradient 00102 for (int k=0; k<X.width(); k++) 00103 //même exemple, coordonnée aussi // correspond à un exemple 00104 for (int j=0; j<X.width(); j++) 00105 { 00106 temp = matGradient(n,j)*safeexp(matValue(n,j) + X(n,k) + A(j,k) - X(n,j)); 00107 00108 if(k==j) 00109 grad_X(n,k) += matGradient(n,j)*(1.-safeexp(matValue(n,k))); 00110 else 00111 grad_X(n,k) -= temp; 00112 00113 grad_A(j,k) -= temp; 00114 } 00115 } 00116 00117 // ### You can implement these methods: 00118 // void LogSoftSoftMaxVariable::bbprop() {} 00119 // void LogSoftSoftMaxVariable::symbolicBprop() {} 00120 // void LogSoftSoftMaxVariable::rfprop() {} 00121 00122 00123 // ### Nothing to add here, simply calls build_ 00124 void LogSoftSoftMaxVariable::build() 00125 { 00126 inherited::build(); 00127 build_(); 00128 } 00129 00130 void LogSoftSoftMaxVariable::makeDeepCopyFromShallowCopy(CopiesMap& copies) 00131 { 00132 inherited::makeDeepCopyFromShallowCopy(copies); 00133 00134 // ### Call deepCopyField on all "pointer-like" fields 00135 // ### that you wish to be deepCopied rather than 00136 // ### shallow-copied. 00137 // ### ex: 00138 // deepCopyField(trainvec, copies); 00139 00140 // ### If you want to deepCopy a Var field: 00141 // varDeepCopyField(somevariable, copies); 00142 00143 // ### Remove this line when you have fully implemented this method. 00144 PLERROR("LogSoftSoftMaxVariable::makeDeepCopyFromShallowCopy not fully (correctly) implemented yet!"); 00145 } 00146 00147 void LogSoftSoftMaxVariable::declareOptions(OptionList& ol) 00148 { 00149 // ### Declare all of this object's options here. 00150 // ### For the "flags" of each option, you should typically specify 00151 // ### one of OptionBase::buildoption, OptionBase::learntoption or 00152 // ### OptionBase::tuningoption. If you don't provide one of these three, 00153 // ### this option will be ignored when loading values from a script. 00154 // ### You can also combine flags, for example with OptionBase::nosave: 00155 // ### (OptionBase::buildoption | OptionBase::nosave) 00156 00157 // ### ex: 00158 // declareOption(ol, "myoption", &LogSoftSoftMaxVariable::myoption, 00159 // OptionBase::buildoption, 00160 // "Help text describing this option"); 00161 // ... 00162 00163 // Now call the parent class' declareOptions 00164 inherited::declareOptions(ol); 00165 } 00166 00167 void LogSoftSoftMaxVariable::build_() 00168 { 00169 // ### This method should do the real building of the object, 00170 // ### according to set 'options', in *any* situation. 00171 // ### Typical situations include: 00172 // ### - Initial building of an object from a few user-specified options 00173 // ### - Building of a "reloaded" object: i.e. from the complete set of 00174 // ### all serialised options. 00175 // ### - Updating or "re-building" of an object after a few "tuning" 00176 // ### options have been modified. 00177 // ### You should assume that the parent class' build_() has already been 00178 // ### called. 00179 00180 if (input2.length() != input1.width() || input2.width() != input1.width()) 00181 PLERROR("Length and width of input2 must be equal to width of input1 in LogSoftSoftMaxVariable"); 00182 } 00183 00184 00185 } // end of namespace PLearn 00186 00187 00188 /* 00189 Local Variables: 00190 mode:c++ 00191 c-basic-offset:4 00192 c-file-style:"stroustrup" 00193 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00194 indent-tabs-mode:nil 00195 fill-column:79 00196 End: 00197 */ 00198 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :