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

Learner that trains several sub-learners on 'bags'. More...

#include <BaggingLearner.h>

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

List of all members.

Public Member Functions

 BaggingLearner (PP< Splitter > splitter_=0, PP< PLearner > template_learner_=0, TVec< string > stats_=TVec< string >(1,"E"), int exclude_extremes_=0, bool output_sub_outputs_=false)
 Default constructor.
virtual int outputsize () const
 SUBCLASS WRITING: override this so that it returns the size of this learner's output, as a function of its inputsize(), targetsize() and set options.
virtual void forget ()
 *** SUBCLASS WRITING: ***
virtual void train ()
 *** SUBCLASS WRITING: ***
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: ***
virtual TVec< std::string > getTestCostNames () const
 *** SUBCLASS WRITING: ***
virtual TVec< std::string > getTrainCostNames () const
 *** SUBCLASS WRITING: ***
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 int nTrainCosts () const
 Caches getTrainCostNames().size() in an internal variable the first time it is called, and then returns the content of this variable.
virtual void resetInternalState ()
 If any, reset the internal state Default: do nothing.
virtual bool isStatefulLearner () const
 Does this PLearner has an internal state? Default: false.
virtual void setTrainingSet (VMat training_set, bool call_forget=true)
 Declares the training set.
virtual TVec< string > getOutputNames () const
 Returns a vector of length outputsize() containing the outputs' names.
virtual void setTrainStatsCollector (PP< VecStatsCollector > statscol)
 Sets the statistics collector whose update() method will be called during training.
virtual string classname () const
virtual OptionListgetOptionList () const
virtual OptionMapgetOptionMap () const
virtual RemoteMethodMapgetRemoteMethodMap () const
virtual BaggingLearnerdeepCopy (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.

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

PP< Splittersplitter
 splitter used to get bags(=splits)
PP< PLearnertemplate_learner
 to deep-copy once for each bag
TVec< string > stats
 functions used to combine outputs from all learners
int exclude_extremes
 for computeOutput: remove the highest and lowest outputs before averaging (nb.
bool output_sub_outputs
 Wether computeOutput should append sub-learners outputs to output.

Static Public Attributes

static StaticInitializer _static_initializer_

Protected Member Functions

TVec< string > addStatNames (const TVec< string > &names) const

Static Protected Member Functions

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

Protected Attributes

TVec< PP< PLearner > > learners
VecStatsCollector stcol
Mat learners_outputs
Mat outputs
Vec learner_costs
Vec last_test_input

Private Types

typedef PLearner inherited

Private Member Functions

void build_ ()
 **** SUBCLASS WRITING: ****

Detailed Description

Learner that trains several sub-learners on 'bags'.

Definition at line 51 of file BaggingLearner.h.


Member Typedef Documentation

Reimplemented from PLearn::PLearner.

Definition at line 53 of file BaggingLearner.h.


Constructor & Destructor Documentation

PLearn::BaggingLearner::BaggingLearner ( PP< Splitter splitter_ = 0,
PP< PLearner template_learner_ = 0,
TVec< string >  stats_ = TVec<string>(1,"E"),
int  exclude_extremes_ = 0,
bool  output_sub_outputs_ = false 
)

Default constructor.

Definition at line 60 of file BaggingLearner.cc.

    :splitter(splitter_),
     template_learner(template_learner_),
     stats(stats_),
     exclude_extremes(exclude_extremes_),
     output_sub_outputs(output_sub_outputs_)
{
}

Member Function Documentation

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

Reimplemented from PLearn::PLearner.

Definition at line 58 of file BaggingLearner.cc.

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

Reimplemented from PLearn::PLearner.

Definition at line 58 of file BaggingLearner.cc.

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

Reimplemented from PLearn::PLearner.

Definition at line 58 of file BaggingLearner.cc.

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

Reimplemented from PLearn::PLearner.

Definition at line 58 of file BaggingLearner.cc.

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

Reimplemented from PLearn::Object.

Definition at line 58 of file BaggingLearner.cc.

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

Reimplemented from PLearn::PLearner.

Definition at line 58 of file BaggingLearner.cc.

TVec<string> PLearn::BaggingLearner::addStatNames ( const TVec< string > &  names) const [inline, protected]

Definition at line 140 of file BaggingLearner.h.

References PLearn::TVec< T >::begin(), PLearn::TVec< T >::end(), PLearn::TVec< T >::push_back(), and stats.

Referenced by getOutputNames().

    {
        TVec<string> outputnames;
        for(TVec<string>::iterator it= names.begin(); it != names.end(); ++it)
            for(TVec<string>::const_iterator jt= stats.begin();
                jt != stats.end(); ++jt)
                outputnames.push_back(*jt + '[' + *it + ']');
        return outputnames;
    }

Here is the call graph for this function:

Here is the caller graph for this function:

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

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

Reimplemented from PLearn::PLearner.

Definition at line 118 of file BaggingLearner.cc.

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

Here is the call graph for this function:

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

**** SUBCLASS WRITING: ****

This method should finish 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 (such as hyper-parameters) have been modified.

You can assume that the parent class' build_() has already been called.

A typical build method will want to know the inputsize(), targetsize() and outputsize(), and may also want to check whether train_set->hasWeights(). All these methods require a train_set to be set, so the first thing you may want to do, is check if(train_set), before doing any heavy building...

Note: build() is always called by setTrainingSet.

Reimplemented from PLearn::PLearner.

Definition at line 112 of file BaggingLearner.cc.

Referenced by build().

{}

Here is the caller graph for this function:

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

Reimplemented from PLearn::Object.

Definition at line 58 of file BaggingLearner.cc.

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

Definition at line 347 of file BaggingLearner.cc.

References i, PLearn::TVec< T >::isEqual(), j, last_test_input, learners, learners_outputs, PLearn::TVec< T >::length(), nTestCosts(), PLERROR, PLearn::TVec< T >::resize(), and PLearn::TVec< T >::size().

{
    //if(input != last_test_input)
    if (!input.isEqual(last_test_input, true))
        PLERROR("BaggingLearner::computeCostsFromOutputs has to be called "
                "right after computeOutput, with the same input.");
    
    int nlearners= learners.size();
    costs.resize(nTestCosts());
    int k= 0;
    for(int i= 0; i < nlearners; ++i)
    {
        Vec subcosts;
        learners[i]->computeCostsFromOutputs(input, learners_outputs(i),
                                             target, subcosts);
        for(int j= 0; j < subcosts.length(); ++j)
            costs[k++]= subcosts[j];
    }

}

Here is the call graph for this function:

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

Definition at line 291 of file BaggingLearner.cc.

References PLearn::TVec< T >::begin(), PLearn::TMat< T >::column(), PLearn::TVec< T >::end(), exclude_extremes, PLearn::VecStatsCollector::forget(), PLearn::StatsCollector::getStat(), PLearn::VecStatsCollector::getStats(), i, j, last_test_input, learners, learners_outputs, PLearn::TMat< T >::length(), output_sub_outputs, outputs, outputsize(), PLASSERT, PLERROR, PLearn::TMat< T >::resize(), PLearn::TVec< T >::resize(), PLearn::VecStatsCollector::size(), PLearn::TVec< T >::size(), PLearn::sortElements(), stats, stcol, PLearn::TMat< T >::subMatRows(), template_learner, PLearn::TMat< T >::toVec(), and PLearn::VecStatsCollector::update().

{
    int nout = outputsize();
    output.resize(nout);
    int nlearners= learners.size();
    PLASSERT(template_learner);
    int sub_nout = template_learner->outputsize();
    learners_outputs.resize(nlearners, sub_nout);

    last_test_input.resize(input.size());
    last_test_input << input;//save it, to test in computeCostsFromOutputs

    for(int i= 0; i < nlearners; ++i)
    {
        Vec outp= learners_outputs(i);
        learners[i]->computeOutput(input, outp);
    }

    if(exclude_extremes > 0)
    {
        outputs.resize(nlearners, sub_nout);
        outputs << learners_outputs;
        //exclude highest and lowest n predictions for each output
        int nexcl= 2*exclude_extremes;
        if(nlearners <= nexcl)
            PLERROR("BaggingLearner::computeOutput : Cannot exclude all outputs! "
                    "nlearners=%d, exclude_extremes=%d",nlearners,exclude_extremes);
        // sort all in place, one output at a time
        for(int j= 0; j < sub_nout; ++j)
            sortElements(outputs.column(j).toVec());
        // exclude from both ends
        outputs= outputs.subMatRows(exclude_extremes, outputs.length()-nexcl);
        nlearners-= nexcl;
    }
    else 
        outputs= learners_outputs;

    stcol.forget();
    for(int i= 0; i < outputs.length(); ++i)
        stcol.update(outputs(i));
    
    int i= 0;
    for(int j= 0; j < stcol.size(); ++j)
        for(TVec<string>::iterator it= stats.begin();
            it != stats.end(); ++it)
            output[i++]= stcol.getStats(j).getStat(*it);

    if(output_sub_outputs)
        for(int j= 0; j < nlearners; ++j)
            for(int k= 0; k < sub_nout; ++k)
                output[i++]= learners_outputs(j,k);
}

Here is the call graph for this function:

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

Declares this class' options.

Reimplemented from PLearn::PLearner.

Definition at line 73 of file BaggingLearner.cc.

References PLearn::OptionBase::basic_level, PLearn::OptionBase::buildoption, PLearn::declareOption(), PLearn::PLearner::declareOptions(), exclude_extremes, learners, PLearn::OptionBase::learntoption, output_sub_outputs, splitter, stats, and template_learner.

{
    declareOption(ol, "splitter", &BaggingLearner::splitter,
        OptionBase::buildoption,
        "Splitter used to get bags. In each split, only the first set is\n"
        "used (as the training set for a bag). A typical splitter used in\n"
        "bagging is a BootstrapSplitter.", OptionBase::basic_level);

    declareOption(ol, "template_learner", &BaggingLearner::template_learner,
                  OptionBase::buildoption,
                  "Template for all sub-learners; deep-copied once for each bag.",
                  OptionBase::basic_level);

    declareOption(ol, "stats", &BaggingLearner::stats,
        OptionBase::buildoption,
        "Statistics used to combine outputs from all learners. You can use\n"
        "any statistic that can be computed by a StatsCollector.",
        OptionBase::basic_level);

    declareOption(ol, "exclude_extremes", &BaggingLearner::exclude_extremes,
                  OptionBase::buildoption,
                  "If >0, sub-learners outputs are sorted and the exclude_extremes "
                  "highest and lowest are excluded.");
                  
    declareOption(ol, "output_sub_outputs", &BaggingLearner::output_sub_outputs,
                  OptionBase::buildoption,
                  "Wether computeOutput should append sub-learners outputs to output.");
                  
    declareOption(ol, "learners", &BaggingLearner::learners,
                  OptionBase::learntoption,
                  "Trained sub-learners.");

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

Here is the call graph for this function:

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

Reimplemented from PLearn::PLearner.

Definition at line 106 of file BaggingLearner.h.

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

Reimplemented from PLearn::PLearner.

Definition at line 58 of file BaggingLearner.cc.

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

Definition at line 157 of file BaggingLearner.cc.

References PLearn::PLearner::forget(), i, learners, and PLearn::TVec< T >::length().

{
    for(int i= 0; i < learners.length(); ++i)
        learners[i]->forget();
    inherited::forget();
}

Here is the call graph for this function:

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

Reimplemented from PLearn::Object.

Definition at line 58 of file BaggingLearner.cc.

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

Reimplemented from PLearn::Object.

Definition at line 58 of file BaggingLearner.cc.

TVec< string > PLearn::BaggingLearner::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 446 of file BaggingLearner.cc.

References addStatNames(), PLearn::TVec< T >::append(), i, j, PLearn::TVec< T >::length(), output_sub_outputs, PLASSERT, splitter, template_learner, and PLearn::tostring().

{
    PLASSERT(template_learner);
    PLASSERT(splitter);
    TVec<string> suboutputnames= template_learner->getOutputNames();
    TVec<string> outputnames= addStatNames(suboutputnames);
    if(output_sub_outputs)
    {
        int nbags= splitter->nsplits();
        int nsout= suboutputnames.length();
        for(int i= 0; i < nbags; ++i)
            for(int j= 0; j < nsout; ++j)
                outputnames.append(string("learner")+tostring(i)+"."+suboutputnames[j]);
    }
    return outputnames;
}

Here is the call graph for this function:

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

Reimplemented from PLearn::Object.

Definition at line 58 of file BaggingLearner.cc.

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

*** SUBCLASS WRITING: ***

This should return the names of the costs computed by computeCostsFromOutputs.

Implements PLearn::PLearner.

Definition at line 372 of file BaggingLearner.cc.

References i, j, PLearn::TVec< T >::length(), nTestCosts(), PLASSERT, splitter, template_learner, and PLearn::tostring().

{
    PLASSERT(splitter);
    PLASSERT(template_learner);
    int nbags= splitter->nsplits();
    TVec<string> subcosts= template_learner->getTestCostNames();
    TVec<string> costnames(nTestCosts());
    int nsubcosts= subcosts.length();
    int k= 0;
    for(int i= 0; i < nbags; ++i)
        for(int j= 0; j < nsubcosts; ++j)
            costnames[k++]= string("learner")+tostring(i)+"."+subcosts[j];
    return costnames;
}

Here is the call graph for this function:

TVec< string > PLearn::BaggingLearner::getTrainCostNames ( ) const [virtual]

*** SUBCLASS WRITING: ***

This should return 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 390 of file BaggingLearner.cc.

{
    return TVec<string>(); // for now
}
bool PLearn::BaggingLearner::isStatefulLearner ( ) const [virtual]

Does this PLearner has an internal state? Default: false.

Reimplemented from PLearn::PLearner.

Definition at line 425 of file BaggingLearner.cc.

References PLASSERT, and template_learner.

{
    PLASSERT(template_learner);
    return template_learner->isStatefulLearner();
}
void PLearn::BaggingLearner::makeDeepCopyFromShallowCopy ( CopiesMap copies) [virtual]

Transforms a shallow copy into a deep copy.

Reimplemented from PLearn::PLearner.

Definition at line 127 of file BaggingLearner.cc.

References PLearn::deepCopyField(), last_test_input, learner_costs, learners, learners_outputs, PLearn::PLearner::makeDeepCopyFromShallowCopy(), outputs, splitter, stats, and template_learner.

Here is the call graph for this function:

int PLearn::BaggingLearner::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 398 of file BaggingLearner.cc.

References PLASSERT, splitter, and template_learner.

Referenced by computeCostsFromOutputs(), and getTestCostNames().

{
    PLASSERT(splitter);
    PLASSERT(template_learner);
    return splitter->nsplits()*template_learner->nTestCosts();
}

Here is the caller graph for this function:

int PLearn::BaggingLearner::nTrainCosts ( ) const [virtual]

Caches getTrainCostNames().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 408 of file BaggingLearner.cc.

{
    return 0;
}
int PLearn::BaggingLearner::outputsize ( ) const [virtual]

SUBCLASS WRITING: override this so that it returns the size of this learner's output, as a function of its inputsize(), targetsize() and set options.

Implements PLearn::PLearner.

Definition at line 144 of file BaggingLearner.cc.

References PLearn::TVec< T >::length(), output_sub_outputs, PLASSERT, splitter, stats, and template_learner.

Referenced by computeOutput().

{ 
    PLASSERT(template_learner);
    PLASSERT(splitter);
    int sz= template_learner->outputsize() * stats.length(); 
    if(output_sub_outputs)
        sz+= template_learner->outputsize() * splitter->nsplits();
    return sz;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void PLearn::BaggingLearner::resetInternalState ( ) [virtual]

If any, reset the internal state Default: do nothing.

Reimplemented from PLearn::PLearner.

Definition at line 416 of file BaggingLearner.cc.

References i, learners, and PLearn::TVec< T >::length().

{
    for(int i= 0; i < learners.length(); ++i)
        learners[i]->resetInternalState();
}

Here is the call graph for this function:

void PLearn::BaggingLearner::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 476 of file BaggingLearner.cc.

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

{
    inherited::setExperimentDirectory(the_expdir);
    template_learner->setExperimentDirectory(the_expdir / "BaggingSubLearner");
}

Here is the call graph for this function:

void PLearn::BaggingLearner::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 434 of file BaggingLearner.cc.

References PLASSERT, PLearn::PLearner::setTrainingSet(), and template_learner.

{
    PLASSERT(template_learner);
    //set template learner's train set so that we can get 
    //output size and names (among others)
    template_learner->setTrainingSet(training_set, call_forget);
    inherited::setTrainingSet(training_set, call_forget);
}

Here is the call graph for this function:

void PLearn::BaggingLearner::setTrainStatsCollector ( PP< VecStatsCollector statscol) [virtual]

Sets the statistics collector whose update() method will be called during training.

Note: You shouldn't have to override this in subclasses, except maybe to forward the call to an underlying learner.

Reimplemented from PLearn::PLearner.

Definition at line 466 of file BaggingLearner.cc.

References PLearn::PLearner::setTrainStatsCollector(), and template_learner.

{
    inherited::setTrainStatsCollector(statscol);
    template_learner->setTrainStatsCollector(statscol);
}

Here is the call graph for this function:

void PLearn::BaggingLearner::train ( ) [virtual]

*** SUBCLASS WRITING: ***

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

TYPICAL CODE:

  static Vec input;  // static so we don't reallocate/deallocate memory each time...
  static Vec target; // (but be careful that static means shared!)
  input.resize(inputsize());    // the train_set's inputsize()
  target.resize(targetsize());  // the train_set's targetsize()
  real weight;
  
  if(!train_stats)   // make a default stats collector, in case there's none
      train_stats = new VecStatsCollector();
  
  if(nstages<stage)  // asking to revert to a previous stage!
      forget();      // reset the learner to stage=0
  
  while(stage<nstages)
  {
      // clear statistics of previous epoch
      train_stats->forget(); 
            
      //... train for 1 stage, and update train_stats,
      // using train_set->getSample(input, target, weight);
      // and train_stats->update(train_costs)
          
      ++stage;
      train_stats->finalize(); // finalize statistics for this epoch
  }

Implements PLearn::PLearner.

Definition at line 167 of file BaggingLearner.cc.

References PLearn::PLearnService::availableServers(), c, PLearn::RemotePLearnServer::callMethod(), DBG_LOG, PLearn::endl(), PLearn::PLearnService::freeServer(), i, PLearn::PLearner::initTrain(), PLearn::PLearnService::instance(), learners, PLearn::TVec< T >::length(), PLearn::PLearner::master_sends_testset_rows, PLearn::min(), PLearn::RemotePLearnServer::newObject(), PLearn::PLearner::nservers, PLearn::PLearner::nstages, PLearn::PLearner::parallelize_here, PLASSERT, PLERROR, PLearn::PLearner::report_progress, PLearn::PLearnService::reserveServers(), PLearn::TVec< T >::resize(), PLearn::TVec< T >::size(), splitter, PLearn::PLearner::stage, template_learner, PLearn::VMat::toMat(), PLearn::PLearner::train_set, and PLearn::PLearnService::waitForResult().

{
    PLASSERT(train_set);
    if(!splitter)
        PLERROR("BaggingLearner::train() needs a splitter.");
    if(!template_learner)
        PLERROR("BaggingLearner::train() needs a template learner.");
    if(nstages != 1)
        PLERROR("BaggingLearner.nstages should be 1 (not %d).", nstages);
    if(splitter->nSetsPerSplit() != 1)
        PLERROR("BaggingLearner.splitter->nSetsPerSplit() should be 1 (not %d).", 
                splitter->nSetsPerSplit());

    splitter->setDataSet(train_set);

    if (!initTrain())
        return;

    // init learners
    int nbags= splitter->nsplits();
    if(learners.size() != nbags)
    {
        learners.resize(nbags);
        for(int i= 0; i < nbags; ++i)
        {
            CopiesMap c;
            learners[i]= template_learner->deepCopy(c);
            learners[i]->report_progress= false;
        }
    }

    PP<ProgressBar> pb= 0;
    if(report_progress)
        pb= new ProgressBar("BaggingLearner::train",nbags);

    PLearnService& service(PLearnService::instance());
    int nservers= min(nbags, service.availableServers());

    if(nservers > 1 && parallelize_here)//parallel train
    {
        TVec<PP<RemotePLearnServer> > servers= service.reserveServers(nservers);
        nservers= servers.length();

        map<PP<RemotePLearnServer>, int> learners_ids;
        map<PP<RemotePLearnServer>, int> bagnums;
        map<PP<RemotePLearnServer>, int> step;

        for(int i= 0; i < nservers; ++i)
        {
            RemotePLearnServer* s= servers[i];
            int id= s->newObject(*learners[i]);
            VMat sts= splitter->getSplit(i)[0];
            if(master_sends_testset_rows)
                sts= new MemoryVMatrix(sts.toMat());
            s->callMethod(id, "setTrainingSet", sts, true);
            learners_ids[s]= id;
            bagnums[s]= i;
            step[s]= 1;
        }

        int lastbag= nservers-1;
        int ndone= 0;

        while(nservers > 0)
        {
            PP<RemotePLearnServer> s= service.waitForResult();
            switch(step[s])
            {
            case 1: 
                DBG_LOG << "** get setTrainingSet result" << endl;
                s->getResults();//from setTrainingSet
                s->callMethod(learners_ids[s], "train");
                step[s]= 2;
                break;
            case 2:
                DBG_LOG << "** get train result" << endl;
                s->getResults();//from train
                if(pb) pb->update(++ndone);
                s->callMethod(learners_ids[s], "getObject");
                step[s]= 3;
                break;
            case 3:
                DBG_LOG << "** get getObject result" << endl;
                s->getResults(learners[bagnums[s]]);//from getObject
                s->deleteObject(learners_ids[s]);
                if(++lastbag < nbags)
                {
                    int id= s->newObject(*learners[lastbag]);
                    VMat sts= splitter->getSplit(lastbag)[0];
                    if(master_sends_testset_rows)
                        sts= new MemoryVMatrix(sts.toMat());
                    s->callMethod(id, "setTrainingSet", sts, true);
                    learners_ids[s]= id;
                    bagnums[s]= lastbag;
                    step[s]= 1;
                }
                else
                {
                    service.freeServer(s);
                    --nservers;
                }
                break;
            }
        }

        return; // avoid extra indentation
    }

    // sequential train
    for(int i= 0; i < nbags; ++i)
    {
        PP<PLearner> l = learners[i];
        l->setTrainingSet(splitter->getSplit(i)[0]);
        l->train();
        if(pb) pb->update(i);
    }

    stage++;
    PLASSERT( stage == 1 );
}

Here is the call graph for this function:


Member Data Documentation

Reimplemented from PLearn::PLearner.

Definition at line 106 of file BaggingLearner.h.

for computeOutput: remove the highest and lowest outputs before averaging (nb.

to exclude at each end)

Definition at line 64 of file BaggingLearner.h.

Referenced by computeOutput(), and declareOptions().

Definition at line 136 of file BaggingLearner.h.

Referenced by makeDeepCopyFromShallowCopy().

Wether computeOutput should append sub-learners outputs to output.

Definition at line 66 of file BaggingLearner.h.

Referenced by computeOutput(), declareOptions(), getOutputNames(), and outputsize().

Mat PLearn::BaggingLearner::outputs [mutable, protected]

Definition at line 135 of file BaggingLearner.h.

Referenced by computeOutput(), and makeDeepCopyFromShallowCopy().

splitter used to get bags(=splits)

Definition at line 58 of file BaggingLearner.h.

Referenced by declareOptions(), getOutputNames(), getTestCostNames(), makeDeepCopyFromShallowCopy(), nTestCosts(), outputsize(), and train().

functions used to combine outputs from all learners

Definition at line 61 of file BaggingLearner.h.

Referenced by addStatNames(), computeOutput(), declareOptions(), makeDeepCopyFromShallowCopy(), and outputsize().

Definition at line 133 of file BaggingLearner.h.

Referenced by computeOutput().


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