PLearn 0.1
|
Variable which wraps an option of an object. More...
#include <ObjectOptionVariable.h>
Public Member Functions | |
ObjectOptionVariable () | |
Default constructor for persistence. | |
ObjectOptionVariable (PP< Object > root, const string &option_name, const string &initial_value="") | |
virtual string | classname () const |
virtual OptionList & | getOptionList () const |
virtual OptionMap & | getOptionMap () const |
virtual RemoteMethodMap & | getRemoteMethodMap () const |
virtual ObjectOptionVariable * | deepCopy (CopiesMap &copies) const |
virtual void | build () |
Post-constructor. | |
virtual void | makeDeepCopyFromShallowCopy (CopiesMap &copies) |
Transforms a shallow copy into a deep copy. | |
virtual void | setParents (const VarArray &parents) |
set this Variable's parents. To use with default constructor. | |
virtual bool | markPath () |
Sets the marked flag of all the sVariable that are to be in the fprop path. | |
virtual void | buildPath (VarArray &proppath) |
Finally buildPath is to be called from the output Variable of interest (this will build the proppath at the same time as erasing the marks) | |
virtual void | fprop () |
compute output given input | |
virtual void | bprop () |
virtual void | bbprop () |
compute an approximation to diag(d^2/dinput^2) given diag(d^2/doutput^2), with diag(d^2/dinput^2) ~=~ (doutput/dinput)' diag(d^2/doutput^2) (doutput/dinput) In particular: if 'C' depends on 'y' and 'y' depends on x ... | |
virtual void | symbolicBprop () |
compute a piece of new Var graph that represents the symbolic derivative of this Var | |
virtual void | rfprop () |
virtual VarArray | sources () |
if not marked, find all constant sources that influence this Variable. | |
virtual VarArray | random_sources () |
return ancestors which compute a non-deterministic function of their parents | |
virtual VarArray | ancestors () |
if not marked, find all Variables that influence this Variable. | |
virtual void | unmarkAncestors () |
undo any marking done by a call to sources() or ancestors() | |
virtual VarArray | parents () |
returns all the direct parents of this Var that are not marked (the call doesn't change any mark) | |
bool | isConstant () |
void | printInfo (bool print_gradient) |
virtual bool | update (real step_size, Vec direction_vec, real coeff=1.0, real b=0.0) |
virtual bool | update (Vec step_sizes, Vec direction_vec, real coeff=1.0, real b=0.0) |
virtual bool | update (real step_size, bool clear=false) |
set value = value + step_size * gradient | |
virtual bool | update (Vec new_value) |
virtual void | updateAndClear () |
Set value += gradient (respecting potential box constraints), and clear the gradient. | |
virtual void | updateWithWeightDecay (real step_size, real weight_decay, bool L1, bool clear=true) |
if (L1) value += learning_rate*gradient decrease |value| by learning_rate*weight_decay if it does not make value change sign else // L2 value += learning_rate*(gradient - weight_decay*value) if (clear) gradient=0 | |
Static Public Member Functions | |
static string | _classname_ () |
ObjectOptionVariable. | |
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 () |
Public Attributes | |
PP< Object > | m_root |
Pointer to the object we are wrapping an option of. | |
string | m_option_name |
Name of the option we are wrapping within the object. | |
string | m_initial_value |
Optional initial value that should be set into the option. | |
int * | m_option_int |
real * | m_option_real |
Vec * | m_option_vec |
Mat * | m_option_mat |
Static Public Attributes | |
static StaticInitializer | _static_initializer_ |
Static Protected Member Functions | |
static void | declareOptions (OptionList &ol) |
Declares the class options. | |
Private Types | |
enum | OptionType { OptionTypeUnknown, OptionTypeInt, OptionTypeReal, OptionTypeVec, OptionTypeMat } |
Encodes the type of the option in order to perform appropriate cast upon fpropping. More... | |
typedef Variable | inherited |
Private Member Functions | |
void | build_ () |
This does the actual building. | |
Private Attributes | |
Object * | m_final_object |
Pointer to the final enclosing object of the option. | |
enum PLearn::ObjectOptionVariable::OptionType | m_option_type |
union { | |
int * m_option_int | |
real * m_option_real | |
Vec * m_option_vec | |
Mat * m_option_mat | |
}; | |
int | m_index |
Index into a vector if the option contains []. |
Variable which wraps an option of an object.
This variable serves to mirror its contents onto an option of a PLearn Object. The option can refer to either an integer, a real value, a vector or a matrix within the object: the option will discover its size automatically. Furthermore, upon performing an fprop on the variable, this translates into a changeOption on the object.
Definition at line 58 of file ObjectOptionVariable.h.
typedef Variable PLearn::ObjectOptionVariable::inherited [private] |
Reimplemented from PLearn::Variable.
Definition at line 60 of file ObjectOptionVariable.h.
enum PLearn::ObjectOptionVariable::OptionType [private] |
Encodes the type of the option in order to perform appropriate cast upon fpropping.
Definition at line 150 of file ObjectOptionVariable.h.
PLearn::ObjectOptionVariable::ObjectOptionVariable | ( | ) |
Default constructor for persistence.
Definition at line 62 of file ObjectOptionVariable.cc.
: m_final_object(0), m_option_type(OptionTypeUnknown), m_option_int(0), m_index(-1) { }
PLearn::ObjectOptionVariable::ObjectOptionVariable | ( | PP< Object > | root, |
const string & | option_name, | ||
const string & | initial_value = "" |
||
) |
Definition at line 69 of file ObjectOptionVariable.cc.
References build(), and m_root.
: m_root(root), m_option_name(option_name), m_initial_value(initial_value), m_final_object(0), m_option_type(OptionTypeUnknown), m_option_int(0), m_index(-1) { if (m_root) build(); }
string PLearn::ObjectOptionVariable::_classname_ | ( | ) | [static] |
Reimplemented from PLearn::Variable.
Definition at line 60 of file ObjectOptionVariable.cc.
OptionList & PLearn::ObjectOptionVariable::_getOptionList_ | ( | ) | [static] |
Reimplemented from PLearn::Variable.
Definition at line 60 of file ObjectOptionVariable.cc.
RemoteMethodMap & PLearn::ObjectOptionVariable::_getRemoteMethodMap_ | ( | ) | [static] |
Reimplemented from PLearn::Variable.
Definition at line 60 of file ObjectOptionVariable.cc.
Reimplemented from PLearn::Variable.
Definition at line 60 of file ObjectOptionVariable.cc.
Object * PLearn::ObjectOptionVariable::_new_instance_for_typemap_ | ( | ) | [static] |
Reimplemented from PLearn::Object.
Definition at line 60 of file ObjectOptionVariable.cc.
StaticInitializer ObjectOptionVariable::_static_initializer_ & PLearn::ObjectOptionVariable::_static_initialize_ | ( | ) | [static] |
Reimplemented from PLearn::Variable.
Definition at line 60 of file ObjectOptionVariable.cc.
VarArray PLearn::ObjectOptionVariable::ancestors | ( | ) | [virtual] |
if not marked, find all Variables that influence this Variable.
Implements PLearn::Variable.
Definition at line 278 of file ObjectOptionVariable.cc.
References PLearn::Variable::marked, PLearn::Variable::setMark(), and PLearn::Variable::Var.
void PLearn::ObjectOptionVariable::bbprop | ( | ) | [virtual] |
compute an approximation to diag(d^2/dinput^2) given diag(d^2/doutput^2), with diag(d^2/dinput^2) ~=~ (doutput/dinput)' diag(d^2/doutput^2) (doutput/dinput) In particular: if 'C' depends on 'y' and 'y' depends on x ...
d^2C/dx^2 = d^2C/dy^2 * (dy/dx)^2 + dC/dy * d^2y/dx^2 (diaghessian) (gradient)
Reimplemented from PLearn::Variable.
Definition at line 257 of file ObjectOptionVariable.cc.
{} // No input: nothing to bbprop
void PLearn::ObjectOptionVariable::bprop | ( | ) | [virtual] |
Implements PLearn::Variable.
Definition at line 256 of file ObjectOptionVariable.cc.
{} // No input: nothing to bprop
void PLearn::ObjectOptionVariable::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::Variable.
Definition at line 188 of file ObjectOptionVariable.cc.
References PLearn::Variable::build(), and build_().
Referenced by makeDeepCopyFromShallowCopy(), and ObjectOptionVariable().
{ inherited::build(); build_(); }
void PLearn::ObjectOptionVariable::build_ | ( | ) | [private] |
This does the actual building.
Reimplemented from PLearn::Variable.
Definition at line 90 of file ObjectOptionVariable.cc.
References PLearn::TMat< T >::length(), PLearn::Variable::length(), m_final_object, m_index, m_initial_value, m_option_int, m_option_mat, m_option_name, m_option_real, m_option_type, m_option_vec, m_root, PLearn::Variable::matValue, OptionTypeInt, OptionTypeMat, OptionTypeReal, OptionTypeVec, PLASSERT, PLERROR, PLearn::Variable::resize(), PLearn::TVec< T >::size(), PLearn::Variable::size(), PLearn::Variable::value, and PLearn::TMat< T >::width().
Referenced by build().
{ PLASSERT( m_root ); PLASSERT( ! m_option_name.empty() ); if (! m_initial_value.empty()) m_root->changeOption(m_option_name, m_initial_value); // Infer the option type from the option, set related member variables, and // initialize the base-class variable size OptionList::iterator option_iter; string option_index; if (m_root->parseOptionName(m_option_name, m_final_object, option_iter, option_index)) { PLASSERT( m_final_object ); void* bound_option = (*option_iter)->getAsVoidPtr(m_final_object); string option_type = (*option_iter)->optiontype(); if (! option_index.empty()) m_index = lexical_cast<int>(option_index); else m_index = -1; // Set the appropriate pointer depending on the actual option type. If // an index is specified, don't immediately dereference it. Wait until // very last minute since the actual memory location the vector points // to may change with vector resizes/reallocations. if (option_type == TypeTraits<int>::name()) { m_option_type = OptionTypeInt; m_option_int = static_cast<int*>(bound_option); } else if (option_type == TypeTraits<real>::name()) { m_option_type = OptionTypeReal; m_option_real = static_cast<real*>(bound_option); } else if (option_type == TypeTraits<Vec>::name()) { m_option_type = OptionTypeVec; m_option_vec = static_cast<Vec*>(bound_option); } else if (option_type == TypeTraits<Mat>::name()) { m_option_type = OptionTypeMat; m_option_mat = static_cast<Mat*>(bound_option); } else PLERROR("ObjectOptionVariable::build: unhandled type \"%s\" the option " "\"%s\"; supported types are: { %s, %s, %s, %s }", option_type.c_str(), m_option_name.c_str(), TypeTraits<int>::name() .c_str(), TypeTraits<real>::name().c_str(), TypeTraits<Vec>::name() .c_str(), TypeTraits<Mat>::name() .c_str()); } // Now properly set the size and initial contents of the variable switch (m_option_type) { case OptionTypeInt: PLASSERT( m_option_int ); inherited::resize(1,1); value[0] = *m_option_int; break; case OptionTypeReal: PLASSERT( m_option_real ); inherited::resize(1,1); value[0] = *m_option_real; break; case OptionTypeVec: // Assume row vector PLASSERT( m_option_vec ); if (m_index < 0 || m_option_vec->size() == 0) { inherited::resize(1, m_option_vec->size()); value << *m_option_vec; } else { inherited::resize(1,1); value[0] = (*m_option_vec)[m_index]; } break; case OptionTypeMat: // Indexing works for ROWS in the case of matrices PLASSERT( m_option_mat ); if (m_index < 0 || m_option_mat->length() == 0) { inherited::resize(m_option_mat->length(), m_option_mat->width()); matValue << *m_option_mat; } else { inherited::resize(1, m_option_mat->width()); value << (*m_option_mat)(m_index); } break; default: PLASSERT( false ); } }
void PLearn::ObjectOptionVariable::buildPath | ( | VarArray & | proppath | ) | [virtual] |
Finally buildPath is to be called from the output Variable of interest (this will build the proppath at the same time as erasing the marks)
Implements PLearn::Variable.
Definition at line 302 of file ObjectOptionVariable.cc.
References PLearn::TVec< T >::append(), PLearn::Variable::clearMark(), PLearn::Variable::marked, and PLearn::Variable::Var.
string PLearn::ObjectOptionVariable::classname | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 60 of file ObjectOptionVariable.cc.
void PLearn::ObjectOptionVariable::declareOptions | ( | OptionList & | ol | ) | [static, protected] |
Declares the class options.
Reimplemented from PLearn::Variable.
Definition at line 84 of file ObjectOptionVariable.cc.
References PLearn::Variable::declareOptions().
{ inherited::declareOptions(ol); }
static const PPath& PLearn::ObjectOptionVariable::declaringFile | ( | ) | [inline, static] |
Reimplemented from PLearn::Variable.
Definition at line 90 of file ObjectOptionVariable.h.
{ return true; }
ObjectOptionVariable * PLearn::ObjectOptionVariable::deepCopy | ( | CopiesMap & | copies | ) | const [virtual] |
Reimplemented from PLearn::Variable.
Definition at line 60 of file ObjectOptionVariable.cc.
void PLearn::ObjectOptionVariable::fprop | ( | ) | [virtual] |
compute output given input
Implements PLearn::Variable.
Definition at line 203 of file ObjectOptionVariable.cc.
References PLearn::Object::build(), m_final_object, m_index, m_option_int, m_option_mat, m_option_real, m_option_type, m_option_vec, PLearn::Variable::matValue, OptionTypeInt, OptionTypeMat, OptionTypeReal, OptionTypeVec, PLASSERT, and PLearn::Variable::value.
Referenced by update(), updateAndClear(), and updateWithWeightDecay().
{ // Depending on the type of the option, set the proper contents of the option. switch (m_option_type) { case OptionTypeInt: { PLASSERT( m_option_int ); *m_option_int = int(value[0]); break; } case OptionTypeReal: { PLASSERT( m_option_real ); *m_option_real = value[0]; break; } case OptionTypeVec: { PLASSERT( m_option_vec ); if (m_index < 0) *m_option_vec << value; else (*m_option_vec)[m_index] = value[0]; break; } case OptionTypeMat: { PLASSERT( m_option_mat ); if (m_index < 0) *m_option_mat << matValue; else (*m_option_mat)(m_index)<< value; break; } default: PLASSERT( false ); } // Rebuild the object to reflect the change; we should find a smart way to // optimize this away in the future PLASSERT( m_final_object ); m_final_object->build(); }
OptionList & PLearn::ObjectOptionVariable::getOptionList | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 60 of file ObjectOptionVariable.cc.
OptionMap & PLearn::ObjectOptionVariable::getOptionMap | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 60 of file ObjectOptionVariable.cc.
RemoteMethodMap & PLearn::ObjectOptionVariable::getRemoteMethodMap | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 60 of file ObjectOptionVariable.cc.
bool PLearn::ObjectOptionVariable::isConstant | ( | ) | [inline, virtual] |
Reimplemented from PLearn::Variable.
Definition at line 117 of file ObjectOptionVariable.h.
{ return true; }
void PLearn::ObjectOptionVariable::makeDeepCopyFromShallowCopy | ( | CopiesMap & | copies | ) | [virtual] |
Transforms a shallow copy into a deep copy.
Reimplemented from PLearn::Variable.
Definition at line 196 of file ObjectOptionVariable.cc.
References build(), PLearn::deepCopyField(), m_root, and PLearn::Variable::makeDeepCopyFromShallowCopy().
{ inherited::makeDeepCopyFromShallowCopy(copies); deepCopyField(m_root, copies); build(); }
bool PLearn::ObjectOptionVariable::markPath | ( | ) | [virtual] |
Sets the marked flag of all the sVariable that are to be in the fprop path.
The input sVariable that are of interest are to be marked first. Then markPath is to be called from the output Variable of interest
Implements PLearn::Variable.
Definition at line 297 of file ObjectOptionVariable.cc.
References PLearn::Variable::marked.
{ return marked; }
VarArray PLearn::ObjectOptionVariable::parents | ( | ) | [virtual] |
returns all the direct parents of this Var that are not marked (the call doesn't change any mark)
Implements PLearn::Variable.
Definition at line 292 of file ObjectOptionVariable.cc.
{
return VarArray(0,0);
}
void PLearn::ObjectOptionVariable::printInfo | ( | bool | print_gradient | ) | [inline, virtual] |
Implements PLearn::Variable.
Definition at line 119 of file ObjectOptionVariable.h.
References PLearn::endl(), and PLearn::pout.
{ pout << getName() << "[" << (void*)this << "] " << *this << " = " << value; if (print_gradient) pout << " gradient=" << gradient; pout << endl; }
VarArray PLearn::ObjectOptionVariable::random_sources | ( | ) | [virtual] |
return ancestors which compute a non-deterministic function of their parents
Implements PLearn::Variable.
Definition at line 271 of file ObjectOptionVariable.cc.
References PLearn::Variable::marked, and PLearn::Variable::setMark().
void PLearn::ObjectOptionVariable::rfprop | ( | ) | [virtual] |
Reimplemented from PLearn::Variable.
Definition at line 258 of file ObjectOptionVariable.cc.
{} // No input: nothing to rfprop
void PLearn::ObjectOptionVariable::setParents | ( | const VarArray & | parents | ) | [virtual] |
set this Variable's parents. To use with default constructor.
Reimplemented from PLearn::Variable.
Definition at line 251 of file ObjectOptionVariable.cc.
References PLERROR.
{ PLERROR("ObjectOptionVariable::setParents: this variable has no parents."); }
VarArray PLearn::ObjectOptionVariable::sources | ( | ) | [virtual] |
if not marked, find all constant sources that influence this Variable.
A constant source is normally a SourceVariable.
Implements PLearn::Variable.
Definition at line 261 of file ObjectOptionVariable.cc.
References PLearn::Variable::marked, PLearn::Variable::setMark(), and PLearn::Variable::Var.
void PLearn::ObjectOptionVariable::symbolicBprop | ( | ) | [virtual] |
compute a piece of new Var graph that represents the symbolic derivative of this Var
Reimplemented from PLearn::Variable.
Definition at line 259 of file ObjectOptionVariable.cc.
{} // No input: nothing to bprop
void PLearn::ObjectOptionVariable::unmarkAncestors | ( | ) | [virtual] |
undo any marking done by a call to sources() or ancestors()
Implements PLearn::Variable.
Definition at line 286 of file ObjectOptionVariable.cc.
References PLearn::Variable::clearMark(), and PLearn::Variable::marked.
set value = value + step_size * gradient
with step_size possibly scaled down s.t. box constraints are satisfied return true if box constraints have been hit with the update
Reimplemented from PLearn::Variable.
Definition at line 328 of file ObjectOptionVariable.cc.
References fprop(), and PLearn::Variable::update().
{ bool ret = inherited::update(step_size, clear); fprop(); return ret; }
virtual bool PLearn::ObjectOptionVariable::update | ( | real | step_size, |
Vec | direction_vec, | ||
real | coeff = 1.0 , |
||
real | b = 0.0 |
||
) | [virtual] |
virtual bool PLearn::ObjectOptionVariable::update | ( | Vec | step_sizes, |
Vec | direction_vec, | ||
real | coeff = 1.0 , |
||
real | b = 0.0 |
||
) | [virtual] |
void PLearn::ObjectOptionVariable::updateAndClear | ( | ) | [virtual] |
Set value += gradient (respecting potential box constraints), and clear the gradient.
Reimplemented from PLearn::Variable.
Definition at line 342 of file ObjectOptionVariable.cc.
References fprop(), and PLearn::Variable::updateAndClear().
{ inherited::updateAndClear(); fprop(); }
void PLearn::ObjectOptionVariable::updateWithWeightDecay | ( | real | step_size, |
real | weight_decay, | ||
bool | L1, | ||
bool | clear = true |
||
) | [virtual] |
if (L1) value += learning_rate*gradient decrease |value| by learning_rate*weight_decay if it does not make value change sign else // L2 value += learning_rate*(gradient - weight_decay*value) if (clear) gradient=0
Reimplemented from PLearn::Variable.
Definition at line 348 of file ObjectOptionVariable.cc.
References fprop(), and PLearn::Variable::updateWithWeightDecay().
{ inherited::updateWithWeightDecay(step_size, weight_decay, L1, clear); fprop(); }
union { ... } [private] |
Reimplemented from PLearn::Variable.
Definition at line 90 of file ObjectOptionVariable.h.
Pointer to the final enclosing object of the option.
Definition at line 146 of file ObjectOptionVariable.h.
int PLearn::ObjectOptionVariable::m_index [private] |
Index into a vector if the option contains [].
Definition at line 167 of file ObjectOptionVariable.h.
Optional initial value that should be set into the option.
This is a string that is set with 'changeOption'. Note that this exact initial value is initially set into the option, regardless of the setting for 'log_variable'.
Definition at line 78 of file ObjectOptionVariable.h.
Referenced by build_().
Definition at line 160 of file ObjectOptionVariable.h.
Definition at line 163 of file ObjectOptionVariable.h.
Name of the option we are wrapping within the object.
Definition at line 70 of file ObjectOptionVariable.h.
Referenced by build_().
Definition at line 161 of file ObjectOptionVariable.h.
Definition at line 162 of file ObjectOptionVariable.h.
Pointer to the object we are wrapping an option of.
This can also be a 'root' object in the case of a complex option such as 'A.B.C'.
Definition at line 67 of file ObjectOptionVariable.h.
Referenced by build_(), makeDeepCopyFromShallowCopy(), and ObjectOptionVariable().