Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members

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

Generated on Tue Aug 17 15:59:36 2004 for PLearn by doxygen 1.3.7