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

The first sentence should be a BRIEF DESCRIPTION of what the class does. More...

#include <SplitModule.h>

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

List of all members.

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 OptionListgetOptionList () const
virtual OptionMapgetOptionMap () const
virtual RemoteMethodMapgetRemoteMethodMap () const
virtual SplitModuledeepCopy (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 PPathdeclaringFile ()

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< intup_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< intsizes

Detailed Description

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

Todo:
Write class to-do's here if there are any.
Deprecated:
Write deprecated stuff here if there is any. Indicate what else should be used instead.

Definition at line 57 of file SplitModule.h.


Member Typedef Documentation

Reimplemented from PLearn::OnlineLearningModule.

Definition at line 59 of file SplitModule.h.


Constructor & Destructor Documentation

PLearn::SplitModule::SplitModule ( )

Default constructor.

Definition at line 65 of file SplitModule.cc.

    : down_port_name("down_port")
{
}

Member Function Documentation

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.

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

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:

  • a full matrix : this is the gradient that is provided to the module, and can be used to compute other ports' gradient.
  • an empty matrix: this is a gradient we want to compute and accumulate into. This matrix must have length 0 and a width equal to the width of the corresponding matrix in the 'ports_value' vector (we can thus accumulate gradients using PLearn's ability to keep intact stored values when resizing a matrix' length).
  • a NULL pointer : this is a gradient that is not available, but does not need to be returned (or even computed). The default version tries to use the standard mini-batch bpropUpdate method, when possible.

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

Here is the call graph for this function:

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

Here is the call graph for this function:

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

Here is the call graph for this function:

Here is the caller graph for this function:

string PLearn::SplitModule::classname ( ) const [virtual]

Reimplemented from PLearn::Object.

Definition at line 60 of file SplitModule.cc.

Referenced by fprop().

Here is the caller graph for this function:

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

Here is the call graph for this function:

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.

{}
void PLearn::SplitModule::fprop ( const TVec< Mat * > &  ports_value) [virtual]

Perform a fprop step.

Each Mat* pointer in the 'ports_value' vector can be one of:

  • a full matrix: this is data that is provided to the module, and can be used to compute other ports' values
  • an empty matrix: this is what we want to compute
  • a NULL pointer: this is data that is not available, but whose value does not need to be returned (or even computed) The default version will either:
  • call the mini-batch versions of standard fprop if 'ports_value' has size 2, with the first value being provided (and the second being the desired output)
  • crash otherwise

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

Here is the call graph for this function:

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

Here is the call graph for this function:

const TMat< int > & PLearn::SplitModule::getPortSizes ( ) [virtual]

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:

  • in the first column (lengths): -1
  • in the second column (widths):
    • -1 if nPorts() != 2
    • 'input_size' for the first row and 'output_size' for the second row if nPorts() == 2 (also assuming the port names are respectively 'input' and 'output')

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

Here is the call graph for this function:

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.

Here is the call graph for this function:

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

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

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

Definition at line 178 of file SplitModule.h.

Referenced by getPorts().

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


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