PLearn 0.1
|
Computes sum(square_i(netout[i]-(i==classnum ?hotval :coldval)) This is used typically in a classification setting where netout is a Var of network outputs, and classnum is the target class number. More...
#include <OneHotSquaredLoss.h>
Public Member Functions | |
OneHotSquaredLoss () | |
Default constructor for persistence. | |
OneHotSquaredLoss (Variable *netout, Variable *classnum, real coldval=0., real hotval=1.) | |
virtual string | classname () const |
virtual OptionList & | getOptionList () const |
virtual OptionMap & | getOptionMap () const |
virtual RemoteMethodMap & | getRemoteMethodMap () const |
virtual OneHotSquaredLoss * | 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 () |
virtual void | symbolicBprop () |
compute a piece of new Var graph that represents the symbolic derivative of this Var | |
virtual void | rfprop () |
Static Public Member Functions | |
static string | _classname_ () |
OneHotSquaredLoss. | |
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 | coldval_ |
real | hotval_ |
Private Types | |
typedef BinaryVariable | inherited |
Computes sum(square_i(netout[i]-(i==classnum ?hotval :coldval)) This is used typically in a classification setting where netout is a Var of network outputs, and classnum is the target class number.
* Efficient and numerically stable loss functions *
Definition at line 59 of file OneHotSquaredLoss.h.
typedef BinaryVariable PLearn::OneHotSquaredLoss::inherited [private] |
Reimplemented from PLearn::BinaryVariable.
Definition at line 61 of file OneHotSquaredLoss.h.
PLearn::OneHotSquaredLoss::OneHotSquaredLoss | ( | ) |
Default constructor for persistence.
Definition at line 58 of file OneHotSquaredLoss.cc.
string PLearn::OneHotSquaredLoss::_classname_ | ( | ) | [static] |
Reimplemented from PLearn::BinaryVariable.
Definition at line 56 of file OneHotSquaredLoss.cc.
OptionList & PLearn::OneHotSquaredLoss::_getOptionList_ | ( | ) | [static] |
Reimplemented from PLearn::BinaryVariable.
Definition at line 56 of file OneHotSquaredLoss.cc.
RemoteMethodMap & PLearn::OneHotSquaredLoss::_getRemoteMethodMap_ | ( | ) | [static] |
Reimplemented from PLearn::BinaryVariable.
Definition at line 56 of file OneHotSquaredLoss.cc.
Reimplemented from PLearn::BinaryVariable.
Definition at line 56 of file OneHotSquaredLoss.cc.
Object * PLearn::OneHotSquaredLoss::_new_instance_for_typemap_ | ( | ) | [static] |
Reimplemented from PLearn::Object.
Definition at line 56 of file OneHotSquaredLoss.cc.
StaticInitializer OneHotSquaredLoss::_static_initializer_ & PLearn::OneHotSquaredLoss::_static_initialize_ | ( | ) | [static] |
Reimplemented from PLearn::BinaryVariable.
Definition at line 56 of file OneHotSquaredLoss.cc.
void PLearn::OneHotSquaredLoss::bprop | ( | ) | [virtual] |
Implements PLearn::Variable.
Definition at line 106 of file OneHotSquaredLoss.cc.
References coldval_, PLearn::fast_exact_is_equal(), PLearn::Variable::gradientdata, hotval_, i, PLearn::BinaryVariable::input1, PLearn::BinaryVariable::input2, n, and PLearn::two().
{ real gr = *gradientdata; real* netout = input1->valuedata; int n = input1->value.size(); int classnum = (int) input2->valuedata[0]; real* input1grptr = input1->gradientdata; if(!fast_exact_is_equal(gr, 1.)) { gr = gr+gr; for(int i=0; i<n; i++) *input1grptr++ += gr*(*netout++ - (i==classnum ? hotval_ : coldval_)); } else // specialised version for gr==1 { for(int i=0; i<n; i++) input1->gradientdata[i] += two(netout[i] - (i==classnum ? hotval_ : coldval_)); } }
void PLearn::OneHotSquaredLoss::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 69 of file OneHotSquaredLoss.cc.
References PLearn::BinaryVariable::build(), and build_().
{ inherited::build(); build_(); }
void PLearn::OneHotSquaredLoss::build_ | ( | ) | [protected] |
This does the actual building.
Reimplemented from PLearn::BinaryVariable.
Definition at line 76 of file OneHotSquaredLoss.cc.
References PLearn::BinaryVariable::input2, and PLERROR.
Referenced by build(), and OneHotSquaredLoss().
{ // input2 is classnum from constructor if(input2 && !input2->isScalar()) PLERROR("In OneHotSquaredLoss: classnum must be a scalar variable representing an index of netout (typically a classnum)"); }
string PLearn::OneHotSquaredLoss::classname | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 56 of file OneHotSquaredLoss.cc.
void PLearn::OneHotSquaredLoss::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 84 of file OneHotSquaredLoss.cc.
References PLearn::OptionBase::buildoption, coldval_, PLearn::declareOption(), PLearn::BinaryVariable::declareOptions(), and hotval_.
{ declareOption(ol, "coldval_", &OneHotSquaredLoss::coldval_, OptionBase::buildoption, ""); declareOption(ol, "hotval_", &OneHotSquaredLoss::hotval_, OptionBase::buildoption, ""); inherited::declareOptions(ol); }
static const PPath& PLearn::OneHotSquaredLoss::declaringFile | ( | ) | [inline, static] |
Reimplemented from PLearn::BinaryVariable.
Definition at line 71 of file OneHotSquaredLoss.h.
: void build_();
OneHotSquaredLoss * PLearn::OneHotSquaredLoss::deepCopy | ( | CopiesMap & | copies | ) | const [virtual] |
Reimplemented from PLearn::BinaryVariable.
Definition at line 56 of file OneHotSquaredLoss.cc.
void PLearn::OneHotSquaredLoss::fprop | ( | ) | [virtual] |
compute output given input
Implements PLearn::Variable.
Definition at line 94 of file OneHotSquaredLoss.cc.
References coldval_, hotval_, i, PLearn::BinaryVariable::input1, PLearn::BinaryVariable::input2, n, PLearn::square(), and PLearn::Variable::valuedata.
{ real* netout = input1->valuedata; int n = input1->value.size(); int classnum = (int) input2->valuedata[0]; real res = 0.; for(int i=0; i<n; i++) res += square(*netout++ - (i==classnum ? hotval_ : coldval_)); *valuedata = res; }
OptionList & PLearn::OneHotSquaredLoss::getOptionList | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 56 of file OneHotSquaredLoss.cc.
OptionMap & PLearn::OneHotSquaredLoss::getOptionMap | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 56 of file OneHotSquaredLoss.cc.
RemoteMethodMap & PLearn::OneHotSquaredLoss::getRemoteMethodMap | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 56 of file OneHotSquaredLoss.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 91 of file OneHotSquaredLoss.cc.
{ l=1, w=1; }
void PLearn::OneHotSquaredLoss::rfprop | ( | ) | [virtual] |
Reimplemented from PLearn::Variable.
Definition at line 136 of file OneHotSquaredLoss.cc.
References coldval_, for(), hotval_, i, PLearn::BinaryVariable::input1, PLearn::BinaryVariable::input2, n, PLearn::Variable::rvaluedata, and PLearn::sum().
{ int n = input1->value.size(); int classnum = (int) input2->valuedata[0]; real sum = 0; for (int i=0; i<n; i++) sum += 2 * input1->rvaluedata[i] * (input1->valuedata[i] - (i==classnum ? hotval_ : coldval_)); rvaluedata[0] = sum; }
void PLearn::OneHotSquaredLoss::symbolicBprop | ( | ) | [virtual] |
compute a piece of new Var graph that represents the symbolic derivative of this Var
Reimplemented from PLearn::Variable.
Definition at line 127 of file OneHotSquaredLoss.cc.
References coldval_, PLearn::Variable::g, hotval_, PLearn::BinaryVariable::input1, PLearn::BinaryVariable::input2, and PLearn::Var::length().
{ Var gi = g*(input1 - coldval_); Var gindex = new RowAtPositionVariable(g*(coldval_-hotval_), input2, input1->length()); Var ginput = gi + gindex; input1->accg(ginput+ginput); //2*gi }
Reimplemented from PLearn::BinaryVariable.
Definition at line 71 of file OneHotSquaredLoss.h.
real PLearn::OneHotSquaredLoss::coldval_ [protected] |
Definition at line 64 of file OneHotSquaredLoss.h.
Referenced by bprop(), declareOptions(), fprop(), rfprop(), and symbolicBprop().
real PLearn::OneHotSquaredLoss::hotval_ [protected] |
Definition at line 64 of file OneHotSquaredLoss.h.
Referenced by bprop(), declareOptions(), fprop(), rfprop(), and symbolicBprop().