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

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>

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

List of all members.

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

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.

Detailed Description

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.


Member Typedef Documentation

Reimplemented from PLearn::OnlineLearningModule.

Definition at line 55 of file BinarizeModule.h.


Constructor & Destructor Documentation

PLearn::BinarizeModule::BinarizeModule ( )

Default constructor.

Definition at line 60 of file BinarizeModule.cc.

    : stochastic(true),copy_gradients(false),saturate_gradients(false)
{
}

Member Function Documentation

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.

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

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:

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

Here is the call graph for this function:

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

Here is the call graph for this function:

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

{
}

Here is the caller graph for this function:

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

Here is the call graph for this function:

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.

{
}
void PLearn::BinarizeModule::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 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;
    }
}

Here is the call graph for this function:

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

Here is the call graph for this function:

const TMat< int > & PLearn::BinarizeModule::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

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

Here is the call graph for this function:

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

Here is the call graph for this function:


Member Data Documentation

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


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