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

#include <SequentialLearner.h>

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

List of all members.

Public Types

typedef PLearner inherited

Public Member Functions

int get_last_train_t ()
 last value of test_set.length() for which testing was actually done
int get_last_call_train_t ()
int get_last_test_t ()
 SequentialLearner ()
 Constructor.
virtual void build ()
 simply calls inherited::build() then build_()
virtual int outputsize () const
 Default behaviour: return train_set->targetsize()
virtual int nTestCosts () const
 Caches getTestCostNames().size() in an internal variable the first time it is called, and then returns the content of this variable.
virtual void setTrainingSet (VMat training_set, bool call_forget=true)
 Declares the training set.
virtual void train ()=0
virtual void test (VMat testset, PP< VecStatsCollector > test_stats, VMat testoutputs=0, VMat testcosts=0) const =0
virtual void computeOutputAndCosts (const Vec &input, const Vec &target, Vec &output, Vec &costs) const
 Default calls computeOutput and computeCostsFromOutputs.
virtual void computeCostsOnly (const Vec &input, const Vec &target, Vec &costs) const
 Default calls computeOutputAndCosts.
virtual void computeOutput (const Vec &input, Vec &output) const
 *** SUBCLASS WRITING: ***
virtual void computeCostsFromOutputs (const Vec &input, const Vec &output, const Vec &target, Vec &costs) const
 *** SUBCLASS WRITING: ***
int sequenceStart () const
int sequenceLength () const
Vec getCostSequence (int cost_index) const
Vec getCostSequence (string cname) const
virtual void matlabSave (const string &matlab_subdir)
virtual void forget ()
 *** SUBCLASS WRITING: ***
virtual SequentialLearnerdeepCopy (CopiesMap &copies) const
virtual void makeDeepCopyFromShallowCopy (CopiesMap &copies)
 Transforms a shallow copy into a deep copy.

Static Public Member Functions

static string _classname_ ()
 Does the necessary operations to transform a shallow copy (this) into a deep copy by deep-copying all the members that need to be.
static OptionList_getOptionList_ ()
static RemoteMethodMap_getRemoteMethodMap_ ()
static bool _isa_ (const Object *o)
static void _static_initialize_ ()
static const PPathdeclaringFile ()

Public Attributes

int init_train_size
int max_seq_len
 max length of the VMat that train can contain = max de t ci-haut
int max_train_len
 max nb of (input,target) pairs actually used for training
int train_step
 how often we have to re-train a model, (default = 1 = after every time step)
int horizon
 by how much to offset the target columns wrt the input columns (default = 1)
int outputsize_
Mat predictions
 these two fields are used by other classes such as SequentialModelSelector and SequentialValidation and they are filled when the method test is called
Mat errors
 each element indexed by (time_index, output_index), there are (max_seq_len,outputsize) elements.

Static Public Attributes

static StaticInitializer _static_initializer_

Static Protected Member Functions

static void declareOptions (OptionList &ol)
 Declare this class' options.

Protected Attributes

int last_train_t
int last_call_train_t
 last value of train_set.length() for which training was actually done (<= last_call_train_t)
int last_test_t
 last value of train_set.length() for which train() was called (may not have done anything)

Private Member Functions

void build_ ()
 each element indexed by (time_index, cost_index), there are (max_seq_len,nCosts) elements.

Detailed Description

We use the following conventions throughout this module:

Definition at line 57 of file SequentialLearner.h.


Member Typedef Documentation

Reimplemented from PLearn::PLearner.

Reimplemented in PLearn::MovingAverage, and PLearn::SequentialModelSelector.

Definition at line 60 of file SequentialLearner.h.


Constructor & Destructor Documentation

PLearn::SequentialLearner::SequentialLearner ( )

Constructor.

Definition at line 46 of file SequentialLearner.cc.


Member Function Documentation

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

Does the necessary operations to transform a shallow copy (this) into a deep copy by deep-copying all the members that need to be.

Reimplemented from PLearn::PLearner.

Reimplemented in PLearn::EmbeddedSequentialLearner, PLearn::MovingAverage, and PLearn::SequentialModelSelector.

Definition at line 44 of file SequentialLearner.cc.

OptionList & PLearn::SequentialLearner::_getOptionList_ ( ) [static]
RemoteMethodMap & PLearn::SequentialLearner::_getRemoteMethodMap_ ( ) [static]
bool PLearn::SequentialLearner::_isa_ ( const Object o) [static]
StaticInitializer SequentialLearner::_static_initializer_ & PLearn::SequentialLearner::_static_initialize_ ( ) [static]
void PLearn::SequentialLearner::build ( ) [virtual]

simply calls inherited::build() then build_()

Reimplemented from PLearn::PLearner.

Reimplemented in PLearn::EmbeddedSequentialLearner, PLearn::MovingAverage, and PLearn::SequentialModelSelector.

Definition at line 78 of file SequentialLearner.cc.

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

Referenced by PLearn::SequentialModelSelector::build(), and PLearn::MovingAverage::build().

Here is the call graph for this function:

Here is the caller graph for this function:

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

each element indexed by (time_index, cost_index), there are (max_seq_len,nCosts) elements.

This does the actual building

Reimplemented from PLearn::PLearner.

Reimplemented in PLearn::EmbeddedSequentialLearner, PLearn::MovingAverage, and PLearn::SequentialModelSelector.

Definition at line 59 of file SequentialLearner.cc.

References errors, PLearn::TMat< T >::fill(), max_seq_len, MISSING_VALUE, nTestCosts(), outputsize(), PLERROR, predictions, and PLearn::TMat< T >::resize().

Referenced by build().

{
    if(max_seq_len != -1)
    {
        if( outputsize() == 0 )
            PLERROR("SequentialLearner::build_\n"
                    "outputsize() returns 0 but predictions will later be assumed to have nonzero width.");
        predictions.resize(max_seq_len, outputsize());
        predictions.fill(MISSING_VALUE);

        int n_test_costs = nTestCosts();
        if( n_test_costs > 0 )
        {
            errors.resize(max_seq_len, n_test_costs);
            errors.fill(MISSING_VALUE);
        }
    }  
}

Here is the call graph for this function:

Here is the caller graph for this function:

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

*** SUBCLASS WRITING: ***

This should be defined in subclasses to compute the weighted costs from already computed output. The costs should correspond to the cost names returned by getTestCostNames().

NOTE: In exotic cases, the cost may also depend on some info in the input, that's why the method also gets so see it.

Implements PLearn::PLearner.

Reimplemented in PLearn::MovingAverage, and PLearn::SequentialModelSelector.

Definition at line 154 of file SequentialLearner.cc.

References PLERROR.

{ PLERROR("The method computeCostsFromOutputs is not defined for this SequentialLearner"); }
void PLearn::SequentialLearner::computeCostsOnly ( const Vec input,
const Vec target,
Vec costs 
) const [virtual]

Default calls computeOutputAndCosts.

This may be overridden if there is a more efficient way to compute the costs directly, without computing the whole output vector.

Reimplemented from PLearn::PLearner.

Reimplemented in PLearn::SequentialModelSelector.

Definition at line 147 of file SequentialLearner.cc.

References PLERROR.

{ PLERROR("The method computeCostsOnly is not defined for this SequentialLearner"); }
void PLearn::SequentialLearner::computeOutput ( const Vec input,
Vec output 
) const [virtual]

*** SUBCLASS WRITING: ***

This should be defined in subclasses to compute the output from the input.

Reimplemented from PLearn::PLearner.

Reimplemented in PLearn::SequentialModelSelector.

Definition at line 151 of file SequentialLearner.cc.

References PLERROR.

{ PLERROR("The method computeOutput is not defined for this SequentialLearner"); }
void PLearn::SequentialLearner::computeOutputAndCosts ( const Vec input,
const Vec target,
Vec output,
Vec costs 
) const [virtual]

Default calls computeOutput and computeCostsFromOutputs.

You may override this if you have a more efficient way to compute both output and weighted costs at the same time.

Reimplemented from PLearn::PLearner.

Reimplemented in PLearn::SequentialModelSelector.

Definition at line 143 of file SequentialLearner.cc.

References PLERROR.

{ PLERROR("The method computeOutputAndCosts is not defined for this SequentialLearner"); }
void PLearn::SequentialLearner::declareOptions ( OptionList ol) [static, protected]

Declare this class' options.

Reimplemented from PLearn::PLearner.

Reimplemented in PLearn::EmbeddedSequentialLearner, PLearn::MovingAverage, and PLearn::SequentialModelSelector.

Definition at line 84 of file SequentialLearner.cc.

References PLearn::OptionBase::buildoption, PLearn::declareOption(), PLearn::PLearner::declareOptions(), horizon, init_train_size, max_seq_len, max_train_len, outputsize_, and train_step.

Referenced by PLearn::SequentialModelSelector::declareOptions(), and PLearn::MovingAverage::declareOptions().

{
    declareOption(ol, "init_train_size", &SequentialLearner::init_train_size, OptionBase::buildoption,
                  "Before the length of train_set reaches init_train_size, train doesn't do anything.\n"
                  "Default = 1.");

    declareOption(ol, "max_seq_len", &SequentialLearner::max_seq_len,
                  OptionBase::buildoption,
                  "Maximum length that the training matrix will ever reach;\n"
                  "this is used as an optimization to preallocate buffers\n"
                  "and avoid reallocations as training/testing proceeds.");

    declareOption(ol, "max_train_len", &SequentialLearner::max_train_len,
                  OptionBase::buildoption,
                  "Maximum number of (input,target) pairs used for training;\n"
                  "for longer training sequences, only the last max_train_len\n"
                  "pairs are actually used for training");

    declareOption(ol, "train_step", &SequentialLearner::train_step,
                  OptionBase::buildoption,
                  "How often we have to re-train a model;\n"
                  "value of 1 = after every time step");

    declareOption(ol, "horizon", &SequentialLearner::horizon,
                  OptionBase::buildoption,
                  "How much to offset the target columns with respect to\n"
                  "the input columns");

    declareOption(ol, "outputsize", &SequentialLearner::outputsize_,
                  OptionBase::buildoption,
                  "Size of the output vector (number of outputs)");

    inherited::declareOptions(ol);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static const PPath& PLearn::SequentialLearner::declaringFile ( ) [inline, static]
SequentialLearner * PLearn::SequentialLearner::deepCopy ( CopiesMap copies) const [virtual]
void PLearn::SequentialLearner::forget ( ) [virtual]

*** SUBCLASS WRITING: ***

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

A typical forget() method should do the following:

  • initialize the learner's parameters, using this random generator
  • stage = 0;

This method is typically called by the build_() method, after it has finished setting up the parameters, and if it deemed useful to set or reset the learner in its fresh state. (remember build may be called after modifying options that do not necessarily require the learner to restart from a fresh state...) forget is also called by the setTrainingSet method, after calling build(), so it will generally be called TWICE during setTrainingSet!

Reimplemented from PLearn::PLearner.

Reimplemented in PLearn::EmbeddedSequentialLearner, PLearn::MovingAverage, and PLearn::SequentialModelSelector.

Definition at line 125 of file SequentialLearner.cc.

References errors, PLearn::TMat< T >::fill(), PLearn::TMat< T >::isNotEmpty(), last_call_train_t, last_test_t, last_train_t, MISSING_VALUE, and predictions.

Referenced by PLearn::SequentialModelSelector::forget(), PLearn::MovingAverage::forget(), and setTrainingSet().

{
    if (predictions.isNotEmpty())
        predictions.fill(MISSING_VALUE);
    if (errors.isNotEmpty())
        errors.fill(MISSING_VALUE);
    last_train_t      = -1;
    last_call_train_t = -1;
    last_test_t       = -1;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int PLearn::SequentialLearner::get_last_call_train_t ( ) [inline]

Definition at line 69 of file SequentialLearner.h.

{ return last_call_train_t; }
int PLearn::SequentialLearner::get_last_test_t ( ) [inline]

Definition at line 70 of file SequentialLearner.h.

{ return last_test_t; }
int PLearn::SequentialLearner::get_last_train_t ( ) [inline]

last value of test_set.length() for which testing was actually done

Definition at line 68 of file SequentialLearner.h.

{ return last_train_t; }
Vec PLearn::SequentialLearner::getCostSequence ( int  cost_index) const [inline]

Definition at line 153 of file SequentialLearner.h.

Referenced by PLearn::SequentialModelSelector::matlabSave(), and matlabSave().

    { return errors.subMat(sequenceStart(), cost_index, sequenceLength(), 1).toVecCopy(); }

Here is the caller graph for this function:

Vec PLearn::SequentialLearner::getCostSequence ( string  cname) const [inline]

Definition at line 155 of file SequentialLearner.h.

References getCostSequence().

Referenced by getCostSequence().

    { return getCostSequence(getTestCostIndex(cname)); }

Here is the call graph for this function:

Here is the caller graph for this function:

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

Transforms a shallow copy into a deep copy.

Reimplemented from PLearn::PLearner.

Reimplemented in PLearn::EmbeddedSequentialLearner, and PLearn::SequentialModelSelector.

Definition at line 52 of file SequentialLearner.cc.

References PLearn::deepCopyField(), errors, PLearn::PLearner::makeDeepCopyFromShallowCopy(), and predictions.

Referenced by PLearn::SequentialModelSelector::makeDeepCopyFromShallowCopy().

Here is the call graph for this function:

Here is the caller graph for this function:

void PLearn::SequentialLearner::matlabSave ( const string &  matlab_subdir) [virtual]

Any SequentialLearner will have the possiblity to save some data in a matlab 'readable' format. The data will be saved in expdir/matlab_subdir through the global matlabSave function (MatIO.h).

Reimplemented in PLearn::SequentialModelSelector.

Definition at line 158 of file SequentialLearner.cc.

References PLearn::add(), PLearn::append_slash(), g, getCostSequence(), PLearn::PLearner::getExperimentDirectory(), PLearn::PLearner::getTestCostNames(), PLearn::TVec< T >::length(), PLearn::matlabSave(), predictions, and sequenceStart().

{
    inherited::matlabSave(matlab_subdir);

    string save_dir = append_slash(getExperimentDirectory()) + matlab_subdir;
    Vec dummy, add(1); add[0] = 0;

    TVec<string> cost_names = getTestCostNames();  
    Vec startX(1, real(sequenceStart()));
    for(int g=0; g < cost_names.length(); g++)
        PLearn::matlabSave(save_dir, cost_names[g], 
                           startX,
                           getCostSequence(g), add, dummy);       

    PLearn::matlabSave(save_dir, "Predictions", predictions, dummy, dummy);
}

Here is the call graph for this function:

int PLearn::SequentialLearner::nTestCosts ( ) const [virtual]

Caches getTestCostNames().size() in an internal variable the first time it is called, and then returns the content of this variable.

Reimplemented from PLearn::PLearner.

Definition at line 140 of file SequentialLearner.cc.

References PLearn::PLearner::getTestCostNames(), and PLearn::TVec< T >::size().

Referenced by build_().

{ return getTestCostNames().size(); }

Here is the call graph for this function:

Here is the caller graph for this function:

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

Default behaviour: return train_set->targetsize()

Returns train_set->targetsize()

Implements PLearn::PLearner.

Definition at line 137 of file SequentialLearner.cc.

References outputsize_.

Referenced by build_(), PLearn::MovingAverage::test(), and PLearn::MovingAverage::train().

{ return outputsize_; }

Here is the caller graph for this function:

int PLearn::SequentialLearner::sequenceLength ( ) const [inline]

Definition at line 152 of file SequentialLearner.h.

{ return last_test_t - init_train_size + 1; }
int PLearn::SequentialLearner::sequenceStart ( ) const [inline]

The getCostSequence method returns the sequence of the cost at index 'cost_index' or named 'cname' from sequenceStart() with a length of sequenceLength().

Definition at line 151 of file SequentialLearner.h.

Referenced by matlabSave().

{ return init_train_size; }

Here is the caller graph for this function:

void PLearn::SequentialLearner::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 119 of file SequentialLearner.cc.

References forget(), and PLearn::PLearner::train_set.

{
    train_set = training_set;
    if (call_forget) forget();
}

Here is the call graph for this function:

virtual void PLearn::SequentialLearner::test ( VMat  testset,
PP< VecStatsCollector test_stats,
VMat  testoutputs = 0,
VMat  testcosts = 0 
) const [pure virtual]

*** SUBCLASS WRITING: *** The method should:

  • call computeOutputAndCosts on the test set
  • save the outputs and the costs in the predictions & errors matrices, beginning at position last_call_train_t

Reimplemented from PLearn::PLearner.

Implemented in PLearn::EmbeddedSequentialLearner, PLearn::MovingAverage, and PLearn::SequentialModelSelector.

virtual void PLearn::SequentialLearner::train ( ) [pure virtual]

*** SUBCLASS WRITING: *** Does the actual training. Subclasses must implement this method. The method should upon entry, call setTrainingSet(training_set); The method should:

  • do nothing if we already called it with this value of train.length or a value between [last_train_t-train_step+1,last_train_t]
  • if not, train and update the value of last_train_t
  • in either case, update the value of last_call_train_t

Implements PLearn::PLearner.

Implemented in PLearn::EmbeddedSequentialLearner, PLearn::MovingAverage, and PLearn::SequentialModelSelector.


Member Data Documentation

each element indexed by (time_index, output_index), there are (max_seq_len,outputsize) elements.

initial values may be 'missing value'

Definition at line 89 of file SequentialLearner.h.

Referenced by build_(), forget(), makeDeepCopyFromShallowCopy(), PLearn::SequentialModelSelector::paired_t_test(), PLearn::MovingAverage::test(), PLearn::SequentialModelSelector::test(), PLearn::SequentialModelSelector::train(), and PLearn::MovingAverage::train().

by how much to offset the target columns wrt the input columns (default = 1)

Definition at line 82 of file SequentialLearner.h.

Referenced by PLearn::SequentialModelSelector::build_(), declareOptions(), PLearn::MovingAverage::test(), PLearn::SequentialModelSelector::train(), and PLearn::MovingAverage::train().

Before the length of train_set reaches init_train_size, train doesn't do anything. Default: 1.

Definition at line 78 of file SequentialLearner.h.

Referenced by PLearn::SequentialModelSelector::build_(), declareOptions(), and PLearn::SequentialModelSelector::train().

last value of train_set.length() for which training was actually done (<= last_call_train_t)

Definition at line 64 of file SequentialLearner.h.

Referenced by forget(), and PLearn::SequentialModelSelector::train().

last value of train_set.length() for which train() was called (may not have done anything)

Definition at line 65 of file SequentialLearner.h.

Referenced by forget(), PLearn::MovingAverage::test(), PLearn::SequentialModelSelector::test(), and PLearn::SequentialModelSelector::train().

max length of the VMat that train can contain = max de t ci-haut

Definition at line 79 of file SequentialLearner.h.

Referenced by PLearn::SequentialModelSelector::build_(), build_(), declareOptions(), PLearn::SequentialModelSelector::forget(), and PLearn::SequentialModelSelector::test().

max nb of (input,target) pairs actually used for training

Definition at line 80 of file SequentialLearner.h.

Referenced by PLearn::MovingAverage::build_(), and declareOptions().

Definition at line 83 of file SequentialLearner.h.

Referenced by declareOptions(), and outputsize().

these two fields are used by other classes such as SequentialModelSelector and SequentialValidation and they are filled when the method test is called

Definition at line 87 of file SequentialLearner.h.

Referenced by build_(), forget(), makeDeepCopyFromShallowCopy(), matlabSave(), PLearn::MovingAverage::test(), PLearn::SequentialModelSelector::test(), PLearn::SequentialModelSelector::train(), and PLearn::MovingAverage::train().

how often we have to re-train a model, (default = 1 = after every time step)

Definition at line 81 of file SequentialLearner.h.

Referenced by declareOptions(), and PLearn::SequentialModelSelector::train().


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