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

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

#include <ToBagClassifier.h>

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

List of all members.

Public Member Functions

 ToBagClassifier ()
 Default constructor.
virtual void setTrainingSet (VMat training_set, bool call_forget=true)
 Set training set.
virtual int targetsize () const
 Targetsize is one more than the inner learner's targetsize.
virtual void computeCostsFromOutputs (const Vec &input, const Vec &output, const Vec &target, Vec &costs) const
 Compute costs.
virtual void computeOutputAndCosts (const Vec &input, const Vec &target, Vec &output, Vec &costs) const
 Overridden to be able to use the sub-learner's corresponding method.
virtual void computeOutputsAndCosts (const Mat &input, const Mat &target, Mat &output, Mat &costs) const
 Currently using PLearner's simple version for code simplicity.
virtual TVec< string > getTestCostNames () const
 Currently only compute classification error.
virtual string classname () const
virtual OptionListgetOptionList () const
virtual OptionMapgetOptionMap () const
virtual RemoteMethodMapgetRemoteMethodMap () const
virtual ToBagClassifierdeepCopy (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_ ()
 Declares name and deepCopy methods.
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 n_classes

Static Public Attributes

static StaticInitializer _static_initializer_

Protected Member Functions

void updateCostAndBagOutput (const Vec &target, const Vec &output, Vec &costs) const
 Given a target and the corresponding output, update the 'bag_output' data matrix and compute the costs (which will be missing except for the last sample in a bag).
void fillSubTarget (const Vec &target) const
 Fill 'sub_target' with all elements of 'target' but the last one.

Static Protected Member Functions

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

Protected Attributes

Vec sub_target
 Used to store the target forwarded to the inner learner.
Mat bag_output
 Used to store outputs on a bag.
Vec votes
 Used to store votes.

Private Types

typedef EmbeddedLearner 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 ToBagClassifier.h.


Member Typedef Documentation

Reimplemented from PLearn::EmbeddedLearner.

Definition at line 59 of file ToBagClassifier.h.


Constructor & Destructor Documentation

PLearn::ToBagClassifier::ToBagClassifier ( )

Default constructor.

Definition at line 66 of file ToBagClassifier.cc.

                                :
    n_classes(-1)
{}

Member Function Documentation

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

Declares name and deepCopy methods.

Reimplemented from PLearn::EmbeddedLearner.

Definition at line 61 of file ToBagClassifier.cc.

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

Reimplemented from PLearn::EmbeddedLearner.

Definition at line 61 of file ToBagClassifier.cc.

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

Reimplemented from PLearn::EmbeddedLearner.

Definition at line 61 of file ToBagClassifier.cc.

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

Reimplemented from PLearn::EmbeddedLearner.

Definition at line 61 of file ToBagClassifier.cc.

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

Reimplemented from PLearn::EmbeddedLearner.

Definition at line 61 of file ToBagClassifier.cc.

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

Reimplemented from PLearn::EmbeddedLearner.

Definition at line 61 of file ToBagClassifier.cc.

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

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

Reimplemented from PLearn::EmbeddedLearner.

Definition at line 94 of file ToBagClassifier.cc.

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

Here is the call graph for this function:

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

This does the actual building.

Reimplemented from PLearn::EmbeddedLearner.

Definition at line 87 of file ToBagClassifier.cc.

Referenced by build().

{
}

Here is the caller graph for this function:

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

Reimplemented from PLearn::EmbeddedLearner.

Definition at line 61 of file ToBagClassifier.cc.

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

Compute costs.

Reimplemented from PLearn::EmbeddedLearner.

Definition at line 103 of file ToBagClassifier.cc.

References PLearn::EmbeddedLearner::computeCostsFromOutputs(), fillSubTarget(), sub_target, and updateCostAndBagOutput().

{
    fillSubTarget(target);
    inherited::computeCostsFromOutputs(input, output, sub_target, costs);
    updateCostAndBagOutput(target, output, costs);
}

Here is the call graph for this function:

void PLearn::ToBagClassifier::computeOutputAndCosts ( const Vec input,
const Vec target,
Vec output,
Vec costs 
) const [virtual]

Overridden to be able to use the sub-learner's corresponding method.

Reimplemented from PLearn::EmbeddedLearner.

Definition at line 116 of file ToBagClassifier.cc.

References PLearn::EmbeddedLearner::computeOutputAndCosts(), fillSubTarget(), sub_target, and updateCostAndBagOutput().

{
    fillSubTarget(target);
    inherited::computeOutputAndCosts(input, sub_target, output, costs);
    updateCostAndBagOutput(target, output, costs);
}

Here is the call graph for this function:

virtual void PLearn::ToBagClassifier::computeOutputsAndCosts ( const Mat input,
const Mat target,
Mat output,
Mat costs 
) const [inline, virtual]

Currently using PLearner's simple version for code simplicity.

Reimplemented from PLearn::EmbeddedLearner.

Definition at line 91 of file ToBagClassifier.h.

    {
        PLearner::computeOutputsAndCosts(input, target, output, costs);
    }
void PLearn::ToBagClassifier::declareOptions ( OptionList ol) [static, protected]

Declares the class options.

Reimplemented from PLearn::EmbeddedLearner.

Definition at line 73 of file ToBagClassifier.cc.

References PLearn::OptionBase::buildoption, PLearn::declareOption(), PLearn::EmbeddedLearner::declareOptions(), and n_classes.

{
    declareOption(ol, "n_classes", &ToBagClassifier::n_classes,
                  OptionBase::buildoption,
        "Number of classes in the dataset. This option is required to\n"
        "compute the confusion matrix, but may be ignored otherwise.");

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

Here is the call graph for this function:

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

Reimplemented from PLearn::EmbeddedLearner.

Definition at line 105 of file ToBagClassifier.h.

:

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

Reimplemented from PLearn::EmbeddedLearner.

Definition at line 61 of file ToBagClassifier.cc.

void PLearn::ToBagClassifier::fillSubTarget ( const Vec target) const [protected]

Fill 'sub_target' with all elements of 'target' but the last one.

Definition at line 128 of file ToBagClassifier.cc.

References PLearn::TVec< T >::length(), PLearn::TVec< T >::resize(), sub_target, and PLearn::TVec< T >::subVec().

Referenced by computeCostsFromOutputs(), and computeOutputAndCosts().

{
    sub_target.resize(target.length() - 1);
    sub_target << target.subVec(0, sub_target.length());
}

Here is the call graph for this function:

Here is the caller graph for this function:

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

Reimplemented from PLearn::EmbeddedLearner.

Definition at line 61 of file ToBagClassifier.cc.

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

Reimplemented from PLearn::EmbeddedLearner.

Definition at line 61 of file ToBagClassifier.cc.

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

Reimplemented from PLearn::EmbeddedLearner.

Definition at line 61 of file ToBagClassifier.cc.

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

Currently only compute classification error.

Reimplemented from PLearn::EmbeddedLearner.

Definition at line 138 of file ToBagClassifier.cc.

References PLearn::TVec< T >::append(), i, j, n_classes, and PLearn::tostring().

{
    TVec<string> costs;
    costs.append("class_error");
    if (n_classes > 0)
        for (int i = 0; i < n_classes; i++)
            for (int j = 0; j < n_classes; j++)
                costs.append("cm_" + tostring(i) + tostring(j));
    return costs;
}

Here is the call graph for this function:

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

Transforms a shallow copy into a deep copy.

Reimplemented from PLearn::EmbeddedLearner.

Definition at line 152 of file ToBagClassifier.cc.

References bag_output, PLearn::deepCopyField(), PLearn::EmbeddedLearner::makeDeepCopyFromShallowCopy(), sub_target, and votes.

Here is the call graph for this function:

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

Set training set.

Reimplemented from PLearn::EmbeddedLearner.

Definition at line 163 of file ToBagClassifier.cc.

References PLearn::VMatrix::defineSizes(), PLearn::get_pointer(), PLearn::VMat::length(), PLCHECK, PLearn::EmbeddedLearner::setInnerLearnerTrainingSet(), and PLearn::VMat::width().

{
    // Remove bag information (last target).
    PLCHECK( training_set->weightsize() == 0 &&
             training_set->extrasize() == 0 ); // Not compatible yet.
    PP<SubVMatrix> sub_train_set = new SubVMatrix(training_set, 0, 0, 
                                                  training_set->length(),
                                                  training_set->width() - 1);
    sub_train_set->defineSizes(training_set->inputsize(),
                               training_set->targetsize() - 1,
                               training_set->weightsize(),
                               training_set->extrasize());
    setInnerLearnerTrainingSet(get_pointer(sub_train_set), call_forget);
    PLearner::setTrainingSet(training_set, call_forget);
}

Here is the call graph for this function:

int PLearn::ToBagClassifier::targetsize ( ) const [virtual]

Targetsize is one more than the inner learner's targetsize.

Reimplemented from PLearn::EmbeddedLearner.

Definition at line 182 of file ToBagClassifier.cc.

References PLearn::EmbeddedLearner::learner_.

{
    return learner_->targetsize() + 1;
}
void PLearn::ToBagClassifier::updateCostAndBagOutput ( const Vec target,
const Vec output,
Vec costs 
) const [protected]

Given a target and the corresponding output, update the 'bag_output' data matrix and compute the costs (which will be missing except for the last sample in a bag).

Definition at line 190 of file ToBagClassifier.cc.

References PLearn::TMat< T >::appendRow(), PLearn::argmax(), bag_output, PLearn::columnSum(), PLearn::TVec< T >::fill(), i, PLearn::is_equal(), PLearn::is_missing(), PLearn::TVec< T >::lastElement(), PLearn::TVec< T >::length(), MISSING_VALUE, n_classes, PLearn::PLearner::nTestCosts(), PLASSERT, PLearn::TMat< T >::resize(), PLearn::TVec< T >::resize(), PLearn::TVec< T >::subVec(), PLearn::sum(), PLearn::SumOverBagsVariable::TARGET_COLUMN_FIRST, PLearn::SumOverBagsVariable::TARGET_COLUMN_LAST, votes, and PLearn::TMat< T >::width().

Referenced by computeCostsFromOutputs(), and computeOutputAndCosts().

{
    costs.resize(nTestCosts());
    costs.fill(MISSING_VALUE);

    int bag_info = int(round(target.lastElement()));
    if (bag_info & SumOverBagsVariable::TARGET_COLUMN_FIRST)
        bag_output.resize(0, 0);

    // Ignore missing outputs from learner.
    if (is_missing(output[0])) {
#ifdef BOUNDCHECK
        for (int i = 1; i < output.length(); i++) {
            PLASSERT( is_missing(output[i]) );
        }
#endif
        return;
    }
    // Ensure the distribution probabilities sum to 1. We relax a
    // bit the default tolerance as probabilities using
    // exponentials could suffer numerical imprecisions.
    PLASSERT( is_equal( sum(output), 1., 1., 1e-5, 1e-5 ) );
    bag_output.appendRow(output);
    if (bag_info & SumOverBagsVariable::TARGET_COLUMN_LAST) {
        // Perform majority vote.
        votes.resize(bag_output.width());
        columnSum(bag_output, votes);
        int target_class = int(round(target[0]));
        int prediction = argmax(votes);
        if (prediction == target_class)
            costs[0] = 0;
        else
            costs[0] = 1;
        if (n_classes > 0) {
            int i_start = 1 + target_class * n_classes;
            costs.subVec(i_start, n_classes).fill(0);
            costs[i_start + prediction] = 1;
        }
    }
}

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Reimplemented from PLearn::EmbeddedLearner.

Definition at line 105 of file ToBagClassifier.h.

Used to store outputs on a bag.

Definition at line 120 of file ToBagClassifier.h.

Referenced by makeDeepCopyFromShallowCopy(), and updateCostAndBagOutput().

Definition at line 64 of file ToBagClassifier.h.

Referenced by declareOptions(), getTestCostNames(), and updateCostAndBagOutput().

Used to store the target forwarded to the inner learner.

Definition at line 117 of file ToBagClassifier.h.

Referenced by computeCostsFromOutputs(), computeOutputAndCosts(), fillSubTarget(), and makeDeepCopyFromShallowCopy().

Vec PLearn::ToBagClassifier::votes [mutable, protected]

Used to store votes.

Definition at line 123 of file ToBagClassifier.h.

Referenced by makeDeepCopyFromShallowCopy(), and updateCostAndBagOutput().


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