PLearn 0.1
SoftmaxLossVariable.cc
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // PLearn (A C++ Machine Learning Library)
00004 // Copyright (C) 1998 Pascal Vincent
00005 // Copyright (C) 1999-2002 Pascal Vincent, Yoshua Bengio, Rejean Ducharme and University of Montreal
00006 // Copyright (C) 2001-2002 Nicolas Chapados, Ichiro Takeuchi, Jean-Sebastien Senecal
00007 // Copyright (C) 2002 Xiangdong Wang, Christian Dorion
00008 
00009 // Redistribution and use in source and binary forms, with or without
00010 // modification, are permitted provided that the following conditions are met:
00011 // 
00012 //  1. Redistributions of source code must retain the above copyright
00013 //     notice, this list of conditions and the following disclaimer.
00014 // 
00015 //  2. Redistributions in binary form must reproduce the above copyright
00016 //     notice, this list of conditions and the following disclaimer in the
00017 //     documentation and/or other materials provided with the distribution.
00018 // 
00019 //  3. The name of the authors may not be used to endorse or promote
00020 //     products derived from this software without specific prior written
00021 //     permission.
00022 // 
00023 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
00024 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00025 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
00026 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00027 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00028 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00029 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00030 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00031 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00032 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00033 // 
00034 // This file is part of the PLearn library. For more information on the PLearn
00035 // library, go to the PLearn Web site at www.plearn.org
00036 
00037 
00038 /* *******************************************************      
00039  * $Id: SoftmaxLossVariable.cc 3994 2005-08-25 13:35:03Z chapados $
00040  * This file is part of the PLearn library.
00041  ******************************************************* */
00042 
00043 #include "ExpVariable.h"
00044 #include "RowAtPositionVariable.h"
00045 #include "SoftmaxLossVariable.h"
00046 #include "Var_operators.h"
00047 
00048 namespace PLearn {
00049 using namespace std;
00050 
00051 
00054 PLEARN_IMPLEMENT_OBJECT(SoftmaxLossVariable,
00055                         "ONE LINE DESCR",
00056                         "NO HELP");
00057 
00058 SoftmaxLossVariable::SoftmaxLossVariable(Variable* input1, Variable* input2) 
00059     : inherited(input1, input2, 1, 1)
00060 {
00061     build_();
00062 }
00063 
00064 void
00065 SoftmaxLossVariable::build()
00066 {
00067     inherited::build();
00068     build_();
00069 }
00070 
00071 void
00072 SoftmaxLossVariable::build_()
00073 {    
00074     if(input2 && !input2->isScalar())
00075         PLERROR("In RowAtPositionVariable: position must be a scalar");
00076 }
00077 
00078 void SoftmaxLossVariable::recomputeSize(int& l, int& w) const
00079 { l=1; w=1; }
00080 
00081 void SoftmaxLossVariable::fprop()
00082 {
00083     int classnum = (int)input2->valuedata[0];
00084     real input_index = input1->valuedata[classnum];
00085     real sum=0;
00086     for(int i=0; i<input1->nelems(); i++)
00087         sum += safeexp(input1->valuedata[i]-input_index);
00088     valuedata[0] = 1.0/sum;
00089 }
00090 
00091 
00092 void SoftmaxLossVariable::bprop()
00093 {
00094     int classnum = (int)input2->valuedata[0];
00095     real input_index = input1->valuedata[classnum];
00096     real vali = valuedata[0];
00097     for(int i=0; i<input1->nelems(); i++)
00098     {
00099         if (i!=classnum)
00100             //input1->gradientdata[i] = -gradientdata[i]/*?*/*vali*vali*safeexp(input1->valuedata[i]-input_index);
00101             input1->gradientdata[i] = -gradientdata[i]*vali*vali*safeexp(input1->valuedata[i]-input_index);
00102         else
00103             input1->gradientdata[i] = gradientdata[i]*vali*(1.-vali);
00104     }
00105 }
00106 
00107 
00108 void SoftmaxLossVariable::bbprop()
00109 {
00110     PLERROR("SofmaxVariable::bbprop() not implemented");
00111 }
00112 
00113 
00114 void SoftmaxLossVariable::symbolicBprop()
00115 {
00116     Var gi = -g * Var(this) * Var(this) * exp(input1-input1(input2));
00117     Var gindex = new RowAtPositionVariable(g * Var(this), input2, input1->length());
00118     input1->accg(gi+gindex);
00119 }
00120 
00121 
00122 // R{ s_i = exp(x_i) / sum_j exp(x_j) }   = (s_i(1-s_i) - sum_{k!=i} s_i s_k) R(s_i) = s_i ((1-s_i) - sum_{k!=i} s_k) R(s_i)
00123 void SoftmaxLossVariable::rfprop()
00124 {
00125     if (rValue.length()==0) resizeRValue();
00126 
00127     int classnum = (int)input2->valuedata[0];
00128     real input_index = input1->valuedata[classnum];
00129     real vali = valuedata[0];
00130     real sum = 0;
00131     for(int i=0; i<input1->nelems(); i++)
00132     {
00133         real res =vali * input1->rvaluedata[i];
00134         if (i != classnum)
00135             sum -= res * vali* safeexp(input1->valuedata[i]-input_index);
00136         else sum += res * (1 - vali);
00137     }
00138     rvaluedata[0] = sum;
00139 }
00140 
00141 
00142 
00143 } // end of namespace PLearn
00144 
00145 
00146 /*
00147   Local Variables:
00148   mode:c++
00149   c-basic-offset:4
00150   c-file-style:"stroustrup"
00151   c-file-offsets:((innamespace . 0)(inline-open . 0))
00152   indent-tabs-mode:nil
00153   fill-column:79
00154   End:
00155 */
00156 // 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