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

#include <OnBagsModule.h>

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

List of all members.

Public Member Functions

 OnBagsModule ()
 Default constructor.
virtual void fpropAcc (const Vec &input)
 ### Main functions to be coded in subclasses ###
virtual void fpropInit (const Vec &input)
virtual void fpropOutput (Vec &output)
virtual void bprop (const Mat &baginputs, const Vec &bagoutput_gradient, Mat &baginputs_gradients)
 compute the gradient w.r.t bag inputs
virtual void fprop (const TVec< Mat * > &ports_value)
 #################################################
virtual void fprop (const Mat &inputs, const Mat &targets, Mat &outputs)
virtual void fprop (const Vec &input, const Vec &target, Vec &output)
virtual void bpropUpdate (const Mat &inputs, const Mat &targets, Mat &input_gradients, const Mat &output_gradients, bool accumulate=false)
 backpropagate the derivative w.r.t. activation
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 const TVec< string > & getPorts ()
 Returns all ports in the module.
virtual const TMat< int > & getPortSizes ()
 Return all ports sizes.
virtual int getPortIndex (const string &port)
 Return the index (as in the list of ports returned by getPorts()) of a given port.
virtual void setLearningRate (real dynamic_learning_rate)
 Overridden to do nothing (in particular, no warning).
virtual TVec< string > name ()
 Indicates the name of the computed costs.
virtual string classname () const
virtual OptionListgetOptionList () const
virtual OptionMapgetOptionMap () const
virtual RemoteMethodMapgetRemoteMethodMap () const
virtual OnBagsModuledeepCopy (CopiesMap &copies) const
virtual void build ()
 Post-constructor.
virtual void makeDeepCopyFromShallowCopy (CopiesMap &copies)
 Transforms a shallow copy into a deep copy.
virtual void forget ()
 reset the parameters to the state they would be BEFORE starting training.

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

int bagtarget_size

Static Public Attributes

static StaticInitializer _static_initializer_

Protected Member Functions

void addPortName (const string &name)
 Add a new port to the 'portname_to_index' map and 'ports' vector.

Static Protected Member Functions

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

Protected Attributes

bool wait_new_bag
map< string, intportname_to_index
 Map from a port name to its index in the 'ports' vector.
TVec< string > ports
 List of port names.

Private Types

typedef OnlineLearningModule inherited

Private Member Functions

void build_ ()
 This does the actual building.

Detailed Description

Definition at line 48 of file OnBagsModule.h.


Member Typedef Documentation

Reimplemented from PLearn::OnlineLearningModule.

Reimplemented in PLearn::LogaddOnBagsModule.

Definition at line 50 of file OnBagsModule.h.


Constructor & Destructor Documentation

PLearn::OnBagsModule::OnBagsModule ( )

Default constructor.

Definition at line 57 of file OnBagsModule.cc.

                          :
    bagtarget_size(1)
{
}

Member Function Documentation

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

Reimplemented from PLearn::OnlineLearningModule.

Reimplemented in PLearn::LogaddOnBagsModule.

Definition at line 55 of file OnBagsModule.cc.

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

Reimplemented from PLearn::OnlineLearningModule.

Reimplemented in PLearn::LogaddOnBagsModule.

Definition at line 55 of file OnBagsModule.cc.

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

Reimplemented from PLearn::OnlineLearningModule.

Reimplemented in PLearn::LogaddOnBagsModule.

Definition at line 55 of file OnBagsModule.cc.

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

Reimplemented from PLearn::OnlineLearningModule.

Reimplemented in PLearn::LogaddOnBagsModule.

Definition at line 55 of file OnBagsModule.cc.

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

Reimplemented from PLearn::Object.

Reimplemented in PLearn::LogaddOnBagsModule.

Definition at line 55 of file OnBagsModule.cc.

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

Reimplemented from PLearn::OnlineLearningModule.

Reimplemented in PLearn::LogaddOnBagsModule.

Definition at line 55 of file OnBagsModule.cc.

void PLearn::OnBagsModule::addPortName ( const string &  name) [protected]

Add a new port to the 'portname_to_index' map and 'ports' vector.

Definition at line 305 of file OnBagsModule.cc.

References PLearn::TVec< T >::append(), PLearn::TVec< T >::length(), name(), PLASSERT, portname_to_index, and ports.

Referenced by build_().

{
    PLASSERT( portname_to_index.find(name) == portname_to_index.end() );
    portname_to_index[name] = ports.length();
    ports.append(name);
}

Here is the call graph for this function:

Here is the caller graph for this function:

void PLearn::OnBagsModule::bprop ( const Mat baginputs,
const Vec bagoutput_gradient,
Mat baginputs_gradients 
) [virtual]

compute the gradient w.r.t bag inputs

Reimplemented in PLearn::LogaddOnBagsModule.

Definition at line 286 of file OnBagsModule.cc.

References classname(), and PLERROR.

Referenced by bpropUpdate().

{
    PLERROR("In OnBagsModule::bprop() - not implemented for class %s",classname().c_str() );
}

Here is the call graph for this function:

Here is the caller graph for this function:

void PLearn::OnBagsModule::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 194 of file OnBagsModule.cc.

References bpropUpdate(), PLearn::OnlineLearningModule::checkProp(), PLearn::TMat< T >::clear(), getPortIndex(), PLearn::OnlineLearningModule::input_size, PLearn::TMat< T >::isEmpty(), PLearn::TMat< T >::length(), PLearn::TVec< T >::length(), PLearn::OnlineLearningModule::nPorts(), PLearn::OnlineLearningModule::output_size, PLASSERT, PLERROR, PLearn::TMat< T >::resize(), and PLearn::TMat< T >::width().

{
    PLASSERT( input_size > 1 );
    PLASSERT( ports_value.length() == nPorts() );
    PLASSERT( ports_gradient.length() == nPorts() );

    const Mat* p_inputs = ports_value[getPortIndex("input")];
    const Mat* p_bagtargets = ports_value[getPortIndex("bagtarget")];
    const Mat* p_output_grad = ports_gradient[getPortIndex("output")];
    Mat* p_inputs_grad = ports_gradient[getPortIndex("input")];

    if( p_inputs_grad
        && p_output_grad && !p_output_grad->isEmpty() )
    {
        PLASSERT( p_inputs && !p_inputs->isEmpty());
        PLASSERT( p_bagtargets && !p_bagtargets->isEmpty());
        int n_samples = p_inputs->length();
        PLASSERT( p_output_grad->length() == n_samples );
        PLASSERT( p_output_grad->width() == output_size );
        if( p_inputs_grad->isEmpty() )
        {
            p_inputs_grad->resize( n_samples, input_size);
            p_inputs_grad->clear();
        }
        bpropUpdate( *p_inputs, *p_bagtargets,
                     *p_inputs_grad, *p_output_grad, true );
        checkProp(ports_gradient);
    }
    else if( !p_inputs_grad && !p_output_grad )
        return;
    else
        PLERROR("In OnBagsModule::bpropAccUpdate - Port configuration not implemented.");

}

Here is the call graph for this function:

void PLearn::OnBagsModule::bpropUpdate ( const Mat inputs,
const Mat targets,
Mat input_gradients,
const Mat output_gradients,
bool  accumulate = false 
) [virtual]

backpropagate the derivative w.r.t. activation

Reimplemented from PLearn::OnlineLearningModule.

Definition at line 230 of file OnBagsModule.cc.

References bagtarget_size, bprop(), PLearn::TMat< T >::fill(), PLearn::OnlineLearningModule::input_size, PLearn::is_missing(), j, PLearn::TMat< T >::length(), PLearn::OnlineLearningModule::output_size, PLASSERT, PLASSERT_MSG, PLERROR, PLearn::TMat< T >::resize(), PLearn::TMat< T >::subMatRows(), PLearn::SumOverBagsVariable::TARGET_COLUMN_FIRST, PLearn::SumOverBagsVariable::TARGET_COLUMN_LAST, and PLearn::TMat< T >::width().

Referenced by bpropAccUpdate().

{
    PLASSERT( inputs.width() == input_size );
    PLASSERT( bagtargets.width() == bagtarget_size );
    PLASSERT( output_gradients.width() == output_size );
    int n_samples = inputs.length();
    PLASSERT( n_samples == bagtargets.length() );
    PLASSERT( n_samples == output_gradients.length() );
    if( accumulate )
        PLASSERT_MSG( input_gradients.width() == input_size &&
                input_gradients.length() == n_samples,
                "Cannot resize input_gradients and accumulate into it." );
    else
    {
        input_gradients.resize(n_samples, input_size);
        input_gradients.fill(0);
    }
    bool bprop_wait_new_bag = true;
    int bag_start;
    for (int j = 0; j < n_samples; j++) {
        int bag_info = int(round(bagtargets(j).lastElement()));
        if (bag_info & SumOverBagsVariable::TARGET_COLUMN_FIRST)
        {
            PLASSERT( bprop_wait_new_bag );
            bprop_wait_new_bag = false;
            bag_start = j;
        }
        else
            PLASSERT( !bprop_wait_new_bag );
        if (bag_info & SumOverBagsVariable::TARGET_COLUMN_LAST)
        {
            PLASSERT( !is_missing(output_gradients(j,0)) );
            bprop_wait_new_bag = true;
            int bag_length = j - bag_start + 1;
            Mat baginputs_gradients;
            bprop( inputs.subMatRows(bag_start, bag_length),
                   output_gradients(j),
                   baginputs_gradients );
            input_gradients.subMatRows(bag_start, bag_length) << baginputs_gradients;
        }
        else
            PLASSERT( is_missing(output_gradients(j,0)) );
    }

    if (!bprop_wait_new_bag)
        PLERROR("In OnBagsModule::bpropUpdate - entire bags must be given."
                "If you use ModuleLearner, you should use the option operate_on_bags = True");
}

Here is the call graph for this function:

Here is the caller graph for this function:

void PLearn::OnBagsModule::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.

Reimplemented in PLearn::LogaddOnBagsModule.

Definition at line 90 of file OnBagsModule.cc.

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

Here is the call graph for this function:

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

This does the actual building.

Reimplemented from PLearn::OnlineLearningModule.

Reimplemented in PLearn::LogaddOnBagsModule.

Definition at line 71 of file OnBagsModule.cc.

References addPortName(), bagtarget_size, PLearn::TMat< T >::fill(), getPortIndex(), PLearn::OnlineLearningModule::input_size, PLearn::OnlineLearningModule::nPorts(), PLearn::OnlineLearningModule::output_size, PLASSERT, PLearn::OnlineLearningModule::port_sizes, portname_to_index, ports, PLearn::TMat< T >::resize(), and PLearn::TVec< T >::resize().

Referenced by build().

{
    PLASSERT( bagtarget_size > 0 );
    PLASSERT( input_size > 0 );

    // The port story...
    ports.resize(0);
    portname_to_index.clear();
    addPortName("input");
    addPortName("output");
    addPortName("bagtarget");

    port_sizes.resize(nPorts(), 3);
    port_sizes.fill(-1);
    port_sizes(getPortIndex("input"), 1) = input_size;
    port_sizes(getPortIndex("output"), 1) = output_size;
    port_sizes(getPortIndex("bagtarget"), 1) = bagtarget_size;
}

Here is the call graph for this function:

Here is the caller graph for this function:

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

Reimplemented from PLearn::Object.

Reimplemented in PLearn::LogaddOnBagsModule.

Definition at line 55 of file OnBagsModule.cc.

Referenced by bprop(), fpropAcc(), fpropInit(), and fpropOutput().

Here is the caller graph for this function:

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

Declares the class options.

Reimplemented from PLearn::OnlineLearningModule.

Reimplemented in PLearn::LogaddOnBagsModule.

Definition at line 62 of file OnBagsModule.cc.

References bagtarget_size, PLearn::OptionBase::buildoption, PLearn::declareOption(), and PLearn::OnlineLearningModule::declareOptions().

{
    inherited::declareOptions(ol);

    declareOption(ol, "bagtarget_size", &OnBagsModule::bagtarget_size,
                  OptionBase::buildoption,
                  "Size of the 'bagtarget' port (bag info is the last element).");
}

Here is the call graph for this function:

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

Reimplemented from PLearn::OnlineLearningModule.

Reimplemented in PLearn::LogaddOnBagsModule.

Definition at line 114 of file OnBagsModule.h.

:

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

Reimplemented from PLearn::OnlineLearningModule.

Reimplemented in PLearn::LogaddOnBagsModule.

Definition at line 55 of file OnBagsModule.cc.

void PLearn::OnBagsModule::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 96 of file OnBagsModule.cc.

References wait_new_bag.

{
    wait_new_bag = true;
}
void PLearn::OnBagsModule::fprop ( const TVec< Mat * > &  ports_value) [virtual]

#################################################

given the input and target, compute the cost

Reimplemented from PLearn::OnlineLearningModule.

Definition at line 113 of file OnBagsModule.cc.

References PLearn::OnlineLearningModule::checkProp(), getPortIndex(), PLearn::TMat< T >::isEmpty(), PLearn::TVec< T >::length(), PLearn::OnlineLearningModule::nPorts(), PLASSERT, and PLERROR.

Referenced by fprop().

{
    PLASSERT( ports_value.length() == nPorts() );

    Mat* p_inputs = ports_value[getPortIndex("input")];
    Mat* p_bagtargets = ports_value[getPortIndex("bagtarget")];
    Mat* p_outputs = ports_value[getPortIndex("output")];

    if ( p_outputs && p_outputs->isEmpty() )
    {
        PLASSERT( p_inputs && !p_inputs->isEmpty() );
        PLASSERT( p_bagtargets && !p_bagtargets->isEmpty() );
        fprop(*p_inputs, *p_bagtargets, *p_outputs);
        checkProp(ports_value);
    }
    else
        PLERROR("In OnBagsModule::fprop - Port configuration not implemented.");
}

Here is the call graph for this function:

Here is the caller graph for this function:

void PLearn::OnBagsModule::fprop ( const Vec input,
const Vec target,
Vec output 
) [virtual]
void PLearn::OnBagsModule::fprop ( const Mat inputs,
const Mat targets,
Mat outputs 
) [virtual]

Definition at line 132 of file OnBagsModule.cc.

References fprop(), PLearn::TMat< T >::length(), PLearn::OnlineLearningModule::output_size, PLearn::TMat< T >::resize(), and PLearn::TMat< T >::row().

{
    int n_samples = inputs.length();
    outputs.resize( n_samples, output_size );
    //outputs.fill( MISSING_VALUE );
    for (int isample = 0; isample < n_samples; isample++)
    {
        Vec output;
        fprop( inputs(isample), bagtargets(isample), output );
        outputs.row(isample) << output;
    }
}

Here is the call graph for this function:

void PLearn::OnBagsModule::fpropAcc ( const Vec input) [virtual]

### Main functions to be coded in subclasses ###

update internal statistics needed to compute the output of bag

Reimplemented in PLearn::LogaddOnBagsModule.

Definition at line 178 of file OnBagsModule.cc.

References classname(), and PLERROR.

Referenced by fprop().

{
    PLERROR("In OnBagsModule::fpropAcc(input,output) - not implemented for class %s",classname().c_str());
}

Here is the call graph for this function:

Here is the caller graph for this function:

void PLearn::OnBagsModule::fpropInit ( const Vec input) [virtual]

Reimplemented in PLearn::LogaddOnBagsModule.

Definition at line 172 of file OnBagsModule.cc.

References classname(), and PLERROR.

Referenced by fprop().

{
    PLERROR("In OnBagsModule::fpropInit(input,output) - not implemented for class %s",classname().c_str());
}

Here is the call graph for this function:

Here is the caller graph for this function:

void PLearn::OnBagsModule::fpropOutput ( Vec output) [virtual]

Reimplemented in PLearn::LogaddOnBagsModule.

Definition at line 185 of file OnBagsModule.cc.

References classname(), and PLERROR.

Referenced by fprop().

{
    PLERROR("In OnBagsModule::fpropOutput(input,output) - not implemented for class %s",classname().c_str());
}

Here is the call graph for this function:

Here is the caller graph for this function:

OptionList & PLearn::OnBagsModule::getOptionList ( ) const [virtual]

Reimplemented from PLearn::Object.

Reimplemented in PLearn::LogaddOnBagsModule.

Definition at line 55 of file OnBagsModule.cc.

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

Reimplemented from PLearn::Object.

Reimplemented in PLearn::LogaddOnBagsModule.

Definition at line 55 of file OnBagsModule.cc.

int PLearn::OnBagsModule::getPortIndex ( const string &  port) [virtual]

Return the index (as in the list of ports returned by getPorts()) of a given port.

If 'port' does not exist, -1 is returned.

Reimplemented from PLearn::OnlineLearningModule.

Definition at line 331 of file OnBagsModule.cc.

References portname_to_index.

Referenced by bpropAccUpdate(), build_(), and fprop().

{
    map<string, int>::const_iterator it = portname_to_index.find(port);
    if (it == portname_to_index.end())
        return -1;
    else
        return it->second;
}

Here is the caller graph for this function:

const TVec< string > & PLearn::OnBagsModule::getPorts ( ) [virtual]

Returns all ports in the module.

Reimplemented from PLearn::OnlineLearningModule.

Definition at line 315 of file OnBagsModule.cc.

References ports.

{
    return ports;
}
const TMat< int > & PLearn::OnBagsModule::getPortSizes ( ) [virtual]

Return all ports sizes.

Reimplemented from PLearn::OnlineLearningModule.

Definition at line 323 of file OnBagsModule.cc.

References PLearn::OnlineLearningModule::port_sizes.

{
    return port_sizes;
}
RemoteMethodMap & PLearn::OnBagsModule::getRemoteMethodMap ( ) const [virtual]

Reimplemented from PLearn::Object.

Reimplemented in PLearn::LogaddOnBagsModule.

Definition at line 55 of file OnBagsModule.cc.

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

Transforms a shallow copy into a deep copy.

Reimplemented from PLearn::OnlineLearningModule.

Reimplemented in PLearn::LogaddOnBagsModule.

Definition at line 101 of file OnBagsModule.cc.

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

Here is the call graph for this function:

TVec< string > PLearn::OnBagsModule::name ( ) [virtual]

Indicates the name of the computed costs.

Definition at line 297 of file OnBagsModule.cc.

Referenced by addPortName().

{
    return TVec<string>(1, OnlineLearningModule::name);
}

Here is the caller graph for this function:

virtual void PLearn::OnBagsModule::setLearningRate ( real  dynamic_learning_rate) [inline, virtual]

Overridden to do nothing (in particular, no warning).

Reimplemented from PLearn::OnlineLearningModule.

Definition at line 104 of file OnBagsModule.h.

{}

Member Data Documentation

Reimplemented from PLearn::OnlineLearningModule.

Reimplemented in PLearn::LogaddOnBagsModule.

Definition at line 114 of file OnBagsModule.h.

Definition at line 55 of file OnBagsModule.h.

Referenced by bpropUpdate(), build_(), declareOptions(), and fprop().

map<string, int> PLearn::OnBagsModule::portname_to_index [protected]

Map from a port name to its index in the 'ports' vector.

Definition at line 129 of file OnBagsModule.h.

Referenced by addPortName(), build_(), and getPortIndex().

TVec<string> PLearn::OnBagsModule::ports [protected]

List of port names.

Definition at line 132 of file OnBagsModule.h.

Referenced by addPortName(), build_(), getPorts(), and makeDeepCopyFromShallowCopy().

Definition at line 126 of file OnBagsModule.h.

Referenced by forget(), and fprop().


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