PLearn 0.1
|
#include <NegCrossEntropySigmoidVariable.h>
Public Member Functions | |
NegCrossEntropySigmoidVariable () | |
Default constructor for persistence. | |
NegCrossEntropySigmoidVariable (Variable *netout, Variable *target, real regularizer_=0, bool ignore_missing_=false) | |
virtual string | classname () const |
virtual OptionList & | getOptionList () const |
virtual OptionMap & | getOptionMap () const |
virtual RemoteMethodMap & | getRemoteMethodMap () const |
virtual NegCrossEntropySigmoidVariable * | deepCopy (CopiesMap &copies) const |
virtual void | build () |
Post-constructor. | |
virtual void | recomputeSize (int &l, int &w) const |
Recomputes the length l and width w that this variable should have, according to its parent variables. | |
virtual void | fprop () |
compute output given input | |
virtual void | bprop () |
void | setRegularizer (real r) |
Deprecated. | |
Static Public Member Functions | |
static string | _classname_ () |
NegCrossEntropySigmoidVariable. | |
static OptionList & | _getOptionList_ () |
static RemoteMethodMap & | _getRemoteMethodMap_ () |
static Object * | _new_instance_for_typemap_ () |
static bool | _isa_ (const Object *o) |
static void | _static_initialize_ () |
static const PPath & | declaringFile () |
Static Public Attributes | |
static StaticInitializer | _static_initializer_ |
Protected Member Functions | |
void | build_ () |
This does the actual building. | |
Static Protected Member Functions | |
static void | declareOptions (OptionList &ol) |
Declare options (data fields) for the class. | |
Protected Attributes | |
real | regularizer |
If > 0, will modify the cost function to: (1-t)(r*log(o)+(1-r)*log(1-o)) + t*(r*log(1-o)+(1-r)*log(o)) (t = target, o = output, r = regularizer = a small value) | |
bool | ignore_missing |
Indication that missing targets should be ignored. | |
Private Types | |
typedef BinaryVariable | inherited |
Definition at line 52 of file NegCrossEntropySigmoidVariable.h.
typedef BinaryVariable PLearn::NegCrossEntropySigmoidVariable::inherited [private] |
Reimplemented from PLearn::BinaryVariable.
Definition at line 54 of file NegCrossEntropySigmoidVariable.h.
PLearn::NegCrossEntropySigmoidVariable::NegCrossEntropySigmoidVariable | ( | ) | [inline] |
Default constructor for persistence.
Definition at line 66 of file NegCrossEntropySigmoidVariable.h.
: regularizer(0.0), ignore_missing(false) {}
PLearn::NegCrossEntropySigmoidVariable::NegCrossEntropySigmoidVariable | ( | Variable * | netout, |
Variable * | target, | ||
real | regularizer_ = 0 , |
||
bool | ignore_missing_ = false |
||
) |
Definition at line 60 of file NegCrossEntropySigmoidVariable.cc.
References build_().
: inherited(netout,target,1,1),regularizer(regularizer_), ignore_missing(ignore_missing_) { build_(); }
string PLearn::NegCrossEntropySigmoidVariable::_classname_ | ( | ) | [static] |
NegCrossEntropySigmoidVariable.
Reimplemented from PLearn::BinaryVariable.
Definition at line 55 of file NegCrossEntropySigmoidVariable.cc.
OptionList & PLearn::NegCrossEntropySigmoidVariable::_getOptionList_ | ( | ) | [static] |
Reimplemented from PLearn::BinaryVariable.
Definition at line 55 of file NegCrossEntropySigmoidVariable.cc.
RemoteMethodMap & PLearn::NegCrossEntropySigmoidVariable::_getRemoteMethodMap_ | ( | ) | [static] |
Reimplemented from PLearn::BinaryVariable.
Definition at line 55 of file NegCrossEntropySigmoidVariable.cc.
Reimplemented from PLearn::BinaryVariable.
Definition at line 55 of file NegCrossEntropySigmoidVariable.cc.
Object * PLearn::NegCrossEntropySigmoidVariable::_new_instance_for_typemap_ | ( | ) | [static] |
Reimplemented from PLearn::Object.
Definition at line 55 of file NegCrossEntropySigmoidVariable.cc.
StaticInitializer NegCrossEntropySigmoidVariable::_static_initializer_ & PLearn::NegCrossEntropySigmoidVariable::_static_initialize_ | ( | ) | [static] |
Reimplemented from PLearn::BinaryVariable.
Definition at line 55 of file NegCrossEntropySigmoidVariable.cc.
void PLearn::NegCrossEntropySigmoidVariable::bprop | ( | ) | [virtual] |
Implements PLearn::Variable.
Definition at line 146 of file NegCrossEntropySigmoidVariable.cc.
References PLearn::fast_exact_is_equal(), PLearn::Variable::gradientdata, i, ignore_missing, PLearn::BinaryVariable::input1, PLearn::BinaryVariable::input2, PLearn::is_missing(), PLERROR, regularizer, and PLearn::sigmoid().
{ real gr = *gradientdata; for (int i=0; i<input1->size(); i++) { real output = sigmoid(input1->valuedata[i]); real target = input2->valuedata[i]; if(!ignore_missing || !is_missing(target)) { if (fast_exact_is_equal(regularizer, 0)) { // Standard cross entropy. input1->gradientdata[i] += gr*(output - target); } else { // Regularized cross entropy. if (fast_exact_is_equal(target, 0.0)) { input1->gradientdata[i] += gr*((1-regularizer) * output - regularizer * (1-output)); } else if (fast_exact_is_equal(target, 1.0)) { input1->gradientdata[i] += gr*(regularizer * output - (1-regularizer) * (1-output)); } else { PLERROR("NegCrossEntropySigmoidVariable::bprop: target is neither 0 nor 1"); } } } } }
void PLearn::NegCrossEntropySigmoidVariable::build | ( | ) | [virtual] |
Post-constructor.
The normal implementation should call simply inherited::build(), then this class's build_(). This method should be callable again at later times, after modifying some option fields to change the "architecture" of the object.
Reimplemented from PLearn::BinaryVariable.
Definition at line 85 of file NegCrossEntropySigmoidVariable.cc.
References PLearn::BinaryVariable::build(), and build_().
{ inherited::build(); build_(); }
void PLearn::NegCrossEntropySigmoidVariable::build_ | ( | ) | [protected] |
This does the actual building.
Reimplemented from PLearn::BinaryVariable.
Definition at line 91 of file NegCrossEntropySigmoidVariable.cc.
References PLearn::BinaryVariable::input1, PLearn::BinaryVariable::input2, and PLERROR.
Referenced by build(), and NegCrossEntropySigmoidVariable().
{ if (input1 && input2) { // input1 and input2 are (respectively) netout and target from constructor if(input1->size() != input2->size()) PLERROR("In NegCrossEntropySigmoidVariable: netout and target must have the same size"); } }
string PLearn::NegCrossEntropySigmoidVariable::classname | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 55 of file NegCrossEntropySigmoidVariable.cc.
void PLearn::NegCrossEntropySigmoidVariable::declareOptions | ( | OptionList & | ol | ) | [static, protected] |
Declare options (data fields) for the class.
Redefine this in subclasses: call declareOption
(...) for each option, and then call inherited::declareOptions(options)
. Please call the inherited
method AT THE END to get the options listed in a consistent order (from most recently defined to least recently defined).
static void MyDerivedClass::declareOptions(OptionList& ol) { declareOption(ol, "inputsize", &MyObject::inputsize_, OptionBase::buildoption, "The size of the input; it must be provided"); declareOption(ol, "weights", &MyObject::weights, OptionBase::learntoption, "The learned model weights"); inherited::declareOptions(ol); }
ol | List of options that is progressively being constructed for the current class. |
Reimplemented from PLearn::BinaryVariable.
Definition at line 69 of file NegCrossEntropySigmoidVariable.cc.
References PLearn::OptionBase::buildoption, PLearn::declareOption(), PLearn::BinaryVariable::declareOptions(), ignore_missing, and regularizer.
{ declareOption(ol, "regularizer", &NegCrossEntropySigmoidVariable::regularizer, OptionBase::buildoption, "If > 0, will modify the cost function to: \n" "(1-t)(r*log(o)+(1-r)*log(1-o)) + t*(r*log(1-o)+(1-r)*log(o)) \n" "(t = target, o = output, r = regularizer = a small value)\n"); declareOption(ol, "ignore_missing", &NegCrossEntropySigmoidVariable::ignore_missing, OptionBase::buildoption, "Indication that missing targets should be ignored"); inherited::declareOptions(ol); }
static const PPath& PLearn::NegCrossEntropySigmoidVariable::declaringFile | ( | ) | [inline, static] |
Reimplemented from PLearn::BinaryVariable.
Definition at line 71 of file NegCrossEntropySigmoidVariable.h.
: static void declareOptions(OptionList & ol);
NegCrossEntropySigmoidVariable * PLearn::NegCrossEntropySigmoidVariable::deepCopy | ( | CopiesMap & | copies | ) | const [virtual] |
Reimplemented from PLearn::BinaryVariable.
Definition at line 55 of file NegCrossEntropySigmoidVariable.cc.
void PLearn::NegCrossEntropySigmoidVariable::fprop | ( | ) | [virtual] |
compute output given input
Implements PLearn::Variable.
Definition at line 109 of file NegCrossEntropySigmoidVariable.cc.
References PLearn::fast_exact_is_equal(), i, ignore_missing, PLearn::BinaryVariable::input1, PLearn::BinaryVariable::input2, PLearn::is_missing(), pl_log, PLWARNING, regularizer, PLearn::sigmoid(), and PLearn::Variable::valuedata.
{ real cost = 0.0; for (int i=0; i<input1->size(); i++) { real output = sigmoid(input1->valuedata[i]); real target = input2->valuedata[i]; if(!ignore_missing || !is_missing(target)) { if (fast_exact_is_equal(output,0.0)) { if (fast_exact_is_equal(target, 1.0)) { PLWARNING("NegCrossEntropySigmoidVariable::fprop: model output is 0 and target is 1, cost should be infinite !"); cost += -1e9; } // If target == 0.0 do nothing, cost is 0. } else if (fast_exact_is_equal(output, 1.0)) { if (fast_exact_is_equal(target, 0.0)) { PLWARNING("NegCrossEntropySigmoidVariable::fprop: model output is 1 and target is 0, cost should be infinite !"); cost += -1e9; } // If target == 1.0 do nothing, cost is 0. } else { if (fast_exact_is_equal(regularizer, 0)) { // Standard cross entropy. cost += target*pl_log(output) + (1.0-target)*pl_log(1.0-output); } else { // Regularized cross entropy. cost += target*((1 - regularizer) * pl_log(output) + regularizer * pl_log(1.0 - output)) + (1.0-target)*((1 - regularizer) * pl_log(1.0-output) + regularizer * pl_log(output)); } } } } valuedata[0] = -cost; }
OptionList & PLearn::NegCrossEntropySigmoidVariable::getOptionList | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 55 of file NegCrossEntropySigmoidVariable.cc.
OptionMap & PLearn::NegCrossEntropySigmoidVariable::getOptionMap | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 55 of file NegCrossEntropySigmoidVariable.cc.
RemoteMethodMap & PLearn::NegCrossEntropySigmoidVariable::getRemoteMethodMap | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 55 of file NegCrossEntropySigmoidVariable.cc.
Recomputes the length l and width w that this variable should have, according to its parent variables.
This is used for ex. by sizeprop() The default version stupidly returns the current dimensions, so make sure to overload it in subclasses if this is not appropriate.
Reimplemented from PLearn::Variable.
Definition at line 103 of file NegCrossEntropySigmoidVariable.cc.
{ l=1, w=1; }
void PLearn::NegCrossEntropySigmoidVariable::setRegularizer | ( | real | r | ) |
Deprecated.
Definition at line 176 of file NegCrossEntropySigmoidVariable.cc.
References PLWARNING, and regularizer.
{ PLWARNING("NegCrossEntropySigmoidVariable::setRegularizer() has been deprecated, use the setOption() method instead"); this->regularizer = r; }
Reimplemented from PLearn::BinaryVariable.
Definition at line 71 of file NegCrossEntropySigmoidVariable.h.
Indication that missing targets should be ignored.
Definition at line 62 of file NegCrossEntropySigmoidVariable.h.
Referenced by bprop(), declareOptions(), and fprop().
If > 0, will modify the cost function to: (1-t)(r*log(o)+(1-r)*log(1-o)) + t*(r*log(1-o)+(1-r)*log(o)) (t = target, o = output, r = regularizer = a small value)
Definition at line 60 of file NegCrossEntropySigmoidVariable.h.
Referenced by bprop(), declareOptions(), fprop(), and setRegularizer().