PLearn 0.1
Public Member Functions | Static Public Member Functions | Static Public Attributes | Protected Member Functions | Protected Attributes | Private Types
PLearn::OneHotSquaredLoss Class Reference

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>

Inheritance diagram for PLearn::OneHotSquaredLoss:
Inheritance graph
[legend]
Collaboration diagram for PLearn::OneHotSquaredLoss:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 OneHotSquaredLoss ()
 Default constructor for persistence.
 OneHotSquaredLoss (Variable *netout, Variable *classnum, real coldval=0., real hotval=1.)
virtual string classname () const
virtual OptionListgetOptionList () const
virtual OptionMapgetOptionMap () const
virtual RemoteMethodMapgetRemoteMethodMap () const
virtual OneHotSquaredLossdeepCopy (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 PPathdeclaringFile ()
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

Detailed Description

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.


Member Typedef Documentation

Reimplemented from PLearn::BinaryVariable.

Definition at line 61 of file OneHotSquaredLoss.h.


Constructor & Destructor Documentation

PLearn::OneHotSquaredLoss::OneHotSquaredLoss ( )

Default constructor for persistence.

Definition at line 58 of file OneHotSquaredLoss.cc.

    : coldval_(0.), hotval_(0.)
{ }
PLearn::OneHotSquaredLoss::OneHotSquaredLoss ( Variable netout,
Variable classnum,
real  coldval = 0.,
real  hotval = 1. 
)

Definition at line 62 of file OneHotSquaredLoss.cc.

References build_().

    : inherited(netout,classnum,1,1), coldval_(coldval), hotval_(hotval)
{
    build_();
}

Here is the call graph for this function:


Member Function Documentation

string PLearn::OneHotSquaredLoss::_classname_ ( ) [static]

OneHotSquaredLoss.

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.

bool PLearn::OneHotSquaredLoss::_isa_ ( const Object o) [static]

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_));        
    }
}

Here is the call graph for this function:

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_().

Here is the call graph for this function:

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)");
}

Here is the caller graph for this function:

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);
  }
Parameters:
olList 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_.

Here is the call graph for this function:

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;
}

Here is the call graph for this function:

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.

void PLearn::OneHotSquaredLoss::recomputeSize ( int l,
int w 
) const [virtual]

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;
}

Here is the call graph for this function:

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
}

Here is the call graph for this function:


Member Data Documentation

Reimplemented from PLearn::BinaryVariable.

Definition at line 71 of file OneHotSquaredLoss.h.

Definition at line 64 of file OneHotSquaredLoss.h.

Referenced by bprop(), declareOptions(), fprop(), rfprop(), and symbolicBprop().

Definition at line 64 of file OneHotSquaredLoss.h.

Referenced by bprop(), declareOptions(), fprop(), rfprop(), and symbolicBprop().


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines