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

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

#include <TargetEncodingLearner.h>

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

List of all members.

Public Member Functions

 TargetEncodingLearner ()
 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 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
 outputnames are the same as the inputnames
virtual string classname () const
virtual OptionListgetOptionList () const
virtual OptionMapgetOptionMap () const
virtual RemoteMethodMapgetRemoteMethodMap () const
virtual TargetEncodingLearnerdeepCopy (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.
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 void setTrainingSet (VMat training_set, bool call_forget=true)
 Declares the training set.

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 max_nvals_for_encoding
 ### declare public option fields (such as build options) here Start your comments with Doxygen-compatible comments such as //!
PP< PLearnersublearner
TVec< map< real, real > > encodings
TVec< boolencode_col
TVec< realdefaults
real mean
bool encodings_learnt

Static Public Attributes

static StaticInitializer _static_initializer_

Protected Member Functions

void buildEncodingsFromTrainset ()

Static Protected Member Functions

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

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


Member Typedef Documentation

Reimplemented from PLearn::PLearner.

Definition at line 59 of file TargetEncodingLearner.h.


Constructor & Destructor Documentation

PLearn::TargetEncodingLearner::TargetEncodingLearner ( )

Default constructor.

Definition at line 50 of file TargetEncodingLearner.cc.


Member Function Documentation

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

Reimplemented from PLearn::PLearner.

Definition at line 48 of file TargetEncodingLearner.cc.

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

Reimplemented from PLearn::PLearner.

Definition at line 48 of file TargetEncodingLearner.cc.

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

Reimplemented from PLearn::PLearner.

Definition at line 48 of file TargetEncodingLearner.cc.

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

Reimplemented from PLearn::PLearner.

Definition at line 48 of file TargetEncodingLearner.cc.

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

Reimplemented from PLearn::Object.

Definition at line 48 of file TargetEncodingLearner.cc.

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

Reimplemented from PLearn::PLearner.

Definition at line 48 of file TargetEncodingLearner.cc.

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

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

Reimplemented from PLearn::PLearner.

Definition at line 106 of file TargetEncodingLearner.cc.

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

Here is the call graph for this function:

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

This does the actual building.

Reimplemented from PLearn::PLearner.

Definition at line 90 of file TargetEncodingLearner.cc.

References encodings_learnt.

Referenced by build().

{
    // ### 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.
  encodings_learnt= false;
}

Here is the caller graph for this function:

void PLearn::TargetEncodingLearner::buildEncodingsFromTrainset ( ) [protected]

Definition at line 166 of file TargetEncodingLearner.cc.

References PLearn::TVec< T >::clear(), defaults, encode_col, encodings, encodings_learnt, PLearn::TVec< T >::end(), PLearn::TVec< T >::fill(), PLearn::TVec< T >::find(), PLearn::VMat::getExample(), i, PLearn::TVec< T >::insert(), j, PLearn::TVec< T >::length(), PLearn::VMat::length(), max_nvals_for_encoding, mean, n, PLERROR, PLearn::PLearner::report_progress, PLearn::TVec< T >::resize(), PLearn::sum(), and PLearn::PLearner::train_set.

Referenced by train().

{
  int l = train_set->length();
  int n = train_set->inputsize();
  Vec input;
  Vec target;
  real weight;

  encodings.resize(n);

  TVec<map<real, pair<real, real> > > stats(n);
  
  encode_col.resize(n);
  encode_col.fill(true);
  
  PP<ProgressBar> pb;
  if(report_progress)
    pb = new ProgressBar("TargetEncodingLearner computing statistics ",l);

  real tot_weight= 0.;
  real tot_wt_targ= 0.;

  for(int i= 0; i < l; ++i)
    {
      train_set->getExample(i, input, target, weight);
      if(target.length() != 1)
        PLERROR("TargetEncodingLearner supports only one target");
      tot_weight+= weight;
      tot_wt_targ+= weight*target[0];
      for(int j= 0; j < n; ++j)
        {
          if(encode_col[j])
            {
              real val= input[j];

              map<real, pair<real, real> >::iterator it= stats[j].find(val);
              
              if(it == stats[j].end())
                stats[j].insert(make_pair(val, make_pair(weight, weight*target[0])));
              else
                {
                  it->second.first+= weight;
                  it->second.second+= weight*target[0];
                }
              if(static_cast<int>(stats[j].size()) > max_nvals_for_encoding)
                {
                  encode_col[j]= false;
                  stats[j].clear();
                }
            }
        }

      if(pb)
        pb->update(i);
    }

  mean= tot_wt_targ / tot_weight;
  defaults.resize(n);
  defaults.fill(mean);

  for(int i= 0; i < n; ++i)
    {
      encodings[i].clear();
      for(map<real, pair<real, real> >::iterator it= stats[i].begin(); it != stats[i].end(); ++it)
        {
          real count= it->second.first;
          real sum= it->second.second;
          real avg= sum/count;
          encodings[i].insert(make_pair(it->first, avg));
        }
    }

  encodings_learnt= true;
}

Here is the call graph for this function:

Here is the caller graph for this function:

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

Reimplemented from PLearn::Object.

Definition at line 48 of file TargetEncodingLearner.cc.

void PLearn::TargetEncodingLearner::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 251 of file TargetEncodingLearner.cc.

References encodings_learnt, PLERROR, and sublearner.

{
  if(!encodings_learnt)
    PLERROR("TargetEncodingLearner::computeCostsFromOutputs encodings not learnt");
  //EncodedVMatrix::encodeRow(encodings, defaults, encode_col, input);
  sublearner->computeCostsFromOutputs(input, output, target, costs);
}
void PLearn::TargetEncodingLearner::computeOutput ( const Vec input,
Vec output 
) const [virtual]

Computes the output from the input.

Reimplemented from PLearn::PLearner.

Definition at line 243 of file TargetEncodingLearner.cc.

References defaults, encode_col, PLearn::EncodedVMatrix::encodeRow(), encodings, encodings_learnt, PLERROR, and sublearner.

{
  if(!encodings_learnt)
    PLERROR("TargetEncodingLearner::computeOutput encodings not learnt");
  EncodedVMatrix::encodeRow(encodings, defaults, encode_col, input);
  sublearner->computeOutput(input, output);
}

Here is the call graph for this function:

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

Declares the class options.

Reimplemented from PLearn::PLearner.

Definition at line 60 of file TargetEncodingLearner.cc.

References PLearn::OptionBase::buildoption, PLearn::declareOption(), PLearn::PLearner::declareOptions(), encode_col, encodings, encodings_learnt, PLearn::OptionBase::learntoption, max_nvals_for_encoding, mean, and sublearner.

{
    declareOption(ol, "max_nvals_for_encoding", &TargetEncodingLearner::max_nvals_for_encoding,
                  OptionBase::buildoption,
                  "Max number of different values for a field to be encoded.\n");

    declareOption(ol, "sublearner", &TargetEncodingLearner::sublearner,
                  OptionBase::buildoption,
                  "Learner to be trained/tested on the encoded dataset.\n");

    declareOption(ol, "encodings", &TargetEncodingLearner::encodings,
                  OptionBase::learntoption,
                  "Learnt encodings for the trainset.\n");

    declareOption(ol, "encode_col", &TargetEncodingLearner::encode_col,
                  OptionBase::learntoption,
                  "Wether each col. should be encoded.\n");

    declareOption(ol, "mean", &TargetEncodingLearner::mean,
                  OptionBase::learntoption,
                  "Weighted mean all targets.\n");

    declareOption(ol, "encodings_learnt", &TargetEncodingLearner::encodings_learnt,
                  OptionBase::learntoption,
                  "Wether encodings have already been learnt.\n");

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

Here is the call graph for this function:

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

Reimplemented from PLearn::PLearner.

Definition at line 152 of file TargetEncodingLearner.h.

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

Reimplemented from PLearn::PLearner.

Definition at line 48 of file TargetEncodingLearner.cc.

void PLearn::TargetEncodingLearner::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!).

Reimplemented from PLearn::PLearner.

Definition at line 140 of file TargetEncodingLearner.cc.

References encodings_learnt, PLearn::PLearner::forget(), and PLearn::PLearner::stage.

Here is the call graph for this function:

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

Reimplemented from PLearn::Object.

Definition at line 48 of file TargetEncodingLearner.cc.

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

Reimplemented from PLearn::Object.

Definition at line 48 of file TargetEncodingLearner.cc.

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

outputnames are the same as the inputnames

Reimplemented from PLearn::PLearner.

Definition at line 270 of file TargetEncodingLearner.cc.

References sublearner.

{
    return sublearner->getOutputNames();
}
RemoteMethodMap & PLearn::TargetEncodingLearner::getRemoteMethodMap ( ) const [virtual]

Reimplemented from PLearn::Object.

Definition at line 48 of file TargetEncodingLearner.cc.

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

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

Implements PLearn::PLearner.

Definition at line 260 of file TargetEncodingLearner.cc.

References sublearner.

{
    return sublearner->getTestCostNames();
}
TVec< string > PLearn::TargetEncodingLearner::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 265 of file TargetEncodingLearner.cc.

References sublearner.

{
    return sublearner->getTrainCostNames();
}
void PLearn::TargetEncodingLearner::makeDeepCopyFromShallowCopy ( CopiesMap copies) [virtual]

Transforms a shallow copy into a deep copy.

Reimplemented from PLearn::PLearner.

Definition at line 113 of file TargetEncodingLearner.cc.

References PLearn::PLearner::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("TargetEncodingLearner::makeDeepCopyFromShallowCopy not fully (correctly) implemented yet!");
}

Here is the call graph for this function:

int PLearn::TargetEncodingLearner::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 135 of file TargetEncodingLearner.cc.

References sublearner.

{
    return sublearner->outputsize();
}
void PLearn::TargetEncodingLearner::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 128 of file TargetEncodingLearner.cc.

References PLearn::PLearner::setExperimentDirectory(), and sublearner.

{
  inherited::setExperimentDirectory(the_expdir);
  sublearner->setExperimentDirectory(the_expdir / "SubLearner");
}

Here is the call graph for this function:

void PLearn::TargetEncodingLearner::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 275 of file TargetEncodingLearner.cc.

References PLearn::PLearner::setTrainingSet(), and sublearner.

{
    // temporarly set sublearner's train set (reset in train())
    if(sublearner)
        sublearner->setTrainingSet(training_set, call_forget);
    inherited::setTrainingSet(training_set, call_forget);
}

Here is the call graph for this function:

void PLearn::TargetEncodingLearner::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 147 of file TargetEncodingLearner.cc.

References buildEncodingsFromTrainset(), defaults, encode_col, encodings, PLearn::PLearner::getExperimentDirectory(), PLearn::PLearner::getTrainingSet(), PLearn::PLearner::initTrain(), PLearn::PLearner::stage, and sublearner.

{
    if (!initTrain())
        return;

    if(stage<1)
    {

      buildEncodingsFromTrainset();
      VMat vm= new EncodedVMatrix(getTrainingSet(), encodings, defaults, encode_col);
      vm->setMetaDataDir(getExperimentDirectory() / "EncodedVMatrix.metadata");
      sublearner->setTrainingSet(vm);
      ++stage;
    }

    sublearner->train();

}

Here is the call graph for this function:


Member Data Documentation

Reimplemented from PLearn::PLearner.

Definition at line 152 of file TargetEncodingLearner.h.

Definition at line 75 of file TargetEncodingLearner.h.

Referenced by buildEncodingsFromTrainset(), computeOutput(), and train().

### declare public option fields (such as build options) here Start your comments with Doxygen-compatible comments such as //!

Definition at line 68 of file TargetEncodingLearner.h.

Referenced by buildEncodingsFromTrainset(), and declareOptions().

Definition at line 76 of file TargetEncodingLearner.h.

Referenced by buildEncodingsFromTrainset(), and declareOptions().


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