PLearn 0.1
|
#include <MarginPerceptronCostVariable.h>
Public Member Functions | |
MarginPerceptronCostVariable () | |
Default constructor for persistence. | |
MarginPerceptronCostVariable (Variable *output, Variable *target, real margin) | |
virtual string | classname () const |
virtual OptionList & | getOptionList () const |
virtual OptionMap & | getOptionMap () const |
virtual RemoteMethodMap & | getRemoteMethodMap () const |
virtual MarginPerceptronCostVariable * | 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 () |
Static Public Member Functions | |
static string | _classname_ () |
MarginPerceptronCostVariable. | |
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 void | declareOptions (OptionList &ol) |
Declare options (data fields) for the class. | |
Static Public Attributes | |
static StaticInitializer | _static_initializer_ |
Protected Member Functions | |
void | build_ () |
This does the actual building. | |
Protected Attributes | |
real | margin |
Private Types | |
typedef BinaryVariable | inherited |
Definition at line 56 of file MarginPerceptronCostVariable.h.
typedef BinaryVariable PLearn::MarginPerceptronCostVariable::inherited [private] |
Reimplemented from PLearn::BinaryVariable.
Definition at line 58 of file MarginPerceptronCostVariable.h.
PLearn::MarginPerceptronCostVariable::MarginPerceptronCostVariable | ( | ) |
Default constructor for persistence.
Definition at line 61 of file MarginPerceptronCostVariable.cc.
: margin(0.) { }
string PLearn::MarginPerceptronCostVariable::_classname_ | ( | ) | [static] |
Reimplemented from PLearn::BinaryVariable.
Definition at line 55 of file MarginPerceptronCostVariable.cc.
OptionList & PLearn::MarginPerceptronCostVariable::_getOptionList_ | ( | ) | [static] |
Reimplemented from PLearn::BinaryVariable.
Definition at line 55 of file MarginPerceptronCostVariable.cc.
RemoteMethodMap & PLearn::MarginPerceptronCostVariable::_getRemoteMethodMap_ | ( | ) | [static] |
Reimplemented from PLearn::BinaryVariable.
Definition at line 55 of file MarginPerceptronCostVariable.cc.
Reimplemented from PLearn::BinaryVariable.
Definition at line 55 of file MarginPerceptronCostVariable.cc.
Object * PLearn::MarginPerceptronCostVariable::_new_instance_for_typemap_ | ( | ) | [static] |
Reimplemented from PLearn::Object.
Definition at line 55 of file MarginPerceptronCostVariable.cc.
StaticInitializer MarginPerceptronCostVariable::_static_initializer_ & PLearn::MarginPerceptronCostVariable::_static_initialize_ | ( | ) | [static] |
Reimplemented from PLearn::BinaryVariable.
Definition at line 55 of file MarginPerceptronCostVariable.cc.
void PLearn::MarginPerceptronCostVariable::bprop | ( | ) | [virtual] |
Implements PLearn::Variable.
Definition at line 120 of file MarginPerceptronCostVariable.cc.
References PLearn::diff(), PLearn::Variable::gradientdata, i, PLearn::BinaryVariable::input1, PLearn::BinaryVariable::input2, and margin.
{ real gr = *gradientdata; int target = int(input2->valuedata[0]); for (int i=0; i<input1->size(); i++) { real output = input1->valuedata[i]; int signed_target = input1->size()==1?target*2-1:(target==i) - (target!=i); real diff = margin - signed_target * output; if (diff>0) input1->gradientdata[i] -= gr*signed_target; } }
void PLearn::MarginPerceptronCostVariable::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 72 of file MarginPerceptronCostVariable.cc.
References PLearn::BinaryVariable::build(), and build_().
{ inherited::build(); build_(); }
void PLearn::MarginPerceptronCostVariable::build_ | ( | ) | [protected] |
This does the actual building.
Reimplemented from PLearn::BinaryVariable.
Definition at line 79 of file MarginPerceptronCostVariable.cc.
References PLearn::BinaryVariable::input2, and PLERROR.
Referenced by build(), and MarginPerceptronCostVariable().
{ // input2 is target from constructor if (input2 && input2->size() != 1) PLERROR("In MarginPerceptronCostVariable: target represents a class (0...n_classes-1) and must be a single integer"); }
string PLearn::MarginPerceptronCostVariable::classname | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 55 of file MarginPerceptronCostVariable.cc.
void PLearn::MarginPerceptronCostVariable::declareOptions | ( | OptionList & | ol | ) | [static] |
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 87 of file MarginPerceptronCostVariable.cc.
References PLearn::OptionBase::buildoption, PLearn::declareOption(), PLearn::BinaryVariable::declareOptions(), and margin.
{ declareOption(ol, "margin", &MarginPerceptronCostVariable::margin, OptionBase::buildoption, ""); inherited::declareOptions(ol); }
static const PPath& PLearn::MarginPerceptronCostVariable::declaringFile | ( | ) | [inline, static] |
Reimplemented from PLearn::BinaryVariable.
Definition at line 68 of file MarginPerceptronCostVariable.h.
: void build_();
MarginPerceptronCostVariable * PLearn::MarginPerceptronCostVariable::deepCopy | ( | CopiesMap & | copies | ) | const [virtual] |
Reimplemented from PLearn::BinaryVariable.
Definition at line 55 of file MarginPerceptronCostVariable.cc.
void PLearn::MarginPerceptronCostVariable::fprop | ( | ) | [virtual] |
compute output given input
Implements PLearn::Variable.
Definition at line 102 of file MarginPerceptronCostVariable.cc.
References PLearn::diff(), i, PLearn::BinaryVariable::input1, PLearn::BinaryVariable::input2, margin, and PLearn::Variable::valuedata.
{ real cost = 0.0; int target = int(input2->valuedata[0]); for (int i=0; i<input1->size(); i++) { real output = input1->valuedata[i]; int signed_target = input1->size()==1?target*2-1:(target==i) - (target!=i); real diff = margin - signed_target * output; if (diff>0) cost += diff; } valuedata[0] = cost; }
OptionList & PLearn::MarginPerceptronCostVariable::getOptionList | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 55 of file MarginPerceptronCostVariable.cc.
OptionMap & PLearn::MarginPerceptronCostVariable::getOptionMap | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 55 of file MarginPerceptronCostVariable.cc.
RemoteMethodMap & PLearn::MarginPerceptronCostVariable::getRemoteMethodMap | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 55 of file MarginPerceptronCostVariable.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 96 of file MarginPerceptronCostVariable.cc.
{ l=1, w=1; }
Reimplemented from PLearn::BinaryVariable.
Definition at line 68 of file MarginPerceptronCostVariable.h.
real PLearn::MarginPerceptronCostVariable::margin [protected] |
Definition at line 61 of file MarginPerceptronCostVariable.h.
Referenced by bprop(), declareOptions(), and fprop().