PLearn 0.1
|
The first sentence should be a BRIEF DESCRIPTION of what the class does. More...
#include <LocalGaussianClassifier.h>
Public Member Functions | |
LocalGaussianClassifier () | |
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 | setTrainingSet (VMat training_set, bool call_forget=true) |
Declares the training set. | |
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). | |
real | computeLogWeight (const Vec &input, const Vec &traininput) const |
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 OptionList & | getOptionList () const |
virtual OptionMap & | getOptionMap () const |
virtual RemoteMethodMap & | getRemoteMethodMap () const |
virtual LocalGaussianClassifier * | deepCopy (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 PPath & | declaringFile () |
Public Attributes | |
int | nclasses |
real | computation_neighbors |
real | kernel_sigma |
real | regularization_sigma |
real | ignore_weights_below |
Static Public Attributes | |
static StaticInitializer | _static_initializer_ |
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. | |
Private Attributes | |
real | minus_one_half_over_kernel_sigma_square |
Vec | trainsample |
Global storage to save memory allocations. | |
Vec | traininput |
real * | traintarget_ptr |
real * | trainweight_ptr |
TVec< pair< real, int > > | pqvec |
Vec | log_counts |
Vec | log_counts2 |
Mat | means |
Mat | allcovars |
TVec< Mat > | covars |
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
Definition at line 61 of file LocalGaussianClassifier.h.
typedef PLearner PLearn::LocalGaussianClassifier::inherited [private] |
Reimplemented from PLearn::PLearner.
Definition at line 63 of file LocalGaussianClassifier.h.
PLearn::LocalGaussianClassifier::LocalGaussianClassifier | ( | ) |
Default constructor.
Definition at line 52 of file LocalGaussianClassifier.cc.
:nclasses(-1), computation_neighbors(-1), kernel_sigma(0.1), regularization_sigma(1e-6), ignore_weights_below(1e-8), minus_one_half_over_kernel_sigma_square(0), traintarget_ptr(0), trainweight_ptr(0) { // ### You may (or not) want to call build_() to finish building the object // ### (doing so assumes the parent classes' build_() have been called too // ### in the parent classes' constructors, something that you must ensure) // ### If this learner needs to generate random numbers, uncomment the // ### line below to enable the use of the inherited PRandom object. // random_gen = new PRandom(); }
string PLearn::LocalGaussianClassifier::_classname_ | ( | ) | [static] |
Reimplemented from PLearn::PLearner.
Definition at line 50 of file LocalGaussianClassifier.cc.
OptionList & PLearn::LocalGaussianClassifier::_getOptionList_ | ( | ) | [static] |
Reimplemented from PLearn::PLearner.
Definition at line 50 of file LocalGaussianClassifier.cc.
RemoteMethodMap & PLearn::LocalGaussianClassifier::_getRemoteMethodMap_ | ( | ) | [static] |
Reimplemented from PLearn::PLearner.
Definition at line 50 of file LocalGaussianClassifier.cc.
Reimplemented from PLearn::PLearner.
Definition at line 50 of file LocalGaussianClassifier.cc.
Object * PLearn::LocalGaussianClassifier::_new_instance_for_typemap_ | ( | ) | [static] |
Reimplemented from PLearn::Object.
Definition at line 50 of file LocalGaussianClassifier.cc.
StaticInitializer LocalGaussianClassifier::_static_initializer_ & PLearn::LocalGaussianClassifier::_static_initialize_ | ( | ) | [static] |
Reimplemented from PLearn::PLearner.
Definition at line 50 of file LocalGaussianClassifier.cc.
void PLearn::LocalGaussianClassifier::build | ( | ) | [virtual] |
Finish building the object; just call inherited::build followed by build_()
Reimplemented from PLearn::PLearner.
Definition at line 145 of file LocalGaussianClassifier.cc.
References PLearn::PLearner::build(), and build_().
{ inherited::build(); build_(); }
void PLearn::LocalGaussianClassifier::build_ | ( | ) | [private] |
This does the actual building.
Reimplemented from PLearn::PLearner.
Definition at line 124 of file LocalGaussianClassifier.cc.
References PLearn::PP< T >::isNotNull(), kernel_sigma, minus_one_half_over_kernel_sigma_square, setTrainingSet(), and PLearn::PLearner::train_set.
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. // PLASSERT(weighting_kernel.isNotNull()); if(train_set.isNotNull()) setTrainingSet(train_set, false); minus_one_half_over_kernel_sigma_square = -0.5/(kernel_sigma*kernel_sigma); }
string PLearn::LocalGaussianClassifier::classname | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 50 of file LocalGaussianClassifier.cc.
void PLearn::LocalGaussianClassifier::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 370 of file LocalGaussianClassifier.cc.
References PLearn::argmax(), c, PLearn::logadd(), and PLearn::TVec< T >::resize().
{ costs.resize(2); int c = int(target[0]); costs[0] = (argmax(output)==c ?0.0 :1.0); costs[1] = logadd(output)-output[c]; }
real PLearn::LocalGaussianClassifier::computeLogWeight | ( | const Vec & | input, |
const Vec & | traininput | ||
) | const |
Definition at line 247 of file LocalGaussianClassifier.cc.
References minus_one_half_over_kernel_sigma_square, and PLearn::powdistance().
Referenced by computeOutput().
{ return powdistance(input, traininput, 2.0, true)*minus_one_half_over_kernel_sigma_square; }
void PLearn::LocalGaussianClassifier::computeOutput | ( | const Vec & | input, |
Vec & | output | ||
) | const [virtual] |
Computes the output from the input.
Reimplemented from PLearn::PLearner.
Definition at line 252 of file LocalGaussianClassifier.cc.
References PLearn::addToDiagonal(), allcovars, PLearn::argmax(), PLearn::TVec< T >::begin(), c, PLearn::TVec< T >::clear(), PLearn::TMat< T >::clear(), computation_neighbors, computeLogWeight(), covars, d, PLearn::endl(), PLearn::exp(), PLearn::externalProductScaleAcc(), PLearn::TMat< T >::fill(), PLearn::TVec< T >::fill(), i, ignore_weights_below, PLearn::PLearner::inputsize(), PLearn::TVec< T >::length(), PLearn::VMat::length(), log_counts, log_counts2, PLearn::logadd(), PLearn::logOfNormal(), means, PLearn::multiplyAcc(), nclasses, PLearn::perr, pl_log, PLASSERT, pqvec, regularization_sigma, PLearn::TVec< T >::resize(), PLearn::sqrt(), PLearn::square(), PLearn::PLearner::train_set, traininput, trainsample, traintarget_ptr, trainweight_ptr, and PLearn::PLearner::verbosity.
{ int l = train_set.length(); PLASSERT(input.length()==inputsize()); int K = 0; if(computation_neighbors>1) K = int(computation_neighbors); else if(computation_neighbors>0) K = int(computation_neighbors*sqrt(l)); else if(computation_neighbors<0) K = l; if(K>l) K = l; pqvec.resize(K+1); pair<real,int>* pq = pqvec.begin(); int pqsize = 0; log_counts.fill(-FLT_MAX); if(K>0) log_counts2.fill(-FLT_MAX); if(verbosity>=3) perr << "______________________________________" << endl; means.clear(); real ignore_log_weights_below = pl_log(ignore_weights_below); for(int i=0; i<l; i++) { train_set->getRow(i,trainsample); real log_w = computeLogWeight(input, traininput); if(trainweight_ptr) log_w += pl_log(*trainweight_ptr); if(log_w>=ignore_log_weights_below) { if(K>0) { real d = -log_w; if(pqsize<K) { pq[pqsize++] = pair<real,int>(d,i); if(K<l) // need to maintain heap structure only if K<l push_heap(pq,pq+pqsize); } else if(d<pq->first) { pop_heap(pq,pq+pqsize); pq[pqsize-1] = pair<real,int>(d,i); push_heap(pq,pq+pqsize); } } int c = int(*traintarget_ptr); real lcc = log_counts[c]; log_counts[c] = (lcc<ignore_log_weights_below ?log_w :logadd(lcc, log_w)); multiplyAcc(means(c), traininput, exp(log_w)); } } if(verbosity>=3) perr << "log_counts: " << log_counts << endl; for(int c=0; c<nclasses; c++) if(log_counts[c]>=ignore_log_weights_below) means(c) *= exp(-log_counts[c]); allcovars.fill(0.); if(K>0) // compute covars? { for(int k=0; k<pqsize; k++) { int i = pq[k].second; real log_w = -pq[k].first; train_set->getRow(i,trainsample); int c = int(*traintarget_ptr); real lcc = log_counts2[c]; log_counts2[c] = (lcc<ignore_log_weights_below ?log_w :logadd(lcc, log_w)); traininput -= means(c); externalProductScaleAcc(covars[c], traininput, traininput, exp(log_w)); } for(int c=0; c<nclasses; c++) if(log_counts2[c]>=ignore_log_weights_below) covars[c] *= exp(-log_counts2[c]); if(verbosity>=3) perr << "log_counts2: " << log_counts2 << endl; } output.resize(nclasses); output.clear(); for(int c=0; c<nclasses; c++) { if(log_counts[c]<ignore_log_weights_below) output[c] = -FLT_MAX; else { Mat cov = covars[c]; addToDiagonal(cov, square(regularization_sigma)); real log_p_x = logOfNormal(input, means(c), cov); output[c] = log_p_x + log_counts[c]; if(verbosity>=4) { perr << "** Class " << c << " **" << endl; perr << "log_p_x: " << log_p_x << endl; perr << "log_count: " << log_counts[c] << endl; perr << "mean: " << means(c) << endl; perr << "regularized covar: \n" << cov << endl; } } } if(verbosity>=2) { perr << "Scores: " << output << endl; perr << "argmax: " << argmax(output) << endl; } }
void PLearn::LocalGaussianClassifier::declareOptions | ( | OptionList & | ol | ) | [static, protected] |
Declares the class options.
Reimplemented from PLearn::PLearner.
Definition at line 71 of file LocalGaussianClassifier.cc.
References PLearn::OptionBase::buildoption, computation_neighbors, PLearn::declareOption(), PLearn::PLearner::declareOptions(), ignore_weights_below, kernel_sigma, PLearn::OptionBase::learntoption, nclasses, regularization_sigma, and PLearn::PLearner::train_set.
{ // ### Declare all of this object's options here. // ### For the "flags" of each option, you should typically specify // ### one of OptionBase::buildoption, OptionBase::learntoption or // ### OptionBase::tuningoption. If you don't provide one of these three, // ### this option will be ignored when loading values from a script. // ### You can also combine flags, for example with OptionBase::nosave: // ### (OptionBase::buildoption | OptionBase::nosave) // ### ex: // declareOption(ol, "myoption", &LocalGaussianClassifier::myoption, // OptionBase::buildoption, // "Help text describing this option"); // ... declareOption(ol, "nclasses", &LocalGaussianClassifier::nclasses, OptionBase::buildoption, "The number of different classes.\n" "Note that the 'target' part of trining set samples must be an integer\n" "with values between 0 and nclasses-1.\n"); declareOption(ol, "computation_neighbors", &LocalGaussianClassifier::computation_neighbors, OptionBase::buildoption, "This indicates to how many neighbors we should restrict ourselves for the computation\n" "of the covariance matrix only (since they are much cheaper, weight and mean are always\n" "computed using all points.)\n" "If =0 we do not compute a covariance matrix (i.e. use a spherical cov. of width regularization_sigma).\n" "If <0 we use all training points (with an appropriate weight).\n" "If >1 we consider only that many neighbors of the test point;\n" "If between 0 and 1, it's considered a coefficient by which to multiply\n" "the square root of the number of training points, to yield the actual \n" "number of computation neighbors used"); declareOption(ol, "kernel_sigma", &LocalGaussianClassifier::kernel_sigma, OptionBase::buildoption, "The sigma (standard deviation) of the weighting Gaussian Kernel\n"); declareOption(ol, "regularization_sigma", &LocalGaussianClassifier::regularization_sigma, OptionBase::buildoption, "This quantity squared is added to the diagonal of the local empirical covariance matrices.\n"); declareOption(ol, "ignore_weights_below", &LocalGaussianClassifier::ignore_weights_below, OptionBase::buildoption, "minimal weight below which we ignore the point (i.e. consider the weight is 0)\n"); declareOption(ol, "train_set", &LocalGaussianClassifier::train_set, OptionBase::learntoption, "We need to store the training set, as this learner is memory-based..."); /* declareOption(ol, "NN", &LocalGaussianClassifier::NN, OptionBase::learntoption, "The nearest neighbor algorithm used to find nearest neighbors"); */ // Now call the parent class' declareOptions inherited::declareOptions(ol); }
static const PPath& PLearn::LocalGaussianClassifier::declaringFile | ( | ) | [inline, static] |
Reimplemented from PLearn::PLearner.
Definition at line 178 of file LocalGaussianClassifier.h.
:
//##### Protected Options ###############################################
LocalGaussianClassifier * PLearn::LocalGaussianClassifier::deepCopy | ( | CopiesMap & | copies | ) | const [virtual] |
Reimplemented from PLearn::PLearner.
Definition at line 50 of file LocalGaussianClassifier.cc.
void PLearn::LocalGaussianClassifier::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:
Reimplemented from PLearn::PLearner.
Definition at line 171 of file LocalGaussianClassifier.cc.
References PLearn::PLearner::forget().
{ inherited::forget(); }
OptionList & PLearn::LocalGaussianClassifier::getOptionList | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 50 of file LocalGaussianClassifier.cc.
OptionMap & PLearn::LocalGaussianClassifier::getOptionMap | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 50 of file LocalGaussianClassifier.cc.
RemoteMethodMap & PLearn::LocalGaussianClassifier::getRemoteMethodMap | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 50 of file LocalGaussianClassifier.cc.
TVec< string > PLearn::LocalGaussianClassifier::getTestCostNames | ( | ) | const [virtual] |
Returns the names of the costs computed by computeCostsFromOutpus (and thus the test method).
Implements PLearn::PLearner.
Definition at line 379 of file LocalGaussianClassifier.cc.
{ TVec<string> names(2); names[0] = "class_error"; names[1] = "NLL"; return names; }
TVec< string > PLearn::LocalGaussianClassifier::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 387 of file LocalGaussianClassifier.cc.
{
TVec<string> names;
return names;
}
void PLearn::LocalGaussianClassifier::makeDeepCopyFromShallowCopy | ( | CopiesMap & | copies | ) | [virtual] |
Transforms a shallow copy into a deep copy.
Reimplemented from PLearn::PLearner.
Definition at line 152 of file LocalGaussianClassifier.cc.
References PLearn::PLearner::makeDeepCopyFromShallowCopy().
{ inherited::makeDeepCopyFromShallowCopy(copies); // ### Call deepCopyField on all "pointer-like" fields // ### that you wish to be deepCopied rather than // ### shallow-copied. // ### ex: // deepCopyField(trainvec, copies); // deepCopyField(weighting_kernel, copies); // deepCopyField(NN, copies); }
int PLearn::LocalGaussianClassifier::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 166 of file LocalGaussianClassifier.cc.
References nclasses.
{ return nclasses; }
void PLearn::LocalGaussianClassifier::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 221 of file LocalGaussianClassifier.cc.
References allcovars, c, covars, PLearn::PLearner::inputsize(), log_counts, log_counts2, means, nclasses, PLASSERT, PLearn::TMat< T >::resize(), PLearn::TVec< T >::resize(), PLearn::PLearner::setTrainingSet(), PLearn::TMat< T >::subMatRows(), PLearn::TVec< T >::subVec(), PLearn::PLearner::targetsize(), traininput, trainsample, traintarget_ptr, trainweight_ptr, PLearn::PLearner::weightsize(), and PLearn::ws().
Referenced by build_().
{ inherited::setTrainingSet(training_set, call_forget); // int l = train_set.length(); int is = inputsize(); int ts = targetsize(); PLASSERT(ts==1); int ws = weightsize(); PLASSERT(ws==0 || ws==1); trainsample.resize(is+ts+ws); traininput = trainsample.subVec(0,is); traintarget_ptr = &trainsample[is]; trainweight_ptr = NULL; if(ws==1) trainweight_ptr = &trainsample[is+ts]; log_counts.resize(nclasses); log_counts2.resize(nclasses); means.resize(nclasses, is); allcovars.resize(nclasses*is, is); covars.resize(nclasses); for(int c=0; c<nclasses; c++) covars[c] = allcovars.subMatRows(c*is, is); }
void PLearn::LocalGaussianClassifier::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 186 of file LocalGaussianClassifier.cc.
{ // The role of the train method is to bring the learner up to // stage==nstages, updating train_stats with training costs measured // on-line in the process. /* TYPICAL CODE: static Vec input; // static so we don't reallocate 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; // This generic PLearner method does a number of standard stuff useful for // (almost) any learner, and return 'false' if no training should take // place. See PLearner.h for more details. if (!initTrain()) return; while(stage<nstages) { // clear statistics of previous epoch train_stats->forget(); //... train for 1 stage, and update train_stats, // using train_set->getExample(input, target, weight) // and train_stats->update(train_costs) ++stage; train_stats->finalize(); // finalize statistics for this epoch } */ }
Reimplemented from PLearn::PLearner.
Definition at line 178 of file LocalGaussianClassifier.h.
Definition at line 101 of file LocalGaussianClassifier.h.
Referenced by computeOutput(), and setTrainingSet().
Definition at line 83 of file LocalGaussianClassifier.h.
Referenced by computeOutput(), and declareOptions().
TVec<Mat> PLearn::LocalGaussianClassifier::covars [private] |
Definition at line 102 of file LocalGaussianClassifier.h.
Referenced by computeOutput(), and setTrainingSet().
Definition at line 86 of file LocalGaussianClassifier.h.
Referenced by computeOutput(), and declareOptions().
Definition at line 84 of file LocalGaussianClassifier.h.
Referenced by build_(), and declareOptions().
Definition at line 98 of file LocalGaussianClassifier.h.
Referenced by computeOutput(), and setTrainingSet().
Definition at line 99 of file LocalGaussianClassifier.h.
Referenced by computeOutput(), and setTrainingSet().
Mat PLearn::LocalGaussianClassifier::means [private] |
Definition at line 100 of file LocalGaussianClassifier.h.
Referenced by computeOutput(), and setTrainingSet().
Definition at line 89 of file LocalGaussianClassifier.h.
Referenced by build_(), and computeLogWeight().
Definition at line 82 of file LocalGaussianClassifier.h.
Referenced by computeOutput(), declareOptions(), outputsize(), and setTrainingSet().
TVec< pair<real,int> > PLearn::LocalGaussianClassifier::pqvec [mutable, private] |
Definition at line 96 of file LocalGaussianClassifier.h.
Referenced by computeOutput().
Definition at line 85 of file LocalGaussianClassifier.h.
Referenced by computeOutput(), and declareOptions().
Definition at line 93 of file LocalGaussianClassifier.h.
Referenced by computeOutput(), and setTrainingSet().
Global storage to save memory allocations.
Definition at line 92 of file LocalGaussianClassifier.h.
Referenced by computeOutput(), and setTrainingSet().
Definition at line 94 of file LocalGaussianClassifier.h.
Referenced by computeOutput(), and setTrainingSet().
Definition at line 95 of file LocalGaussianClassifier.h.
Referenced by computeOutput(), and setTrainingSet().