PLearn 0.1
NegLogProbCostFunction.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: NegLogProbCostFunction.cc 6128 2006-07-26 01:41:36Z lamblin $
00040  * This file is part of the PLearn library.
00041  ******************************************************* */
00042 
00043 #include "NegLogProbCostFunction.h"
00044 #include <plearn/math/TMat_maths.h>
00045 
00046 #if USING_MPI
00047 #include <plearn/sys/PLMPI.h>
00048 #endif
00049 
00050 namespace PLearn {
00051 using namespace std;
00052 
00053 
00054 
00055 PLEARN_IMPLEMENT_OBJECT(NegLogProbCostFunction, "ONE LINE DESCR", "NO HELP");
00056 
00057 
00058 #define smoothmap sigmoid
00059 real NegLogProbCostFunction::evaluate(const Vec& output, const Vec& target) const
00060 {
00061     real prob = 0.;
00062     int desired_class = int(target[0]);
00063     if (desired_class == -1) desired_class=0;
00064     if(output.length()==1) // we assume output[0] gives the probability of having class 1 
00065     {
00066         prob = output[0];
00067         if(smooth_map_outputs)
00068             prob = smoothmap(prob);
00069         if(desired_class==0)
00070             prob = 1-prob;
00071     }
00072     else 
00073     {
00074         if(!normalize) // we assume output gives a real probability for each class
00075 #if USING_MPI
00076 #define SEND_PROB_TAG 981
00077         {
00078             // EACH CPU ONLY CARRIES THE CORRECT outputs IN THE INTERVAL
00079             // GIVEN BY [out_start,out_end).
00080             // THE RESULT WILL BE THAT CPU 0 WILL HAVE
00081             // THE PROBABILITY OF desired_class IN prob
00082             // (and the other CPUs will have some dummy value)
00083             if (PLMPI::size>1 && out_end>=0)
00084             {
00085                 if (desired_class>=out_start && desired_class<out_end)
00086                 {
00087                     prob = output[desired_class];
00088                     if (PLMPI::rank>0) // send it to CPU 0
00089                     {
00090                         MPI_Send(&prob,1,PLMPI_REAL,0,SEND_PROB_TAG,MPI_COMM_WORLD);
00091                     }
00092                 }
00093                 else
00094                 {
00095                     if (PLMPI::rank==0)
00096                     {
00097                         MPI_Status status;
00098                         MPI_Recv(&prob,1,PLMPI_REAL,MPI_ANY_SOURCE,SEND_PROB_TAG,MPI_COMM_WORLD,&status);
00099                     }
00100                     else 
00101                     {
00102                         prob = 1; // dummy value (whose log exists)
00103                     }
00104                 }
00105             }
00106             else
00107                 prob = output[desired_class];
00108         }
00109 #else
00110         prob = output[desired_class];
00111 #endif
00112         else // outputs may not sum to 1, so we'll normalize them
00113         {
00114 #if USING_MPI
00115             if (PLMPI::size>1 && out_end>=0)
00116                 PLERROR("condprob used in parallel mode: normalize not implemented");
00117 #endif
00118             real* outputdata = output.data();
00119             if(smooth_map_outputs) // outputs may be slightly smaller than 0 or slightly larger than 1
00120             {                      // so we'll smooth-map them to fit in range [0,1] before normalizing 
00121                 real outputsum = 0.0;
00122                 for(int i=0; i<output.length(); i++)
00123                     outputsum += smoothmap(outputdata[i]);
00124                 prob = smoothmap(outputdata[desired_class])/outputsum;
00125             }
00126             else
00127                 prob = output[desired_class]/sum(output, false);
00128         }
00129     }
00130     return -safeflog(prob);
00131 }
00132 
00133 void NegLogProbCostFunction::declareOptions(OptionList &ol)
00134 {
00135     declareOption(ol, "normalize", &NegLogProbCostFunction::normalize, OptionBase::buildoption,
00136                   "TODO: Some comments");
00137     declareOption(ol, "smooth_map_outputs", &NegLogProbCostFunction::smooth_map_outputs, OptionBase::buildoption,
00138                   "TODO: Some comments");
00139     inherited::declareOptions(ol);
00140 }
00141 
00142 } // end of namespace PLearn
00143 
00144 
00145 /*
00146   Local Variables:
00147   mode:c++
00148   c-basic-offset:4
00149   c-file-style:"stroustrup"
00150   c-file-offsets:((innamespace . 0)(inline-open . 0))
00151   indent-tabs-mode:nil
00152   fill-column:79
00153   End:
00154 */
00155 // 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