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

NegCrossEntropySigmoidVariable.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 // Copyright (C) 2003 Olivier Delalleau 00009 00010 // Redistribution and use in source and binary forms, with or without 00011 // modification, are permitted provided that the following conditions are met: 00012 // 00013 // 1. Redistributions of source code must retain the above copyright 00014 // notice, this list of conditions and the following disclaimer. 00015 // 00016 // 2. Redistributions in binary form must reproduce the above copyright 00017 // notice, this list of conditions and the following disclaimer in the 00018 // documentation and/or other materials provided with the distribution. 00019 // 00020 // 3. The name of the authors may not be used to endorse or promote 00021 // products derived from this software without specific prior written 00022 // permission. 00023 // 00024 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 00025 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00026 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 00027 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00028 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00029 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00030 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00031 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00032 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00033 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00034 // 00035 // This file is part of the PLearn library. For more information on the PLearn 00036 // library, go to the PLearn Web site at www.plearn.org 00037 00038 00039 /* ******************************************************* 00040 * $Id: NegCrossEntropySigmoidVariable.cc,v 1.5 2004/04/27 15:58:16 morinf Exp $ 00041 * This file is part of the PLearn library. 00042 ******************************************************* */ 00043 00044 #include "NegCrossEntropySigmoidVariable.h" 00045 00046 namespace PLearn { 00047 using namespace std; 00048 00051 PLEARN_IMPLEMENT_OBJECT(NegCrossEntropySigmoidVariable, 00052 "Compute sigmoid of its first input, and then computes the negative " 00053 "cross-entropy cost", 00054 "NO HELP"); 00055 00057 // NegCrossEntropySigmoidVariable // 00059 NegCrossEntropySigmoidVariable::NegCrossEntropySigmoidVariable(Variable* netout, Variable* target, real regularizer_) 00060 : inherited(netout,target,1,1),regularizer(regularizer_) 00061 { 00062 build_(); 00063 } 00064 00065 void 00066 NegCrossEntropySigmoidVariable::build() 00067 { 00068 inherited::build(); 00069 build_(); 00070 } 00071 00072 void 00073 NegCrossEntropySigmoidVariable::build_() 00074 { 00075 if (input1 && input2) { 00076 // input1 and input2 are (respectively) netout and target from constructor 00077 if(input1->size() != input2->size()) 00078 PLERROR("In NegCrossEntropySigmoidVariable: netout and target must have the same size"); 00079 } 00080 } 00081 00083 // recomputeSize // 00085 void NegCrossEntropySigmoidVariable::recomputeSize(int& l, int& w) const 00086 { l=1, w=1; } 00087 00089 // fprop // 00091 void NegCrossEntropySigmoidVariable::fprop() 00092 { 00093 real cost = 0.0; 00094 for (int i=0; i<input1->size(); i++) 00095 { 00096 real output = sigmoid(input1->valuedata[i]); 00097 real target = input2->valuedata[i]; 00098 if (output == 0.0) { 00099 if (target == 1.0) { 00100 PLWARNING("NegCrossEntropySigmoidVariable::fprop: model output is 0 and target is 1, cost should be infinite !"); 00101 cost += -1e9; 00102 } // If target == 0.0 do nothing, cost is 0. 00103 } else if (output == 1.0) { 00104 if (target == 0.0) { 00105 PLWARNING("NegCrossEntropySigmoidVariable::fprop: model output is 1 and target is 0, cost should be infinite !"); 00106 cost += -1e9; 00107 } // If target == 1.0 do nothing, cost is 0. 00108 } else { 00109 if (!regularizer) { 00110 // Standard cross entropy. 00111 cost += target*log(output) + (1.0-target)*log(1.0-output); 00112 } else { 00113 // Regularized cross entropy. 00114 cost += target*((1 - regularizer) * log(output) + regularizer * log(1.0 - output)) + 00115 (1.0-target)*((1 - regularizer) * log(1.0-output) + regularizer * log(output)); 00116 } 00117 } 00118 } 00119 valuedata[0] = -cost; 00120 } 00121 00123 // bprop // 00125 void NegCrossEntropySigmoidVariable::bprop() 00126 { 00127 real gr = *gradientdata; 00128 for (int i=0; i<input1->size(); i++) 00129 { 00130 real output = sigmoid(input1->valuedata[i]); 00131 real target = input2->valuedata[i]; 00132 if (!regularizer) { 00133 // Standard cross entropy. 00134 input1->gradientdata[i] += gr*(output - target); 00135 } else { 00136 // Regularized cross entropy. 00137 if (target == 0.0) { 00138 input1->gradientdata[i] += gr*((1-regularizer) * output - regularizer * (1-output)); 00139 } else if (target == 1.0) { 00140 input1->gradientdata[i] += gr*(regularizer * output - (1-regularizer) * (1-output)); 00141 } else { 00142 PLERROR("NegCrossEntropySigmoidVariable::bprop: target is neither 0 nor 1"); 00143 } 00144 } 00145 } 00146 } 00147 00149 // setRegularizer // 00151 void NegCrossEntropySigmoidVariable::setRegularizer(real r) 00152 { 00153 PLWARNING("NegCrossEntropySigmoidVariable::setRegularizer() has been deprecated, use the setOption() method instead"); 00154 this->regularizer = r; 00155 } 00156 00157 } // end of namespace PLearn

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