PLearn 0.1
|
The first sentence should be a BRIEF DESCRIPTION of what the class does. More...
#include <SplitModule.h>
Public Member Functions | |
SplitModule () | |
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). | |
int | nPorts () |
Return the number of ports in the module. | |
virtual string | classname () const |
virtual OptionList & | getOptionList () const |
virtual OptionMap & | getOptionMap () const |
virtual RemoteMethodMap & | getRemoteMethodMap () const |
virtual SplitModule * | 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 | |
string | down_port_name |
### declare public option fields (such as build options) here Start your comments with Doxygen-compatible comments such as //! | |
TVec< int > | up_port_sizes |
TVec< string > | up_port_names |
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. | |
Private Attributes | |
TVec< string > | port_names |
TMat< int > | sizes |
The first sentence should be a BRIEF DESCRIPTION of what the class does.
Place the rest of the class programmer documentation here. Doxygen supports Javadoc-style comments. See http://www.doxygen.org/manual.html
Definition at line 57 of file SplitModule.h.
typedef OnlineLearningModule PLearn::SplitModule::inherited [private] |
Reimplemented from PLearn::OnlineLearningModule.
Definition at line 59 of file SplitModule.h.
PLearn::SplitModule::SplitModule | ( | ) |
Default constructor.
Definition at line 65 of file SplitModule.cc.
: down_port_name("down_port") { }
string PLearn::SplitModule::_classname_ | ( | ) | [static] |
Reimplemented from PLearn::OnlineLearningModule.
Definition at line 60 of file SplitModule.cc.
OptionList & PLearn::SplitModule::_getOptionList_ | ( | ) | [static] |
Reimplemented from PLearn::OnlineLearningModule.
Definition at line 60 of file SplitModule.cc.
RemoteMethodMap & PLearn::SplitModule::_getRemoteMethodMap_ | ( | ) | [static] |
Reimplemented from PLearn::OnlineLearningModule.
Definition at line 60 of file SplitModule.cc.
Reimplemented from PLearn::OnlineLearningModule.
Definition at line 60 of file SplitModule.cc.
Object * PLearn::SplitModule::_new_instance_for_typemap_ | ( | ) | [static] |
Reimplemented from PLearn::Object.
Definition at line 60 of file SplitModule.cc.
StaticInitializer SplitModule::_static_initializer_ & PLearn::SplitModule::_static_initialize_ | ( | ) | [static] |
Reimplemented from PLearn::OnlineLearningModule.
Definition at line 60 of file SplitModule.cc.
void PLearn::SplitModule::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 182 of file SplitModule.cc.
References i, PLearn::TMat< T >::length(), PLearn::TVec< T >::length(), nPorts(), PLASSERT, PLASSERT_MSG, PLearn::TMat< T >::resize(), PLearn::TVec< T >::resize(), PLearn::TMat< T >::subMatColumns(), up_port_sizes, and PLearn::TMat< T >::width().
{ PLASSERT( ports_value.length() == nPorts() && ports_gradient.length() == nPorts()); PLASSERT_MSG( up_port_sizes.length()>0, "SplitModule should have up_port_sizes specified with length>0"); if (ports_gradient[0] && ports_gradient[0]->isEmpty()) // the down_port is an input, do a backprop on SPLIT { int start=0; Mat& input_gradient = *ports_gradient[0]; for (int i=0;i<up_port_sizes.length();i++) { int width = up_port_sizes[i]; PLASSERT_MSG( ports_gradient[i+1] && !ports_gradient[i+1]->isEmpty(), "In SplitModule::bpropAccUpdate - When the down_port" " is an input and its gradient is required, one must" " provide a gradient on the up_ports" ); input_gradient.resize(ports_gradient[i+1]->length(), input_gradient.width()); input_gradient.subMatColumns(start, width) += *ports_gradient[i+1]; start += width; } return; } bool one_of_the_up_ports_wants_a_gradient = false; for (int i=0;i<up_port_sizes.length();i++) if (ports_gradient[i+1] && ports_gradient[i+1]->isEmpty()) { one_of_the_up_ports_wants_a_gradient = true; break; } if (one_of_the_up_ports_wants_a_gradient) // the down_port is an output, do a CONCATENATE { int start=0; Mat& output_gradient = *ports_gradient[0]; for (int i=0;i<up_port_sizes.length();i++) { int width = up_port_sizes[i]; if (ports_gradient[i+1] && ports_gradient[i+1]->isEmpty()) { ports_gradient[i+1]->resize(output_gradient.length(),width); (*ports_gradient[i+1]) += output_gradient.subMatColumns(start,width); } start += width; } return; } //PLERROR("In SplitModule::fprop - This configuration of ports not implemented for class " // "'%s'", classname().c_str()); }
void PLearn::SplitModule::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 118 of file SplitModule.cc.
References PLearn::OnlineLearningModule::build(), and build_().
{ inherited::build(); build_(); }
void PLearn::SplitModule::build_ | ( | ) | [private] |
This does the actual building.
Reimplemented from PLearn::OnlineLearningModule.
Definition at line 102 of file SplitModule.cc.
References i, PLearn::OnlineLearningModule::input_size, PLearn::TVec< T >::length(), PLearn::OnlineLearningModule::output_size, PLearn::TVec< T >::resize(), PLearn::sum(), PLearn::tostring(), up_port_names, and up_port_sizes.
Referenced by build().
{ int n_up_ports = up_port_sizes.length(); if (n_up_ports>0) // build cannot be completed until then { if (up_port_names.length()==0) // set default up_port_names { up_port_names.resize(n_up_ports); for (int i=0;i<n_up_ports;i++) up_port_names[i] = "up_port_" + tostring(i+1); } input_size = output_size = sum(up_port_sizes); } }
string PLearn::SplitModule::classname | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 60 of file SplitModule.cc.
Referenced by fprop().
void PLearn::SplitModule::declareOptions | ( | OptionList & | ol | ) | [static, protected] |
Declares the class options.
Reimplemented from PLearn::OnlineLearningModule.
Definition at line 73 of file SplitModule.cc.
References PLearn::OptionBase::buildoption, PLearn::declareOption(), PLearn::OnlineLearningModule::declareOptions(), down_port_name, PLearn::OnlineLearningModule::input_size, PLearn::OptionBase::learntoption, PLearn::OnlineLearningModule::output_size, PLearn::redeclareOption(), up_port_names, and up_port_sizes.
{ declareOption(ol, "down_port_name", &SplitModule::down_port_name, OptionBase::buildoption, "Name of the down-port. Default = 'down_port'."); declareOption(ol, "up_port_sizes", &SplitModule::up_port_sizes, OptionBase::buildoption, "Sizes of the up-ports. Mandatory.\n"); declareOption(ol, "up_port_names", &SplitModule::up_port_names, OptionBase::buildoption, "Names of the up-ports. Default = ['up_port_1','up_port_2',...]."); // Now call the parent class' declareOptions inherited::declareOptions(ol); redeclareOption(ol, "input_size", &SplitModule::input_size, OptionBase::learntoption, "input_size = down_port's size = sum(up_ports_size) but is set automatically."); redeclareOption(ol, "output_size", &SplitModule::output_size, OptionBase::learntoption, "output_size = sum(up_ports_size) but is set automatically."); }
static const PPath& PLearn::SplitModule::declaringFile | ( | ) | [inline, static] |
Reimplemented from PLearn::OnlineLearningModule.
Definition at line 152 of file SplitModule.h.
:
//##### Protected Member Functions ######################################
SplitModule * PLearn::SplitModule::deepCopy | ( | CopiesMap & | copies | ) | const [virtual] |
Reimplemented from PLearn::OnlineLearningModule.
Definition at line 60 of file SplitModule.cc.
virtual void PLearn::SplitModule::forget | ( | ) | [inline, 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 123 of file SplitModule.h.
{}
Perform a fprop step.
Each Mat* pointer in the 'ports_value' vector can be one of:
Reimplemented from PLearn::OnlineLearningModule.
Definition at line 136 of file SplitModule.cc.
References classname(), i, PLearn::OnlineLearningModule::input_size, PLearn::TMat< T >::length(), PLearn::TVec< T >::length(), nPorts(), PLASSERT, PLASSERT_MSG, PLERROR, PLearn::TMat< T >::resize(), PLearn::TVec< T >::resize(), PLearn::TMat< T >::subMatColumns(), and up_port_sizes.
{ PLASSERT( ports_value.length() == nPorts() ); PLASSERT_MSG( up_port_sizes.length()>0, "SplitModule should have up_port_sizes specified with length>0"); PLASSERT_MSG( ports_value[0], "SplitModule's down_port should always be connected.\n"); if (!ports_value[0]->isEmpty()) // the down_port is an input, do a SPLIT { int start=0; Mat& input = *ports_value[0]; for (int i=0;i<up_port_sizes.length();i++) { int width = up_port_sizes[i]; if (ports_value[i+1]) { PLASSERT_MSG(ports_value[i+1]->isEmpty(),"In SplitModule, when the down_port is an input, the up_ports should either be outputs or not connected.\n"); ports_value[i+1]->resize(input.length(),width); *ports_value[i+1] << input.subMatColumns(start,width); } start += width; } return; } else // the down_port is an output, do a CONCATENATE { int start=0; Mat& output = *ports_value[0]; // hacky check so that the resize does not fail PLASSERT_MSG(ports_value[1] && !ports_value[1]->isEmpty(), "In SplitModule, when the down_port is an output, all up_ports should be connected.\n"); output.resize(ports_value[1]->length(),input_size); for (int i=0;i<up_port_sizes.length();i++) { int width = up_port_sizes[i]; PLASSERT_MSG(ports_value[i+1] && !ports_value[i+1]->isEmpty(), "In SplitModule, when the down_port is an output, all up_ports should be connected.\n"); output.subMatColumns(start,width) << *ports_value[i+1]; start += width; } return; } PLERROR("In SplitModule::fprop - Not implemented for class " "'%s'", classname().c_str()); }
OptionList & PLearn::SplitModule::getOptionList | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 60 of file SplitModule.cc.
OptionMap & PLearn::SplitModule::getOptionMap | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 60 of file SplitModule.cc.
const TVec< string > & PLearn::SplitModule::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 238 of file SplitModule.cc.
References down_port_name, PLearn::TVec< T >::isEmpty(), nPorts(), port_names, PLearn::TVec< T >::resize(), PLearn::TVec< T >::subVec(), and up_port_names.
{ if (port_names.isEmpty()) { port_names.resize(nPorts()); port_names[0] = down_port_name; port_names.subVec(1,nPorts()-1) << up_port_names; } return port_names; }
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 251 of file SplitModule.cc.
References PLearn::TMat< T >::column(), PLearn::TMat< T >::fill(), PLearn::OnlineLearningModule::input_size, PLearn::TMat< T >::isEmpty(), nPorts(), PLearn::TMat< T >::resize(), sizes, PLearn::TMat< T >::subMat(), PLearn::TVec< T >::toMat(), and up_port_sizes.
{ if (sizes.isEmpty()) { sizes.resize(nPorts(),2); sizes.column(0).fill(-1); sizes(0,1) = input_size; sizes.subMat(1,1,nPorts()-1,1) << up_port_sizes.toMat(nPorts()-1,1); } return sizes; }
RemoteMethodMap & PLearn::SplitModule::getRemoteMethodMap | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 60 of file SplitModule.cc.
void PLearn::SplitModule::makeDeepCopyFromShallowCopy | ( | CopiesMap & | copies | ) | [virtual] |
Transforms a shallow copy into a deep copy.
Reimplemented from PLearn::OnlineLearningModule.
Definition at line 125 of file SplitModule.cc.
References PLearn::deepCopyField(), PLearn::OnlineLearningModule::makeDeepCopyFromShallowCopy(), up_port_names, and up_port_sizes.
{ inherited::makeDeepCopyFromShallowCopy(copies); deepCopyField(up_port_sizes, copies); deepCopyField(up_port_names, copies); }
int PLearn::SplitModule::nPorts | ( | ) |
Return the number of ports in the module.
Reimplemented from PLearn::OnlineLearningModule.
Definition at line 265 of file SplitModule.cc.
References PLearn::TVec< T >::length(), and up_port_sizes.
Referenced by bpropAccUpdate(), fprop(), getPorts(), and getPortSizes().
{ return 1+up_port_sizes.length(); }
Reimplemented from PLearn::OnlineLearningModule.
Definition at line 152 of file SplitModule.h.
### declare public option fields (such as build options) here Start your comments with Doxygen-compatible comments such as //!
Definition at line 67 of file SplitModule.h.
Referenced by declareOptions(), and getPorts().
TVec<string> PLearn::SplitModule::port_names [private] |
Definition at line 178 of file SplitModule.h.
Referenced by getPorts().
TMat<int> PLearn::SplitModule::sizes [private] |
Definition at line 179 of file SplitModule.h.
Referenced by getPortSizes().
Definition at line 69 of file SplitModule.h.
Referenced by build_(), declareOptions(), getPorts(), and makeDeepCopyFromShallowCopy().
Definition at line 68 of file SplitModule.h.
Referenced by bpropAccUpdate(), build_(), declareOptions(), fprop(), getPortSizes(), makeDeepCopyFromShallowCopy(), and nPorts().