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::ChainedLearners Class Reference

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

#include <ChainedLearners.h>

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

List of all members.

Public Member Functions

 ChainedLearners ()
 Default constructor.
virtual int outputsize () const
 Returns the size of this learner's output, (which typically may depend on its inputsize(), targetsize() and set options).
virtual void forget ()
 (Re-)initializes the PLearner in its fresh state (that state may depend on the 'seed' option) and sets 'stage' back to 0 (this is the stage of a fresh learner!).
virtual void setTrainingSet (VMat training_set, bool call_forget=true)
 Declares the training set.
virtual void train ()
 The role of the train method is to bring the learner up to stage==nstages, updating the train_stats collector with training costs measured on-line in the process.
virtual void computeOutput (const Vec &input, Vec &output) const
 Computes the output from the input.
virtual void computeCostsFromOutputs (const Vec &input, const Vec &output, const Vec &target, Vec &costs) const
 Computes the costs from already computed output.
virtual TVec< std::string > getTestCostNames () const
 Returns the names of the costs computed by computeCostsFromOutpus (and thus the test method).
virtual TVec< std::string > getTrainCostNames () const
 Returns the names of the objective costs that the train method computes and for which it updates the VecStatsCollector train_stats.
virtual TVec< string > getOutputNames () const
 Returns a vector of length outputsize() containing the outputs' names.
virtual void setExperimentDirectory (const PPath &the_expdir)
 The experiment directory is the directory in which files related to this model are to be saved.
virtual string classname () const
virtual OptionListgetOptionList () const
virtual OptionMapgetOptionMap () const
virtual RemoteMethodMapgetRemoteMethodMap () const
virtual ChainedLearnersdeepCopy (CopiesMap &copies) const
virtual void build ()
 Finish building the object; just call inherited::build followed by build_()
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< PLearner > > learners
 ### declare public option fields (such as build options) here Start your comments with Doxygen-compatible comments such as //!

Static Public Attributes

static StaticInitializer _static_initializer_

Static Protected Member Functions

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

Protected Attributes

Vec tmp_input
Vec tmp_output
 Global storage to save memory allocations.

Private Types

typedef PLearner inherited

Private Member Functions

void build_ ()
 This does the actual building.

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 ChainedLearners.h.


Member Typedef Documentation

Reimplemented from PLearn::PLearner.

Definition at line 59 of file ChainedLearners.h.


Constructor & Destructor Documentation

PLearn::ChainedLearners::ChainedLearners ( )

Default constructor.

Definition at line 58 of file ChainedLearners.cc.

{}

Member Function Documentation

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

Reimplemented from PLearn::PLearner.

Definition at line 56 of file ChainedLearners.cc.

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

Reimplemented from PLearn::PLearner.

Definition at line 56 of file ChainedLearners.cc.

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

Reimplemented from PLearn::PLearner.

Definition at line 56 of file ChainedLearners.cc.

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

Reimplemented from PLearn::PLearner.

Definition at line 56 of file ChainedLearners.cc.

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

Reimplemented from PLearn::Object.

Definition at line 56 of file ChainedLearners.cc.

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

Reimplemented from PLearn::PLearner.

Definition at line 56 of file ChainedLearners.cc.

void PLearn::ChainedLearners::build ( ) [virtual]

Finish building the object; just call inherited::build followed by build_()

Reimplemented from PLearn::PLearner.

Definition at line 86 of file ChainedLearners.cc.

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

This does the actual building.

Reimplemented from PLearn::PLearner.

Definition at line 71 of file ChainedLearners.cc.

{
    // ### This method should do the real building of the object,
    // ### according to set 'options', in *any* situation.
    // ### Typical situations include:
    // ###  - Initial building of an object from a few user-specified options
    // ###  - Building of a "reloaded" object: i.e. from the complete set of
    // ###    all serialised options.
    // ###  - Updating or "re-building" of an object after a few "tuning"
    // ###    options have been modified.
    // ### You should assume that the parent class' build_() has already been
    // ### called.
}
string PLearn::ChainedLearners::classname ( ) const [virtual]

Reimplemented from PLearn::Object.

Definition at line 56 of file ChainedLearners.cc.

void PLearn::ChainedLearners::computeCostsFromOutputs ( const Vec input,
const Vec output,
const Vec target,
Vec costs 
) const [virtual]

Computes the costs from already computed output.

Implements PLearn::PLearner.

Definition at line 204 of file ChainedLearners.cc.

{
    // this is generally called after a computeOutput, so the last learner's input 
    // we used was stored in tmp_output
    return learners.lastElement()->computeCostsFromOutputs(tmp_output, output, target, costs);
}
void PLearn::ChainedLearners::computeOutput ( const Vec input,
Vec output 
) const [virtual]

Computes the output from the input.

Reimplemented from PLearn::PLearner.

Definition at line 182 of file ChainedLearners.cc.

References n.

{
    int nlearners = learners.length();
    if(nlearners==1)
        learners[0]->computeOutput(input, output);
    else
    {
        tmp_output.resize(learners[0]->outputsize());
        learners[0]->computeOutput(input, tmp_output);
        for(int k=1; k<nlearners-1; k++)
        {
            int n = tmp_output.length();
            tmp_input.resize(n);
            tmp_input << tmp_output;
            tmp_output->resize(learners[k]->outputsize());
            learners[k]->computeOutput(tmp_input, tmp_output);
        }
        learners[nlearners-1]->computeOutput(tmp_output, output);
        
    }
}
void PLearn::ChainedLearners::declareOptions ( OptionList ol) [static, protected]

Declares the class options.

Reimplemented from PLearn::PLearner.

Definition at line 61 of file ChainedLearners.cc.

References PLearn::OptionBase::buildoption, PLearn::declareOption(), and learners.

{
    declareOption(ol, "learners", &ChainedLearners::learners, 
                  OptionBase::buildoption,
                  "This is a list of learners to train in sequence.");

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

Here is the call graph for this function:

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

Reimplemented from PLearn::PLearner.

Definition at line 147 of file ChainedLearners.h.

:
    //#####  Protected Options  ###############################################
ChainedLearners * PLearn::ChainedLearners::deepCopy ( CopiesMap copies) const [virtual]

Reimplemented from PLearn::PLearner.

Definition at line 56 of file ChainedLearners.cc.

void PLearn::ChainedLearners::forget ( ) [virtual]

(Re-)initializes the PLearner in its fresh state (that state may depend on the 'seed' option) and sets 'stage' back to 0 (this is the stage of a fresh learner!).

(Re-)initialize the PLearner in its fresh state (that state may depend on the 'seed' option) and sets 'stage' back to 0 (this is the stage of a fresh learner!)

A typical forget() method should do the following:

  • call inherited::forget() to initialize its random number generator with the 'seed' option
  • initialize the learner's parameters, using this random generator
  • stage = 0

Reimplemented from PLearn::PLearner.

Definition at line 107 of file ChainedLearners.cc.

{

    for(int k=0; k<learners.length(); k++)
        learners[k]->forget();
    inherited::forget();
    stage = 0;
}
OptionList & PLearn::ChainedLearners::getOptionList ( ) const [virtual]

Reimplemented from PLearn::Object.

Definition at line 56 of file ChainedLearners.cc.

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

Reimplemented from PLearn::Object.

Definition at line 56 of file ChainedLearners.cc.

TVec< string > PLearn::ChainedLearners::getOutputNames ( ) const [virtual]

Returns a vector of length outputsize() containing the outputs' names.

Default version returns ["out0", "out1", ...] Don't forget name should not have space or it will cause trouble when they are saved in the file {metadatadir}/fieldnames

Reimplemented from PLearn::PLearner.

Definition at line 223 of file ChainedLearners.cc.

References PLearn::TVec< T >::lastElement().

{
    return learners.lastElement()->getOutputNames();
}

Here is the call graph for this function:

RemoteMethodMap & PLearn::ChainedLearners::getRemoteMethodMap ( ) const [virtual]

Reimplemented from PLearn::Object.

Definition at line 56 of file ChainedLearners.cc.

TVec< string > PLearn::ChainedLearners::getTestCostNames ( ) const [virtual]

Returns the names of the costs computed by computeCostsFromOutpus (and thus the test method).

Implements PLearn::PLearner.

Definition at line 212 of file ChainedLearners.cc.

{
    return learners.lastElement()->getTestCostNames();
}
TVec< string > PLearn::ChainedLearners::getTrainCostNames ( ) const [virtual]

Returns the names of the objective costs that the train method computes and for which it updates the VecStatsCollector train_stats.

Implements PLearn::PLearner.

Definition at line 217 of file ChainedLearners.cc.

{
    return learners.lastElement()->getTrainCostNames();
}
void PLearn::ChainedLearners::makeDeepCopyFromShallowCopy ( CopiesMap copies) [virtual]

Transforms a shallow copy into a deep copy.

Reimplemented from PLearn::PLearner.

Definition at line 93 of file ChainedLearners.cc.

References PLearn::deepCopyField().

Here is the call graph for this function:

int PLearn::ChainedLearners::outputsize ( ) const [virtual]

Returns the size of this learner's output, (which typically may depend on its inputsize(), targetsize() and set options).

Implements PLearn::PLearner.

Definition at line 102 of file ChainedLearners.cc.

{
    return learners.lastElement()->outputsize();
}
void PLearn::ChainedLearners::setExperimentDirectory ( const PPath the_expdir) [virtual]

The experiment directory is the directory in which files related to this model are to be saved.

If it is an empty string, it is understood to mean that the user doesn't want any file created by this learner.

Reimplemented from PLearn::PLearner.

Definition at line 229 of file ChainedLearners.cc.

References PLearn::PPath::isEmpty(), and PLearn::tostring().

{
    inherited::setExperimentDirectory(the_expdir);
    if (! the_expdir.isEmpty())
        for(int k= 0; k < learners.length(); ++k)
            learners[k]->setExperimentDirectory(the_expdir /
                                                ("SubLearner_"+tostring(k)));
}

Here is the call graph for this function:

void PLearn::ChainedLearners::setTrainingSet ( VMat  training_set,
bool  call_forget = true 
) [virtual]

Declares the training set.

Then calls build() and forget() if necessary. Also sets this learner's inputsize_ targetsize_ weightsize_ from those of the training_set. Note: You shouldn't have to override this in subclasses, except in maybe to forward the call to an underlying learner.

Reimplemented from PLearn::PLearner.

Definition at line 126 of file ChainedLearners.cc.

{
    inherited::setTrainingSet(training_set, call_forget);
    VMat dataset = getTrainingSet();
    int nlearners = learners.length();
    for(int k=0; k<nlearners; k++)
    {
        learners[k]->setTrainingSet(dataset, call_forget);
        if(k<nlearners-1)
            dataset = learners[k]->processDataSet(dataset);            
    }
}
void PLearn::ChainedLearners::train ( ) [virtual]

The role of the train method is to bring the learner up to stage==nstages, updating the train_stats collector with training costs measured on-line in the process.

Implements PLearn::PLearner.

Definition at line 139 of file ChainedLearners.cc.

{
    // The role of the train method is to bring the learner up to
    // stage==nstages, updating train_stats with training costs measured
    // on-line in the process.

    // This generic PLearner method does a number of standard stuff useful for
    // (almost) any learner, and return 'false' if no training should take
    // place. See PLearner.h for more details.
    if (!initTrain())
        return;

    VMat dataset = getTrainingSet();
    int nlearners = learners.length();
    if(stage>nstages)
        forget();

    if(stage==0)
    {
        for(int k=0; k<nlearners; k++)
        {
            learners[k]->setTrainingSet(dataset);
            if(k<nlearners-1)
            {
                learners[k]->train();
                dataset = learners[k]->processDataSet(dataset);            
            }
            else // last learner
            {
                learners[k]->setTrainStatsCollector(train_stats);
                train_stats->forget();
                learners[k]->train();                
                train_stats->finalize(); 
            }
        }
        ++stage;
    }
    else // stage already==1
    { // only call train on last learner, in case its own nstages has changed or something similar
        learners[nlearners-1]->train();
    }
}

Member Data Documentation

Reimplemented from PLearn::PLearner.

Definition at line 147 of file ChainedLearners.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 ChainedLearners.h.

Referenced by declareOptions().

Definition at line 165 of file ChainedLearners.h.

Global storage to save memory allocations.

Reimplemented from PLearn::PLearner.

Definition at line 166 of file ChainedLearners.h.


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