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

#include <ForwardModule.h>

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

List of all members.

Public Member Functions

 ForwardModule (const string &the_name="", bool call_build_=false)
 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 void finalize ()
 Perform some processing after training, or after a series of fprop/bpropUpdate calls to prepare the model for truly out-of-sample operation.
virtual bool bpropDoesNothing ()
 In case bpropUpdate does not do anything, make it known.
virtual void setLearningRate (real dynamic_learning_rate)
 If this class has a learning rate (or something close to it), set it.
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 OptionListgetOptionList () const
virtual OptionMapgetOptionMap () const
virtual RemoteMethodMapgetRemoteMethodMap () const
virtual ForwardModuledeepCopy (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

TVec< PP< OnlineLearningModule > > modules
string forward_to
bool forget_all

Static Public Attributes

static StaticInitializer _static_initializer_

Static Protected Member Functions

static void declareOptions (OptionList &ol)
 Declares the class options.

Protected Attributes

int current
 Index of the module currently being used in the 'modules' list.

Private Types

typedef OnlineLearningModule inherited

Private Member Functions

void build_ ()
 This does the actual building.

Detailed Description

Definition at line 47 of file ForwardModule.h.


Member Typedef Documentation

Reimplemented from PLearn::OnlineLearningModule.

Definition at line 49 of file ForwardModule.h.


Constructor & Destructor Documentation

PLearn::ForwardModule::ForwardModule ( const string &  the_name = "",
bool  call_build_ = false 
)

Constructor.

Definition at line 56 of file ForwardModule.cc.

References build_().

                                                                    :
    inherited(the_name.empty() && call_build_ ? classname() : the_name,
              call_build_),
    forget_all(true),
    current(-1)
{
    if (call_build_)
        build_();
}

Here is the call graph for this function:


Member Function Documentation

string PLearn::ForwardModule::_classname_ ( ) [static]

Reimplemented from PLearn::OnlineLearningModule.

Definition at line 51 of file ForwardModule.cc.

OptionList & PLearn::ForwardModule::_getOptionList_ ( ) [static]

Reimplemented from PLearn::OnlineLearningModule.

Definition at line 51 of file ForwardModule.cc.

RemoteMethodMap & PLearn::ForwardModule::_getRemoteMethodMap_ ( ) [static]

Reimplemented from PLearn::OnlineLearningModule.

Definition at line 51 of file ForwardModule.cc.

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

Reimplemented from PLearn::OnlineLearningModule.

Definition at line 51 of file ForwardModule.cc.

Object * PLearn::ForwardModule::_new_instance_for_typemap_ ( ) [static]

Reimplemented from PLearn::Object.

Definition at line 51 of file ForwardModule.cc.

StaticInitializer ForwardModule::_static_initializer_ & PLearn::ForwardModule::_static_initialize_ ( ) [static]

Reimplemented from PLearn::OnlineLearningModule.

Definition at line 51 of file ForwardModule.cc.

void PLearn::ForwardModule::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 165 of file ForwardModule.cc.

References current, and modules.

{
    modules[current]->bpropAccUpdate(ports_value, ports_gradient);
}
bool PLearn::ForwardModule::bpropDoesNothing ( ) [virtual]

In case bpropUpdate does not do anything, make it known.

Reimplemented from PLearn::OnlineLearningModule.

Definition at line 194 of file ForwardModule.cc.

References current, and modules.

{
     return modules[current]->bpropDoesNothing();
}
void PLearn::ForwardModule::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 137 of file ForwardModule.cc.

References PLearn::OnlineLearningModule::build(), and build_().

Here is the call graph for this function:

void PLearn::ForwardModule::build_ ( ) [private]

This does the actual building.

Reimplemented from PLearn::OnlineLearningModule.

Definition at line 105 of file ForwardModule.cc.

References current, forward_to, i, PLearn::TVec< T >::length(), modules, PLearn::OnlineLearningModule::name, PLCHECK, and PLearn::OnlineLearningModule::random_gen.

Referenced by build(), and ForwardModule().

{
    current = -1;
    if (forward_to.empty())
        current = 0;
    else {
        for (int i = 0; i < modules.length(); i++) {
            if (modules[i]->name == forward_to) {
                current = i;
                break;
            }
        }
    }
    PLCHECK( current >= 0 );

    // Forward random number generator to all underlying modules. Note that we
    // need to forward it to all modules because we call forget().
    if (random_gen) {
        for (int i = 0; i < modules.length(); i++) {
            PP<OnlineLearningModule> mod = modules[i];
            if (!mod->random_gen) {
                mod->random_gen = random_gen;
                mod->build();
                mod->forget();
            }
        }
    }
}

Here is the call graph for this function:

Here is the caller graph for this function:

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

Reimplemented from PLearn::Object.

Definition at line 51 of file ForwardModule.cc.

void PLearn::ForwardModule::declareOptions ( OptionList ol) [static, protected]

Declares the class options.

Reimplemented from PLearn::OnlineLearningModule.

Definition at line 69 of file ForwardModule.cc.

References PLearn::OptionBase::buildoption, current, PLearn::declareOption(), PLearn::OnlineLearningModule::declareOptions(), forget_all, forward_to, modules, and PLearn::OptionBase::nosave.

{

    // 'build' options.

    declareOption(ol, "modules", &ForwardModule::modules,
                  OptionBase::buildoption,
        "The list of modules that can be used to forward calls.");

    declareOption(ol, "forward_to", &ForwardModule::forward_to,
                  OptionBase::buildoption,
        "Name of the module currently being used. If empty, the first module\n"
        "in 'modules' will be used.");

    declareOption(ol, "forget_all", &ForwardModule::forget_all,
                  OptionBase::buildoption,
        "If set to 1, then the forget() method will call forget() on all\n"
        "modules. Otherwise, only the current module pointed by 'forward_to'\n"
        "will be forgotten.");

    // 'nosave' options.

    // 'current' is an option only so it can be modified in server mode for
    // instance, in order to bypass a call to build (yes, this is a hack!).
    declareOption(ol, "current", &ForwardModule::current,
                  OptionBase::nosave,
        "Index in 'modules' of the module given by 'forward_to'.");

    // Now call the parent class' declareOptions
    inherited::declareOptions(ol);

}

Here is the call graph for this function:

static const PPath& PLearn::ForwardModule::declaringFile ( ) [inline, static]

Reimplemented from PLearn::OnlineLearningModule.

Definition at line 146 of file ForwardModule.h.

:

ForwardModule * PLearn::ForwardModule::deepCopy ( CopiesMap copies) const [virtual]

Reimplemented from PLearn::OnlineLearningModule.

Definition at line 51 of file ForwardModule.cc.

void PLearn::ForwardModule::finalize ( ) [virtual]

Perform some processing after training, or after a series of fprop/bpropUpdate calls to prepare the model for truly out-of-sample operation.

Reimplemented from PLearn::OnlineLearningModule.

Definition at line 186 of file ForwardModule.cc.

References current, and modules.

{
     modules[current]->finalize();
}
void PLearn::ForwardModule::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 174 of file ForwardModule.cc.

References current, forget_all, i, PLearn::TVec< T >::length(), and modules.

{
    if (forget_all) {
        for (int i = 0; i < modules.length(); i++)
            modules[i]->forget();
    } else
        modules[current]->forget();
}

Here is the call graph for this function:

void PLearn::ForwardModule::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 157 of file ForwardModule.cc.

References current, and modules.

{
    modules[current]->fprop(ports_value);
}
OptionList & PLearn::ForwardModule::getOptionList ( ) const [virtual]

Reimplemented from PLearn::Object.

Definition at line 51 of file ForwardModule.cc.

OptionMap & PLearn::ForwardModule::getOptionMap ( ) const [virtual]

Reimplemented from PLearn::Object.

Definition at line 51 of file ForwardModule.cc.

const TVec< string > & PLearn::ForwardModule::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 210 of file ForwardModule.cc.

References current, and modules.

                                            {
    return modules[current]->getPorts();
}
const TMat< int > & PLearn::ForwardModule::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 217 of file ForwardModule.cc.

References current, and modules.

{
    return modules[current]->getPortSizes();
}
RemoteMethodMap & PLearn::ForwardModule::getRemoteMethodMap ( ) const [virtual]

Reimplemented from PLearn::Object.

Definition at line 51 of file ForwardModule.cc.

void PLearn::ForwardModule::makeDeepCopyFromShallowCopy ( CopiesMap copies) [virtual]

Transforms a shallow copy into a deep copy.

Reimplemented from PLearn::OnlineLearningModule.

Definition at line 147 of file ForwardModule.cc.

References PLearn::deepCopyField(), PLearn::OnlineLearningModule::makeDeepCopyFromShallowCopy(), and modules.

Here is the call graph for this function:

void PLearn::ForwardModule::setLearningRate ( real  dynamic_learning_rate) [virtual]

If this class has a learning rate (or something close to it), set it.

If not, you can redefine this method to get rid of the warning.

Reimplemented from PLearn::OnlineLearningModule.

Definition at line 202 of file ForwardModule.cc.

References current, and modules.

{
     modules[current]->setLearningRate(dynamic_learning_rate);
}

Member Data Documentation

Reimplemented from PLearn::OnlineLearningModule.

Definition at line 146 of file ForwardModule.h.

Index of the module currently being used in the 'modules' list.

Definition at line 158 of file ForwardModule.h.

Referenced by bpropAccUpdate(), bpropDoesNothing(), build_(), declareOptions(), finalize(), forget(), fprop(), getPorts(), getPortSizes(), and setLearningRate().

Definition at line 57 of file ForwardModule.h.

Referenced by declareOptions(), and forget().

Definition at line 56 of file ForwardModule.h.

Referenced by build_(), and declareOptions().


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