00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
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
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
00077
if(input1->size() != input2->size())
00078
PLERROR(
"In NegCrossEntropySigmoidVariable: netout and target must have the same size");
00079 }
00080 }
00081
00083
00085 void NegCrossEntropySigmoidVariable::recomputeSize(
int& l,
int& w)
const
00086
{ l=1, w=1; }
00087
00089
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 }
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 }
00108 }
else {
00109
if (!
regularizer) {
00110
00111 cost += target*
log(output) + (1.0-target)*
log(1.0-output);
00112 }
else {
00113
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
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
00134 input1->gradientdata[i] += gr*(output - target);
00135 }
else {
00136
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
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 }