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

#include <ClassifierFromDensity.h>

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

List of all members.

Public Member Functions

 ClassifierFromDensity ()
 Default constructor.
virtual void build ()
 Simply calls inherited::build() then build_()
virtual void makeDeepCopyFromShallowCopy (CopiesMap &copies)
 Transforms a shallow copy into a deep copy.
virtual string classname () const
virtual OptionListgetOptionList () const
virtual OptionMapgetOptionMap () const
virtual RemoteMethodMapgetRemoteMethodMap () const
virtual ClassifierFromDensitydeepCopy (CopiesMap &copies) const
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< string > getTestCostNames () const
 Returns the names of the costs computed by computeCostsFromOutpus (and thus the test method)
virtual TVec< string > getTrainCostNames () const
 Returns the names of the objective costs that the train method computes and for which it updates the VecStatsCollector train_stats.

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 nclasses
TVec< PP< PDistribution > > estimators
Vec log_priors
bool output_log_probabilities
bool normalize_probabilities
Vec use_these_priors
bool using_template_estimator

Static Public Attributes

static StaticInitializer _static_initializer_

Static Protected Member Functions

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

Private Types

typedef PLearner inherited

Private Member Functions

void build_ ()
 This does the actual building.

Detailed Description

Definition at line 49 of file ClassifierFromDensity.h.


Member Typedef Documentation

Reimplemented from PLearn::PLearner.

Definition at line 54 of file ClassifierFromDensity.h.


Constructor & Destructor Documentation

PLearn::ClassifierFromDensity::ClassifierFromDensity ( )

Default constructor.

Definition at line 53 of file ClassifierFromDensity.cc.


Member Function Documentation

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

Reimplemented from PLearn::PLearner.

Definition at line 67 of file ClassifierFromDensity.cc.

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

Reimplemented from PLearn::PLearner.

Definition at line 67 of file ClassifierFromDensity.cc.

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

Reimplemented from PLearn::PLearner.

Definition at line 67 of file ClassifierFromDensity.cc.

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

Reimplemented from PLearn::PLearner.

Definition at line 67 of file ClassifierFromDensity.cc.

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

Reimplemented from PLearn::Object.

Definition at line 67 of file ClassifierFromDensity.cc.

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

Reimplemented from PLearn::PLearner.

Definition at line 67 of file ClassifierFromDensity.cc.

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

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

Reimplemented from PLearn::PLearner.

Definition at line 110 of file ClassifierFromDensity.cc.

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

Referenced by forget().

Here is the call graph for this function:

Here is the caller graph for this function:

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

This does the actual building.

Reimplemented from PLearn::PLearner.

Definition at line 119 of file ClassifierFromDensity.cc.

References PLearn::deepCopy(), estimators, i, nclasses, PLERROR, PLearn::TVec< T >::resize(), PLearn::TVec< T >::size(), and using_template_estimator.

Referenced by build().

{
    if(estimators.size()==1)
    {
        using_template_estimator = true;
        estimators.resize(nclasses);
        for(int i=1; i<nclasses; i++)
            estimators[i] = PLearn::deepCopy(estimators[0]);
    }
    else if(estimators.size() != nclasses)
        PLERROR("In ClassifierFromDensity: specified %d estimators but there are %d classes",estimators.size(), nclasses);
}

Here is the call graph for this function:

Here is the caller graph for this function:

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

Reimplemented from PLearn::Object.

Definition at line 67 of file ClassifierFromDensity.cc.

void PLearn::ClassifierFromDensity::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 287 of file ClassifierFromDensity.cc.

References PLearn::class_error(), PLearn::condprob_cost(), and PLearn::TVec< T >::resize().

{
    static CostFunc cl_er;
    static CostFunc cond_p;
    if(!cl_er)
        cl_er = class_error();
    if(!cond_p)
        cond_p = condprob_cost();
    costs.resize(2);
    costs[0] = cl_er->evaluate(output, target);
    costs[1] = cond_p->evaluate(output, target);
}                                

Here is the call graph for this function:

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

Computes the output from the input.

Reimplemented from PLearn::PLearner.

Definition at line 249 of file ClassifierFromDensity.cc.

References c, estimators, PLearn::exp(), log_priors, PLearn::logadd(), nclasses, normalize_probabilities, output_log_probabilities, and PLearn::TVec< T >::resize().

{
    output.resize(nclasses);
    double log_of_sumprob = 0.;

    for(int c=0; c<nclasses; c++)
    {
        // Be slightly careful in the case were the log_prior is -Inf, i.e.
        // no observation of the current class appears in the training set.
        // Don't call the estimator in that case, since its output might be
        // ill-defined (NaN or some such), thereby polluting the rest of the
        // output computation

        double logprob_c = log_priors[c];
        if (! isinf(log_priors[c]))
            logprob_c += estimators[c]->log_density(input);  // multiply p by the prior

        output[c] = logprob_c;
        if (normalize_probabilities)
        {
            if(c==0)
                log_of_sumprob = logprob_c;
            else
                log_of_sumprob = logadd(log_of_sumprob, logprob_c);
        }
    }      

    if (normalize_probabilities)
        output -= real(log_of_sumprob); // divide by the sum

    // Make it probabilities rather than log probabilities...
    if (!output_log_probabilities)
        exp(output, output);
}

Here is the call graph for this function:

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

Declares this class' options.

Reimplemented from PLearn::PLearner.

Definition at line 72 of file ClassifierFromDensity.cc.

References PLearn::OptionBase::buildoption, PLearn::declareOption(), PLearn::PLearner::declareOptions(), estimators, PLearn::OptionBase::learntoption, log_priors, nclasses, normalize_probabilities, output_log_probabilities, use_these_priors, and using_template_estimator.

{

    // Build options.

    declareOption(ol, "nclasses", &ClassifierFromDensity::nclasses, OptionBase::buildoption,
                  "The number of classes");

    declareOption(ol, "estimators", &ClassifierFromDensity::estimators, OptionBase::buildoption,
                  "The array of density estimators, one for each class.\n"
                  "You may also specify just one that will be replicated as many times as there are classes.");

    declareOption(ol, "output_log_probabilities", &ClassifierFromDensity::output_log_probabilities, OptionBase::buildoption,
                  "Whether computeOutput yields log-probabilities or probabilities (of classes given inputs)");
 
    declareOption(ol, "normalize_probabilities", &ClassifierFromDensity::normalize_probabilities, OptionBase::buildoption,
                  "Whether to normalize the probabilities (if not just compute likelihood * prior for each class)");

    declareOption(ol, "use_these_priors", &ClassifierFromDensity::use_these_priors, OptionBase::buildoption,
                  "If empty (the default), then prior class probability is determined upon \n"
                  "training from the empirical class count in the training set. Otherwise this\n"
                  "must be a vector of length nclasses, specifying these prior class prob. \n");

    // Learnt options.

    declareOption(ol, "log_priors", &ClassifierFromDensity::log_priors, OptionBase::learntoption,
                  "The log of the class prior probabilities");

    declareOption(ol, "using_template_estimator", &ClassifierFromDensity::using_template_estimator, OptionBase::learntoption,
                  "Indication that the estimators were originally obtained from one single template");

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

Here is the call graph for this function:

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

Reimplemented from PLearn::PLearner.

Definition at line 106 of file ClassifierFromDensity.h.

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

Reimplemented from PLearn::PLearner.

Definition at line 67 of file ClassifierFromDensity.cc.

void PLearn::ClassifierFromDensity::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 154 of file ClassifierFromDensity.cc.

References build(), c, estimators, PLearn::TVec< T >::length(), PLearn::TVec< T >::resize(), PLearn::PLearner::stage, and using_template_estimator.

Referenced by train().

{
    stage=0;
    if (using_template_estimator)
        estimators.resize(1);
    for(int c=0; c<estimators.length(); c++)
        estimators[c]->forget();
    if (using_template_estimator)
        build(); // Ensure the first estimator is duplicated.
}

Here is the call graph for this function:

Here is the caller graph for this function:

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

Reimplemented from PLearn::Object.

Definition at line 67 of file ClassifierFromDensity.cc.

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

Reimplemented from PLearn::Object.

Definition at line 67 of file ClassifierFromDensity.cc.

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

Reimplemented from PLearn::Object.

Definition at line 67 of file ClassifierFromDensity.cc.

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

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

Implements PLearn::PLearner.

Definition at line 304 of file ClassifierFromDensity.cc.

{
    TVec<string> cnames(2);
    cnames[0] = "class_error";
    cnames[1] = "condprob_cost";
    return cnames;
}
TVec< string > PLearn::ClassifierFromDensity::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 315 of file ClassifierFromDensity.cc.

Referenced by train().

{
    return TVec<string>();
}

Here is the caller graph for this function:

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

Transforms a shallow copy into a deep copy.

Reimplemented from PLearn::PLearner.

Definition at line 135 of file ClassifierFromDensity.cc.

References PLearn::deepCopyField(), estimators, log_priors, PLearn::PLearner::makeDeepCopyFromShallowCopy(), and use_these_priors.

Here is the call graph for this function:

int PLearn::ClassifierFromDensity::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 ClassifierFromDensity.cc.

References nclasses.

{
    return nclasses;
}
void PLearn::ClassifierFromDensity::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 168 of file ClassifierFromDensity.cc.

References c, PLearn::endl(), estimators, forget(), PLearn::PLearner::getExperimentDirectory(), getTrainCostNames(), PLearn::hconcat(), PLearn::indicesOfOccurencesInColumn(), PLearn::PLearner::inputsize(), PLearn::VMat::length(), PLearn::TVec< T >::length(), log_priors, nclasses, PLearn::PLearner::nstages, PLearn::perr, pl_log, PLERROR, PLearn::TVec< T >::resize(), PLearn::VMat::rows(), PLearn::PLearner::stage, PLearn::VMat::subMatColumns(), PLearn::PLearner::targetsize(), PLearn::tostring(), PLearn::PLearner::train_set, PLearn::PLearner::train_stats, use_these_priors, and PLearn::PLearner::verbosity.

{
    if(targetsize()!=1)
        PLERROR("In ClassifierFromDensity::train - Expecting a targetsize of 1 (class index between 0 and nclasses-1), not %d !!",targetsize());

    if(nstages<stage) // asking to revert to a previous stage!
        forget();  // reset the learner to stage=0

    if(stage==0)
    {
        map<real, TVec<int> > indices = indicesOfOccurencesInColumn(train_set, inputsize());

        log_priors.resize(nclasses);
        if(use_these_priors.length()==0)
        {
            for(int c=0; c<nclasses; c++)
                log_priors[c] = pl_log(real(indices[real(c)].length())) - pl_log(real(train_set.length())); // how many do we have?
        }
        else
        {
            if(use_these_priors.length()!=nclasses)
                PLERROR("In ClassifierFromDensity::train, when compute_empirical_priors is false, you must specify a proper log_priors of length nclasses");  
            for(int c=0; c<nclasses; c++)
                log_priors[c] = pl_log(use_these_priors[c]);
        }

        PPath expd = getExperimentDirectory();

        for(int c=0; c<nclasses; c++)
        {
            if(verbosity>=1)
                perr << ">>> Training class " << c << endl;
            VMat set_c = train_set.rows(indices[c]);
            int in_sz = set_c->inputsize();
            int targ_sz = set_c->targetsize();
            int we_sz = set_c->weightsize();
            // Removing target from set
            if (we_sz==0)
            {
                set_c = set_c.subMatColumns(0,in_sz);
                set_c->defineSizes(in_sz, 0, 0);
            }
            else // There are some weights.
            {
                set_c = hconcat(set_c.subMatColumns(0,in_sz), set_c.subMatColumns(in_sz+targ_sz,we_sz));
                set_c->defineSizes(in_sz,0,we_sz);
            }
            if (expd!="")
                estimators[c]->setExperimentDirectory(expd / "Class" / tostring(c));
            if (verbosity>=1)
                perr << " ( " << set_c.length() << " samples)" << endl;
            estimators[c]->setTrainingSet(set_c);
            PP<VecStatsCollector> train_stats = new VecStatsCollector();
            train_stats->setFieldNames(estimators[c]->getTrainCostNames());
            estimators[c]->setTrainStatsCollector(train_stats);
            estimators[c]->nstages = nstages;
            estimators[c]->train();
        }
        stage = nstages; // trained!
        if (verbosity >= 2)
            perr << ">>> Training is over" << endl;
    }
    if(stage>0 && stage<nstages)
    {
        for(int c=0; c<nclasses; c++)
        {
            if(verbosity>=1)
                perr << ">>> Training class " << c;
            estimators[c]->nstages = nstages;
            estimators[c]->train();
        }
        stage = nstages;
        if (verbosity >= 2)
            perr << ">>> Training is over" << endl;
    }
    
}

Here is the call graph for this function:


Member Data Documentation

Reimplemented from PLearn::PLearner.

Definition at line 106 of file ClassifierFromDensity.h.

Definition at line 63 of file ClassifierFromDensity.h.

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

Definition at line 67 of file ClassifierFromDensity.h.

Referenced by computeOutput(), and declareOptions().

Definition at line 66 of file ClassifierFromDensity.h.

Referenced by computeOutput(), and declareOptions().

Definition at line 68 of file ClassifierFromDensity.h.

Referenced by declareOptions(), makeDeepCopyFromShallowCopy(), and train().

Definition at line 69 of file ClassifierFromDensity.h.

Referenced by build_(), declareOptions(), and forget().


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