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

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

#include <KFoldLogisticClassifier.h>

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

List of all members.

Public Member Functions

 KFoldLogisticClassifier ()
 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 string classname () const
virtual OptionListgetOptionList () const
virtual OptionMapgetOptionMap () const
virtual RemoteMethodMapgetRemoteMethodMap () const
virtual KFoldLogisticClassifierdeepCopy (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

int kfold
int max_degraded_steps
int max_epochs
int step_size

Static Public Attributes

static StaticInitializer _static_initializer_

Static Protected Member Functions

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

Protected Attributes

Vec store_output
 Used to store outputs.
TVec< PP< PLearner > > log_net

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


Member Typedef Documentation

Reimplemented from PLearn::PLearner.

Definition at line 59 of file KFoldLogisticClassifier.h.


Constructor & Destructor Documentation

PLearn::KFoldLogisticClassifier::KFoldLogisticClassifier ( )

Default constructor.

Definition at line 65 of file KFoldLogisticClassifier.cc.

                                                :
    kfold(5),
    max_degraded_steps(20),
    max_epochs(500),
    step_size(1)
{
}

Member Function Documentation

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

Reimplemented from PLearn::PLearner.

Definition at line 60 of file KFoldLogisticClassifier.cc.

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

Reimplemented from PLearn::PLearner.

Definition at line 60 of file KFoldLogisticClassifier.cc.

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

Reimplemented from PLearn::PLearner.

Definition at line 60 of file KFoldLogisticClassifier.cc.

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

Reimplemented from PLearn::PLearner.

Definition at line 60 of file KFoldLogisticClassifier.cc.

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

Reimplemented from PLearn::Object.

Definition at line 60 of file KFoldLogisticClassifier.cc.

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

Reimplemented from PLearn::PLearner.

Definition at line 60 of file KFoldLogisticClassifier.cc.

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

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

Reimplemented from PLearn::PLearner.

Definition at line 120 of file KFoldLogisticClassifier.cc.

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

Here is the call graph for this function:

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

This does the actual building.

Reimplemented from PLearn::PLearner.

Definition at line 113 of file KFoldLogisticClassifier.cc.

Referenced by build().

{
}

Here is the caller graph for this function:

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

Reimplemented from PLearn::Object.

Definition at line 60 of file KFoldLogisticClassifier.cc.

void PLearn::KFoldLogisticClassifier::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 295 of file KFoldLogisticClassifier.cc.

References PLearn::argmax(), PLearn::is_equal(), PLearn::TVec< T >::length(), pl_log, PLASSERT, PLearn::TVec< T >::resize(), and PLearn::sum().

{
    int t = int(round(target[0]));
    costs.resize(2);
    if (output.length() == 1) {
        // Binary classification.
        PLASSERT(output[0] >= 0 && output[0] <= 1);
        if (t == 1)
            costs[0] = - pl_log(output[0]);
        else {
            PLASSERT(t == 0);
            costs[0] = - pl_log(1 - output[0]);
        }
        costs[1] = (output[0] - 0.5) * (2 * target[0] - 1) >= 0 ? 0 : 1;
    } else {
        // More than two targets.
        PLASSERT(is_equal(sum(output), 1));
        costs[0] = - pl_log(output[t]);
        costs[1] = argmax(output) == t ? 0 : 1;
    }
}

Here is the call graph for this function:

void PLearn::KFoldLogisticClassifier::computeOutput ( const Vec input,
Vec output 
) const [virtual]

Computes the output from the input.

Reimplemented from PLearn::PLearner.

Definition at line 280 of file KFoldLogisticClassifier.cc.

References PLearn::TVec< T >::isEmpty(), PLearn::TVec< T >::length(), log_net, PLCHECK, PLearn::TVec< T >::resize(), and store_output.

{
    PLCHECK(!log_net.isEmpty());
    log_net[0]->computeOutput(input, output);
    store_output.resize(output.length());
    for (int k = 1; k < log_net.length(); k++) {
        log_net[k]->computeOutput(input, store_output);
        output += store_output;
    }
    output /= real(log_net.length());
}

Here is the call graph for this function:

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

Declares the class options.

Reimplemented from PLearn::PLearner.

Definition at line 76 of file KFoldLogisticClassifier.cc.

References PLearn::OptionBase::buildoption, PLearn::declareOption(), PLearn::PLearner::declareOptions(), kfold, PLearn::OptionBase::learntoption, log_net, max_degraded_steps, max_epochs, and step_size.

{
    // Build options.

    declareOption(ol, "kfold", &KFoldLogisticClassifier::kfold,
                  OptionBase::buildoption,
        "Number of splits of the data (and of classifiers being trained).");

    declareOption(ol, "max_degraded_steps",
                  &KFoldLogisticClassifier::max_degraded_steps,
                  OptionBase::buildoption,
        "Maximum number of optimization steps performed after finding a\n"
        "candidate for early stopping.");

    declareOption(ol, "max_epochs",
                  &KFoldLogisticClassifier::max_epochs,
                  OptionBase::buildoption,
        "Maximum number of epochs when training logistic classifiers\n");

    declareOption(ol, "step_size",
                  &KFoldLogisticClassifier::step_size,
                  OptionBase::buildoption,
        "Measure performance every 'step_size' epochs.");

    // Learnt options.

    declareOption(ol, "log_net", &KFoldLogisticClassifier::log_net,
                  OptionBase::learntoption,
        "Underlying logistic classifiers.");

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

Here is the call graph for this function:

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

Reimplemented from PLearn::PLearner.

Definition at line 138 of file KFoldLogisticClassifier.h.

:

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

Reimplemented from PLearn::PLearner.

Definition at line 60 of file KFoldLogisticClassifier.cc.

void PLearn::KFoldLogisticClassifier::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 157 of file KFoldLogisticClassifier.cc.

References PLearn::PLearner::forget(), log_net, and PLearn::TVec< T >::resize().

{

    inherited::forget();
    log_net.resize(0);
}

Here is the call graph for this function:

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

Reimplemented from PLearn::Object.

Definition at line 60 of file KFoldLogisticClassifier.cc.

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

Reimplemented from PLearn::Object.

Definition at line 60 of file KFoldLogisticClassifier.cc.

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

Reimplemented from PLearn::Object.

Definition at line 60 of file KFoldLogisticClassifier.cc.

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

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

Implements PLearn::PLearner.

Definition at line 321 of file KFoldLogisticClassifier.cc.

References PLearn::TVec< T >::append(), and PLearn::TVec< T >::isEmpty().

{
    static TVec<string> costs;
    if (costs.isEmpty()) {
        costs.append("nll");
        costs.append("class_error");
    }
    return costs;
}

Here is the call graph for this function:

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

{
    return TVec<string>();
}
void PLearn::KFoldLogisticClassifier::makeDeepCopyFromShallowCopy ( CopiesMap copies) [virtual]

Transforms a shallow copy into a deep copy.

Reimplemented from PLearn::PLearner.

Definition at line 129 of file KFoldLogisticClassifier.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("KFoldLogisticClassifier::makeDeepCopyFromShallowCopy not fully (correctly) implemented yet!");
}

Here is the call graph for this function:

int PLearn::KFoldLogisticClassifier::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 146 of file KFoldLogisticClassifier.cc.

References PLearn::TVec< T >::isEmpty(), and log_net.

{
    if (log_net.isEmpty())
        return -1;
    else
        return log_net[0]->outputsize();
}

Here is the call graph for this function:

void PLearn::KFoldLogisticClassifier::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 176 of file KFoldLogisticClassifier.cc.

References PLearn::TVec< T >::append(), PLearn::TVec< T >::find(), PLearn::get_pointer(), PLearn::VMat::getExample(), i, PLearn::PLearner::initTrain(), j, kfold, PLearn::TVec< T >::length(), PLearn::VMat::length(), log_net, max_degraded_steps, max_epochs, PLCHECK, PLearn::PLearner::report_progress, PLearn::TVec< T >::resize(), PLearn::PLearner::seed_, PLearn::PLearner::stage, step_size, and PLearn::PLearner::train_set.

{
    if (!initTrain())
        return;

    PLCHECK( stage == 0 );

    // Find out the number of classes in the dataset.
    TVec<bool> all_classes;
    Vec input, target;
    real weight;
    PLCHECK(train_set->targetsize() == 1);
    for (int i = 0; i < train_set->length(); i++) {
        train_set->getExample(i, input, target, weight);
        int t = int(round(target[0]));
        if (t >= all_classes.length()) {
            int n_to_add = t - all_classes.length() + 1;
            for (int j = 0; j < n_to_add; j++)
                all_classes.append(false);
        }
        all_classes[t] = true;
    }
    int n_classes = all_classes.length();
    PLCHECK(n_classes >= 2);
    PLCHECK(all_classes.find(false) == -1);

    // Split the data.
    PP<KFoldSplitter> splitter = new KFoldSplitter();
    splitter->K = this->kfold;
    splitter->build();
    splitter->setDataSet(train_set);

    // Create logistic regressors.
    log_net.resize(0);
    string cost_func;
    for (int k = 0; k < kfold; k++) {
        PP<ConjGradientOptimizer> opt = new ConjGradientOptimizer();
        opt->build();
        PP<NNet> nnet = new NNet();
        nnet->optimizer = opt;
        nnet->seed_ = this->seed_;
        nnet->report_progress = this->report_progress;
        if (n_classes == 2) {
            cost_func = "stable_cross_entropy";
            nnet->output_transfer_func = "sigmoid";
            nnet->noutputs = 1;
        } else {
            cost_func = "NLL";
            nnet->output_transfer_func = "softmax";
            nnet->noutputs = n_classes;
        }
        nnet->cost_funcs = TVec<string>(1, cost_func);
        nnet->batch_size = 0;
        nnet->build();
        log_net.append(get_pointer(nnet));
    }

    // Train logistic regressors.
    for (int k = 0; k < log_net.length(); k++) {
        // Initialize the hyper-learning framework for early stopping.
        // Splitter.
        PP<ExplicitSplitter> hsplitter = new ExplicitSplitter();
        hsplitter->splitsets = TMat<VMat>(1, 2);
        hsplitter->splitsets(0) << splitter->getSplit(k);
        hsplitter->build();
        // PTester.
        PP<PTester> htester = new PTester();
        htester->splitter = hsplitter;
        string cost = "E[test.E[" + cost_func + "]]";
        htester->setStatNames(TVec<string>(1, cost), false);
        htester->build();
        // Oracle.
        PP<EarlyStoppingOracle> early_stop = new EarlyStoppingOracle();
        early_stop->max_degraded_steps = this->max_degraded_steps;
        early_stop->range = TVec<double>(3, this->step_size);
        early_stop->range[1] = this->max_epochs + 1;
        early_stop->option = "nstages";
        early_stop->build();
        // Strategy.
        PP<HyperOptimize> strategy = new HyperOptimize();
        strategy->oracle = early_stop;
        strategy->which_cost = "0";
        strategy->build();
        // HyperLearner.
        PP<HyperLearner> hyper = new HyperLearner();
        hyper->dont_restart_upon_change = TVec<string>(1, "nstages");
        hyper->learner_ = log_net[k];
        hyper->option_fields = hyper->dont_restart_upon_change;
        hyper->save_final_learner = false;
        hyper->strategy = TVec< PP<HyperCommand> >(1, get_pointer(strategy));
        hyper->tester = htester;
        hyper->verbosity = 0; // Get rid of useless output.
        hyper->build();
        // Perform training.
        hyper->train();
        // Make sure we keep only the best model.
        log_net[k] = hyper->getLearner();
    }
    this->stage = 1;
}

Here is the call graph for this function:


Member Data Documentation

Reimplemented from PLearn::PLearner.

Definition at line 138 of file KFoldLogisticClassifier.h.

Definition at line 64 of file KFoldLogisticClassifier.h.

Referenced by declareOptions(), and train().

Definition at line 154 of file KFoldLogisticClassifier.h.

Referenced by computeOutput(), declareOptions(), forget(), outputsize(), and train().

Definition at line 65 of file KFoldLogisticClassifier.h.

Referenced by declareOptions(), and train().

Definition at line 66 of file KFoldLogisticClassifier.h.

Referenced by declareOptions(), and train().

Definition at line 67 of file KFoldLogisticClassifier.h.

Referenced by declareOptions(), and train().

Used to store outputs.

Definition at line 150 of file KFoldLogisticClassifier.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