PLearn 0.1
NegLogPoissonVariable.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: NegLogPoissonVariable.cc 4412 2005-11-02 19:00:20Z tihocan $
00040  * This file is part of the PLearn library.
00041  ******************************************************* */
00042 
00043 #include "NegLogPoissonVariable.h"
00044 
00045 namespace PLearn {
00046 using namespace std;
00047 
00050 PLEARN_IMPLEMENT_OBJECT(NegLogPoissonVariable,
00051                         "Negative loglikelihood of the poisson distribution",
00052                         "Negative loglikelihood of the poisson distribution\n"
00053                         "cost_i = {exp(output_i) - output_i * target_i + log(target_i!)} / weight_i\n"
00054                         "cost   = sum_i weight_i * cost_i\n"
00055                         "Thus, wrt usual notation, output_i = log(lambda_i)\n"
00056                         "Note that, in order to comply with regular procedures, cost_i\n"
00057                         "is divided by weight_i and multiplied back when computing the weighted cost");
00058 
00059 // We can link the notation used above to the usual
00060 // statistical notation in the following way:
00061 // lambda = exp(output_i) 
00062 // where exponentiation ensures a positive value for lambda
00063 // and k = target_i, the number of observed events
00064 // Thus, the cost corresponds to the negative log likelihood
00065 // of the Poisson density
00066 
00067 NegLogPoissonVariable::NegLogPoissonVariable(VarArray& the_varray)
00068     : inherited(the_varray, 1, 1)
00069 {
00070     build_();
00071 }
00072 
00073 void
00074 NegLogPoissonVariable::build()
00075 {
00076     inherited::build();
00077     build_();
00078 }
00079 
00080 void
00081 NegLogPoissonVariable::build_()
00082 {
00083     if (varray[0] && varray[1] && (varray[0]->size() != varray[1]->size()))
00084         PLERROR("In NegLogPoissonVariable: netout and target must have the same size");
00085     if (varray.size()>2 && varray[0] && varray[2] && (varray[0]->size() != varray[2]->size()))
00086         PLERROR("In NegLogPoissonVariable: netout and weight must have the same size");
00087 }
00088 
00089 void NegLogPoissonVariable::recomputeSize(int& l, int& w) const
00090 { l=1, w=1; }
00091 
00092 void NegLogPoissonVariable::fprop()
00093 {
00094     real cost = 0.0;
00095     for (int i=0; i<varray[0]->size(); i++)
00096     {
00097         real output = varray[0]->valuedata[i];
00098         real target = varray[1]->valuedata[i];
00099         real weight = 1;
00100         if (varray.size()>2)
00101             weight = varray[2]->valuedata[i];
00102 
00103         static real EPSILON = 1e-8;          // Regularization
00104         real log_fact_target = pl_gammln(max(target, EPSILON)+1);
00105 //        cost += exp(output) * weight - (output + pl_log(weight) ) * target + log_fact_target;
00106 
00107         if (weight > 0)
00108             cost += exp(output) - (output + pl_log(weight)) * target / weight +
00109                     log_fact_target / weight;
00110     }
00111     if (is_missing(cost))
00112         PLERROR("NegLogPoissonVariable::fprop: encountered NaN cost");
00113     
00114     valuedata[0] = cost;
00115 }
00116 
00117 void NegLogPoissonVariable::bprop()
00118 {
00119     real gr = gradient[0];
00120     for (int i=0; i<varray[0]->size(); i++)
00121     {
00122         real output = varray[0]->valuedata[i];
00123         real target = varray[1]->valuedata[i];
00124         real weight = 1;
00125         if (varray.size()>2)
00126             weight = varray[2]->valuedata[i];
00127 
00128         if (weight > 0)
00129             varray[0]->gradientdata[i] += gr * ( exp(output) - target / weight);
00130 //        varray[0]->gradientdata[i] += gr* ( exp(output) * weight - target);
00131     }
00132 }
00133 
00134 } // end of namespace PLearn
00135 
00136 
00137 /*
00138   Local Variables:
00139   mode:c++
00140   c-basic-offset:4
00141   c-file-style:"stroustrup"
00142   c-file-offsets:((innamespace . 0)(inline-open . 0))
00143   indent-tabs-mode:nil
00144   fill-column:79
00145   End:
00146 */
00147 // 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