PLearn 0.1
|
Variable that represents the element-wise IF-THEN-ELSE: More...
#include <IfThenElseVariable.h>
Public Member Functions | |
IfThenElseVariable () | |
Default constructor for persistence. | |
IfThenElseVariable (Var IfVar, Var ThenVar, Var ElseVar) | |
virtual string | classname () const |
virtual OptionList & | getOptionList () const |
virtual OptionMap & | getOptionMap () const |
virtual RemoteMethodMap & | getRemoteMethodMap () const |
virtual IfThenElseVariable * | 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 () |
Var & | If () |
Var & | Then () |
Var & | Else () |
Static Public Member Functions | |
static string | _classname_ () |
IfThenElseVariable. | |
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_ () |
Object-specific post-constructor. | |
Private Types | |
typedef NaryVariable | inherited |
Variable that represents the element-wise IF-THEN-ELSE:
Definition at line 58 of file IfThenElseVariable.h.
typedef NaryVariable PLearn::IfThenElseVariable::inherited [private] |
Reimplemented from PLearn::NaryVariable.
Definition at line 60 of file IfThenElseVariable.h.
PLearn::IfThenElseVariable::IfThenElseVariable | ( | ) | [inline] |
Definition at line 61 of file IfThenElseVariable.cc.
References build_().
string PLearn::IfThenElseVariable::_classname_ | ( | ) | [static] |
Reimplemented from PLearn::NaryVariable.
Definition at line 59 of file IfThenElseVariable.cc.
OptionList & PLearn::IfThenElseVariable::_getOptionList_ | ( | ) | [static] |
Reimplemented from PLearn::NaryVariable.
Definition at line 59 of file IfThenElseVariable.cc.
RemoteMethodMap & PLearn::IfThenElseVariable::_getRemoteMethodMap_ | ( | ) | [static] |
Reimplemented from PLearn::NaryVariable.
Definition at line 59 of file IfThenElseVariable.cc.
Reimplemented from PLearn::NaryVariable.
Definition at line 59 of file IfThenElseVariable.cc.
Object * PLearn::IfThenElseVariable::_new_instance_for_typemap_ | ( | ) | [static] |
Reimplemented from PLearn::Object.
Definition at line 59 of file IfThenElseVariable.cc.
StaticInitializer IfThenElseVariable::_static_initializer_ & PLearn::IfThenElseVariable::_static_initialize_ | ( | ) | [static] |
Reimplemented from PLearn::NaryVariable.
Definition at line 59 of file IfThenElseVariable.cc.
void PLearn::IfThenElseVariable::bprop | ( | ) | [virtual] |
Implements PLearn::Variable.
Definition at line 120 of file IfThenElseVariable.cc.
References Else(), PLearn::fast_exact_is_equal(), PLearn::Variable::gradient, PLearn::Variable::gradientdata, If(), PLearn::Variable::isScalar(), PLearn::Variable::nelems(), Then(), and PLearn::Variable::valuedata.
{ if(If()->isScalar()) { if (!fast_exact_is_equal(If()->valuedata[0], 0)) Then()->gradient += gradient; else Else()->gradient += gradient; } else { real* ifv = If()->valuedata; real* theng = Then()->gradientdata; real* elseg = Else()->gradientdata; for (int k=0;k<nelems();k++) { if ( !fast_exact_is_equal(ifv[k], 0) ) theng[k] += gradientdata[k]; else elseg[k] += gradientdata[k]; } } }
void PLearn::IfThenElseVariable::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::NaryVariable.
Definition at line 68 of file IfThenElseVariable.cc.
References PLearn::NaryVariable::build(), and build_().
{ inherited::build(); build_(); }
void PLearn::IfThenElseVariable::build_ | ( | ) | [protected] |
Object-specific post-constructor.
This method should be redefined in subclasses and do the actual building of the object according to previously set option fields. Constructors can just set option fields, and then call build_. This method is NOT virtual, and will typically be called only from three places: a constructor, the public virtual build()
method, and possibly the public virtual read method (which calls its parent's read). build_()
can assume that its parent's build_()
has already been called.
Reimplemented from PLearn::NaryVariable.
Definition at line 75 of file IfThenElseVariable.cc.
References PLearn::Variable::isScalar(), PLearn::TVec< T >::length(), PLearn::Variable::length(), PLERROR, PLearn::TVec< T >::size(), and PLearn::NaryVariable::varray.
Referenced by build(), and IfThenElseVariable().
{ if (varray.size()) { if (varray[1]->length() != varray[2]->length() || varray[1]->width() != varray[2]->width()) PLERROR("In IfThenElseVariable: ElseVar and ThenVar must have the same size"); if (!varray[0]->isScalar() && (varray[0]->length() != varray[1]->length() || varray[0]->width() != varray[1]->width())) PLERROR("In IfThenElseVariable: IfVar must either be a scalar or have the same size as ThenVar and ElseVar"); } }
string PLearn::IfThenElseVariable::classname | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 59 of file IfThenElseVariable.cc.
static const PPath& PLearn::IfThenElseVariable::declaringFile | ( | ) | [inline, static] |
Reimplemented from PLearn::NaryVariable.
Definition at line 67 of file IfThenElseVariable.h.
{ return varray[0]; }
IfThenElseVariable * PLearn::IfThenElseVariable::deepCopy | ( | CopiesMap & | copies | ) | const [virtual] |
Reimplemented from PLearn::NaryVariable.
Definition at line 59 of file IfThenElseVariable.cc.
Var& PLearn::IfThenElseVariable::Else | ( | ) | [inline] |
Definition at line 78 of file IfThenElseVariable.h.
Referenced by bprop(), fprop(), rfprop(), and symbolicBprop().
{ return varray[2]; }
void PLearn::IfThenElseVariable::fprop | ( | ) | [virtual] |
compute output given input
Implements PLearn::Variable.
Definition at line 94 of file IfThenElseVariable.cc.
References Else(), PLearn::fast_exact_is_equal(), If(), PLearn::Variable::isScalar(), PLearn::Variable::nelems(), PLearn::test(), Then(), PLearn::Variable::value, and PLearn::Variable::valuedata.
{ if(If()->isScalar()) { bool test = !fast_exact_is_equal(If()->valuedata[0], 0); if (test) value << Then()->value; else value << Else()->value; } else { real* ifv = If()->valuedata; real* thenv = Then()->valuedata; real* elsev = Else()->valuedata; for (int k=0;k<nelems();k++) { if ( !fast_exact_is_equal(ifv[k], 0) ) valuedata[k]=thenv[k]; else valuedata[k]=elsev[k]; } } }
OptionList & PLearn::IfThenElseVariable::getOptionList | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 59 of file IfThenElseVariable.cc.
OptionMap & PLearn::IfThenElseVariable::getOptionMap | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 59 of file IfThenElseVariable.cc.
RemoteMethodMap & PLearn::IfThenElseVariable::getRemoteMethodMap | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 59 of file IfThenElseVariable.cc.
Var& PLearn::IfThenElseVariable::If | ( | ) | [inline] |
Definition at line 76 of file IfThenElseVariable.h.
Referenced by bprop(), fprop(), rfprop(), and symbolicBprop().
{ return varray[0]; }
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 85 of file IfThenElseVariable.cc.
References PLearn::TVec< T >::length(), PLearn::TVec< T >::size(), and PLearn::NaryVariable::varray.
void PLearn::IfThenElseVariable::rfprop | ( | ) | [virtual] |
Reimplemented from PLearn::Variable.
Definition at line 153 of file IfThenElseVariable.cc.
References Else(), PLearn::fast_exact_is_equal(), If(), PLearn::TVec< T >::length(), PLearn::Variable::nelems(), PLearn::NaryVariable::resizeRValue(), PLearn::Variable::rValue, PLearn::Variable::rvaluedata, Then(), and PLearn::Variable::valuedata.
{ if (rValue.length()==0) resizeRValue(); if(If()->isScalar()) { if (!fast_exact_is_equal(If()->valuedata[0], 0)) rValue << Then()->rValue; else rValue << Else()->rValue; } else { real* ifv = If()->valuedata; real* rthenv = Then()->rvaluedata; real* relsev = Else()->rvaluedata; for (int k=0;k<nelems();k++) { if ( !fast_exact_is_equal(ifv[k], 0) ) rvaluedata[k]=rthenv[k]; else rvaluedata[k]=relsev[k]; } } }
void PLearn::IfThenElseVariable::symbolicBprop | ( | ) | [virtual] |
compute a piece of new Var graph that represents the symbolic derivative of this Var
Reimplemented from PLearn::Variable.
Definition at line 145 of file IfThenElseVariable.cc.
References Else(), PLearn::Variable::g, If(), PLearn::ifThenElse(), PLearn::Variable::length(), Then(), PLearn::Variable::width(), and zero.
{ Var zero(length(), width()); Then()->accg(ifThenElse(If(), g, zero)); Else()->accg(ifThenElse(If(), zero, g)); }
Var& PLearn::IfThenElseVariable::Then | ( | ) | [inline] |
Definition at line 77 of file IfThenElseVariable.h.
Referenced by bprop(), fprop(), rfprop(), and symbolicBprop().
{ return varray[1]; }
Reimplemented from PLearn::NaryVariable.
Definition at line 67 of file IfThenElseVariable.h.