PLearn 0.1
|
Map probabilities in (0,1) to a bit in {0,1}, either according to a hard threshold (> 0.5), or by sampling, and ALLOW GRADIENTS TO PROPAGATE BACKWARDS. More...
#include <BinarizeModule.h>
Public Member Functions | |
BinarizeModule () | |
Default constructor. | |
void | fprop (const TVec< Mat * > &ports_value) |
Perform a fprop step. | |
virtual void | bpropAccUpdate (const TVec< Mat * > &ports_value, const TVec< Mat * > &ports_gradient) |
Perform a back propagation step (also updating parameters according to the provided gradient). | |
virtual void | forget () |
Reset the parameters to the state they would be BEFORE starting training. | |
virtual const TVec< string > & | getPorts () |
Return the list of ports in the module. | |
virtual const TMat< int > & | getPortSizes () |
Return the size of all ports, in the form of a two-column matrix, where each row represents a port, and the two numbers on a row are respectively its length and its width (with -1 representing an undefined or variable value). | |
virtual string | classname () const |
virtual OptionList & | getOptionList () const |
virtual OptionMap & | getOptionMap () const |
virtual RemoteMethodMap & | getRemoteMethodMap () const |
virtual BinarizeModule * | deepCopy (CopiesMap &copies) const |
virtual void | build () |
Post-constructor. | |
virtual void | makeDeepCopyFromShallowCopy (CopiesMap &copies) |
Transforms a shallow copy into a deep copy. | |
Static Public Member Functions | |
static string | _classname_ () |
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 | |
bool | stochastic |
### declare public option fields (such as build options) here Start your comments with Doxygen-compatible comments such as //! | |
bool | copy_gradients |
If true just pass the gradient straight through with no alteration. | |
bool | saturate_gradients |
If true then multiply output gradients by p(1-p) for input probability p. | |
Static Public Attributes | |
static StaticInitializer | _static_initializer_ |
Static Protected Member Functions | |
static void | declareOptions (OptionList &ol) |
Declares the class options. | |
Private Types | |
typedef OnlineLearningModule | inherited |
Private Member Functions | |
void | build_ () |
This does the actual building. |
Map probabilities in (0,1) to a bit in {0,1}, either according to a hard threshold (> 0.5), or by sampling, and ALLOW GRADIENTS TO PROPAGATE BACKWARDS.
Definition at line 53 of file BinarizeModule.h.
typedef OnlineLearningModule PLearn::BinarizeModule::inherited [private] |
Reimplemented from PLearn::OnlineLearningModule.
Definition at line 55 of file BinarizeModule.h.
PLearn::BinarizeModule::BinarizeModule | ( | ) |
Default constructor.
Definition at line 60 of file BinarizeModule.cc.
: stochastic(true),copy_gradients(false),saturate_gradients(false) { }
string PLearn::BinarizeModule::_classname_ | ( | ) | [static] |
Reimplemented from PLearn::OnlineLearningModule.
Definition at line 55 of file BinarizeModule.cc.
OptionList & PLearn::BinarizeModule::_getOptionList_ | ( | ) | [static] |
Reimplemented from PLearn::OnlineLearningModule.
Definition at line 55 of file BinarizeModule.cc.
RemoteMethodMap & PLearn::BinarizeModule::_getRemoteMethodMap_ | ( | ) | [static] |
Reimplemented from PLearn::OnlineLearningModule.
Definition at line 55 of file BinarizeModule.cc.
Reimplemented from PLearn::OnlineLearningModule.
Definition at line 55 of file BinarizeModule.cc.
Object * PLearn::BinarizeModule::_new_instance_for_typemap_ | ( | ) | [static] |
Reimplemented from PLearn::Object.
Definition at line 55 of file BinarizeModule.cc.
StaticInitializer BinarizeModule::_static_initializer_ & PLearn::BinarizeModule::_static_initialize_ | ( | ) | [static] |
Reimplemented from PLearn::OnlineLearningModule.
Definition at line 55 of file BinarizeModule.cc.
void PLearn::BinarizeModule::bpropAccUpdate | ( | const TVec< Mat * > & | ports_value, |
const TVec< Mat * > & | ports_gradient | ||
) | [virtual] |
Perform a back propagation step (also updating parameters according to the provided gradient).
The matrices in 'ports_value' must be the same as the ones given in a previous call to 'fprop' (and thus they should in particular contain the result of the fprop computation). However, they are not necessarily the same as the ones given in the LAST call to 'fprop': if there is a need to store an internal module state, this should be done using a specific port to store this state. Each Mat* pointer in the 'ports_gradient' vector can be one of:
Reimplemented from PLearn::OnlineLearningModule.
Definition at line 104 of file BinarizeModule.cc.
References copy_gradients, i, PLearn::TMat< T >::length(), PLearn::TVec< T >::length(), PLearn::OnlineLearningModule::nPorts(), PLASSERT, PLearn::TMat< T >::resize(), saturate_gradients, and PLearn::TMat< T >::width().
{ PLASSERT( ports_value.length() == nPorts() && ports_gradient.length() == nPorts()); Mat* input = ports_value[0]; Mat* output = ports_value[1]; Mat* input_gradient = ports_gradient[0]; Mat* output_gradient = ports_gradient[1]; int mbs=output->length(); if (input_gradient) { input_gradient->resize(mbs,output->width()); for (int t=0;t<mbs;t++) { real* yt = (*output)[t]; real* dyt = (*output_gradient)[t]; real* dxt = (*input_gradient)[t]; real* xt = (*input)[t]; if (copy_gradients) for (int i=0;i<output->width();i++) dxt[i] += dyt[i]; else if (saturate_gradients) for (int i=0;i<output->width();i++) dxt[i] += dyt[i]*xt[i]*(1-xt[i]); else for (int i=0;i<output->width();i++) if ((yt[i]-0.5)*dyt[i] > 0) dxt[i] += dyt[i]; } } }
void PLearn::BinarizeModule::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::OnlineLearningModule.
Definition at line 95 of file BinarizeModule.cc.
References PLearn::OnlineLearningModule::build(), and build_().
{ inherited::build(); build_(); }
void PLearn::BinarizeModule::build_ | ( | ) | [private] |
This does the actual building.
Reimplemented from PLearn::OnlineLearningModule.
Definition at line 88 of file BinarizeModule.cc.
Referenced by build().
{ }
string PLearn::BinarizeModule::classname | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 55 of file BinarizeModule.cc.
void PLearn::BinarizeModule::declareOptions | ( | OptionList & | ol | ) | [static, protected] |
Declares the class options.
Reimplemented from PLearn::OnlineLearningModule.
Definition at line 68 of file BinarizeModule.cc.
References PLearn::OptionBase::buildoption, copy_gradients, PLearn::declareOption(), PLearn::OnlineLearningModule::declareOptions(), saturate_gradients, and stochastic.
{ declareOption(ol, "stochastic", &BinarizeModule::stochastic, OptionBase::buildoption, "If true then sample the output bits stochastically, else use a hard threshold.\n"); declareOption(ol, "copy_gradients", &BinarizeModule::copy_gradients, OptionBase::buildoption, "If true then simply copy the gradients through with no alteration.\n"); declareOption(ol, "saturate_gradients", &BinarizeModule::saturate_gradients, OptionBase::buildoption, "If true then multiply output gradients by p(1-p) for input probability p.\n"); inherited::declareOptions(ol); }
static const PPath& PLearn::BinarizeModule::declaringFile | ( | ) | [inline, static] |
Reimplemented from PLearn::OnlineLearningModule.
Definition at line 261 of file BinarizeModule.h.
:
//##### Protected Member Functions ######################################
BinarizeModule * PLearn::BinarizeModule::deepCopy | ( | CopiesMap & | copies | ) | const [virtual] |
Reimplemented from PLearn::OnlineLearningModule.
Definition at line 55 of file BinarizeModule.cc.
void PLearn::BinarizeModule::forget | ( | ) | [virtual] |
Reset the parameters to the state they would be BEFORE starting training.
Note that this method is necessarily called from build().
Implements PLearn::OnlineLearningModule.
Definition at line 159 of file BinarizeModule.cc.
{ }
Perform a fprop step.
Each Mat* pointer in the 'ports_value' vector can be one of:
Reimplemented from PLearn::OnlineLearningModule.
Definition at line 166 of file BinarizeModule.cc.
References i, PLearn::TMat< T >::length(), PLearn::TVec< T >::length(), PLearn::OnlineLearningModule::nPorts(), PLASSERT, PLASSERT_MSG, PLearn::OnlineLearningModule::random_gen, PLearn::TMat< T >::resize(), stochastic, w, and PLearn::TMat< T >::width().
{ PLASSERT( ports_value.length() == nPorts() ); // check which ports are input (ports_value[i] && !ports_value[i]->isEmpty()) // which ports are output (ports_value[i] && ports_value[i]->isEmpty()) // and which ports are ignored (!ports_value[i]). // If that combination of (input,output,ignored) is feasible by this class // then perform the corresponding computation. Otherwise launch the error below. // See the comment in the header file for more information. PLASSERT_MSG(random_gen, "random_gen should be initialized before generating samples"); Mat* input = ports_value[0]; Mat* output = ports_value[1]; int mbs=input->length(); output->resize(mbs,input->width()); for (int t=0;t<mbs;t++) { real* xt = (*input)[t]; real* yt = (*output)[t]; int w=input->width(); if (stochastic) for (int i=0;i<w;i++) yt[i]=random_gen->binomial_sample(xt[i]); else for (int i=0;i<w;i++) yt[i]=xt[i]>=0.5?1:0; } }
OptionList & PLearn::BinarizeModule::getOptionList | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 55 of file BinarizeModule.cc.
OptionMap & PLearn::BinarizeModule::getOptionMap | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 55 of file BinarizeModule.cc.
const TVec< string > & PLearn::BinarizeModule::getPorts | ( | ) | [virtual] |
Return the list of ports in the module.
The default implementation returns a pair ("input", "output") to handle the most common case.
Reimplemented from PLearn::OnlineLearningModule.
Definition at line 208 of file BinarizeModule.cc.
References PLearn::OnlineLearningModule::getPorts().
{ return inherited::getPorts(); }
Return the size of all ports, in the form of a two-column matrix, where each row represents a port, and the two numbers on a row are respectively its length and its width (with -1 representing an undefined or variable value).
The default value fills this matrix with:
Reimplemented from PLearn::OnlineLearningModule.
Definition at line 215 of file BinarizeModule.cc.
References PLearn::TMat< T >::fill(), PLearn::OnlineLearningModule::port_sizes, and PLearn::TMat< T >::resize().
{ port_sizes.resize(2,2); port_sizes.fill(-1); return port_sizes; }
RemoteMethodMap & PLearn::BinarizeModule::getRemoteMethodMap | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 55 of file BinarizeModule.cc.
void PLearn::BinarizeModule::makeDeepCopyFromShallowCopy | ( | CopiesMap & | copies | ) | [virtual] |
Transforms a shallow copy into a deep copy.
Reimplemented from PLearn::OnlineLearningModule.
Definition at line 225 of file BinarizeModule.cc.
References PLearn::OnlineLearningModule::makeDeepCopyFromShallowCopy(), and PLERROR.
{ inherited::makeDeepCopyFromShallowCopy(copies); // ### Call deepCopyField on all "pointer-like" fields // ### that you wish to be deepCopied rather than // ### shallow-copied. // ### ex: // deepCopyField(trainvec, copies); // ### Remove this line when you have fully implemented this method. PLERROR("BinarizeModule::makeDeepCopyFromShallowCopy not fully (correctly) implemented yet!"); }
Reimplemented from PLearn::OnlineLearningModule.
Definition at line 261 of file BinarizeModule.h.
If true just pass the gradient straight through with no alteration.
Definition at line 67 of file BinarizeModule.h.
Referenced by bpropAccUpdate(), and declareOptions().
If true then multiply output gradients by p(1-p) for input probability p.
Definition at line 70 of file BinarizeModule.h.
Referenced by bpropAccUpdate(), and declareOptions().
### declare public option fields (such as build options) here Start your comments with Doxygen-compatible comments such as //!
If true then sample the output bits stochastically, else use a hard threshold.
Definition at line 64 of file BinarizeModule.h.
Referenced by declareOptions(), and fprop().