PLearn 0.1
|
#include <BinaryStump.h>
Public Member Functions | |
BinaryStump () | |
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 OptionList & | getOptionList () const |
virtual OptionMap & | getOptionMap () const |
virtual RemoteMethodMap & | getRemoteMethodMap () const |
virtual BinaryStump * | deepCopy (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 PPath & | declaringFile () |
Public Attributes | |
bool | one_hot_output |
Static Public Attributes | |
static StaticInitializer | _static_initializer_ |
Static Protected Member Functions | |
static void | declareOptions (OptionList &ol) |
Declares this class' options. | |
Protected Attributes | |
int | feature |
int | tag |
real | threshold |
Private Types | |
typedef PLearner | inherited |
Private Member Functions | |
void | build_ () |
This does the actual building. | |
Private Attributes | |
TVec< pair< int, real > > | sf |
Definition at line 52 of file BinaryStump.h.
typedef PLearner PLearn::BinaryStump::inherited [private] |
Reimplemented from PLearn::PLearner.
Definition at line 57 of file BinaryStump.h.
PLearn::BinaryStump::BinaryStump | ( | ) |
Default constructor.
Definition at line 130 of file BinaryStump.cc.
: feature(0), tag(0), threshold(0), one_hot_output(false) {}
string PLearn::BinaryStump::_classname_ | ( | ) | [static] |
Reimplemented from PLearn::PLearner.
Definition at line 145 of file BinaryStump.cc.
OptionList & PLearn::BinaryStump::_getOptionList_ | ( | ) | [static] |
Reimplemented from PLearn::PLearner.
Definition at line 145 of file BinaryStump.cc.
RemoteMethodMap & PLearn::BinaryStump::_getRemoteMethodMap_ | ( | ) | [static] |
Reimplemented from PLearn::PLearner.
Definition at line 145 of file BinaryStump.cc.
Reimplemented from PLearn::PLearner.
Definition at line 145 of file BinaryStump.cc.
Object * PLearn::BinaryStump::_new_instance_for_typemap_ | ( | ) | [static] |
Reimplemented from PLearn::Object.
Definition at line 145 of file BinaryStump.cc.
StaticInitializer BinaryStump::_static_initializer_ & PLearn::BinaryStump::_static_initialize_ | ( | ) | [static] |
Reimplemented from PLearn::PLearner.
Definition at line 145 of file BinaryStump.cc.
void PLearn::BinaryStump::build | ( | ) | [virtual] |
Simply calls inherited::build() then build_().
Reimplemented from PLearn::PLearner.
Definition at line 172 of file BinaryStump.cc.
References PLearn::PLearner::build(), and build_().
{ inherited::build(); build_(); }
void PLearn::BinaryStump::build_ | ( | ) | [private] |
This does the actual building.
Reimplemented from PLearn::PLearner.
Definition at line 170 of file BinaryStump.cc.
Referenced by build().
{}
string PLearn::BinaryStump::classname | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 145 of file BinaryStump.cc.
void PLearn::BinaryStump::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 409 of file BinaryStump.cc.
References PLearn::argmin(), PLearn::fast_exact_is_equal(), PLearn::is_equal(), one_hot_output, PLERROR, and PLearn::TVec< T >::resize().
{ costs.resize(1); if(!fast_exact_is_equal(target[0], 0) && !fast_exact_is_equal(target[0], 1)) PLERROR("In BinaryStump:computeCostsFromOutputs() : " "target should be either 1 or 0"); real predict = one_hot_output ? argmin(output) : output[0]; costs[0] = !is_equal(predict, target[0]); }
Computes the output from the input.
Reimplemented from PLearn::PLearner.
Definition at line 395 of file BinaryStump.cc.
References feature, one_hot_output, outputsize(), PLearn::TVec< T >::resize(), tag, and threshold.
{ output.resize(outputsize()); int predict = input[feature] < threshold ? tag : 1 - tag; if (one_hot_output) { output[predict] = 1; output[1 - predict] = 0; } else output[0] = predict; }
void PLearn::BinaryStump::declareOptions | ( | OptionList & | ol | ) | [static, protected] |
Declares this class' options.
Reimplemented from PLearn::PLearner.
Definition at line 150 of file BinaryStump.cc.
References PLearn::OptionBase::buildoption, PLearn::declareOption(), PLearn::PLearner::declareOptions(), feature, PLearn::OptionBase::learntoption, one_hot_output, tag, and threshold.
{ declareOption(ol, "one_hot_output", &BinaryStump::one_hot_output, OptionBase::buildoption, "If set to 1, the output will be a two-dimensional one-hot vector\n" "instead of just a single number."); declareOption(ol, "feature", &BinaryStump::feature, OptionBase::learntoption, "Feature tested by the stump"); declareOption(ol, "threshold", &BinaryStump::threshold, OptionBase::learntoption, "Threshold for decision"); declareOption(ol, "tag", &BinaryStump::tag, OptionBase::learntoption, "Tag assigned when feature is lower than the threshold"); inherited::declareOptions(ol); }
static const PPath& PLearn::BinaryStump::declaringFile | ( | ) | [inline, static] |
Reimplemented from PLearn::PLearner.
Definition at line 120 of file BinaryStump.h.
BinaryStump * PLearn::BinaryStump::deepCopy | ( | CopiesMap & | copies | ) | const [virtual] |
Reimplemented from PLearn::PLearner.
Definition at line 145 of file BinaryStump.cc.
void PLearn::BinaryStump::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 202 of file BinaryStump.cc.
References feature, PLearn::PLearner::stage, tag, and threshold.
OptionList & PLearn::BinaryStump::getOptionList | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 145 of file BinaryStump.cc.
OptionMap & PLearn::BinaryStump::getOptionMap | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 145 of file BinaryStump.cc.
RemoteMethodMap & PLearn::BinaryStump::getRemoteMethodMap | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 145 of file BinaryStump.cc.
TVec< string > PLearn::BinaryStump::getTestCostNames | ( | ) | const [virtual] |
Returns the names of the costs computed by computeCostsFromOutpus (and thus the test method).
Implements PLearn::PLearner.
Definition at line 426 of file BinaryStump.cc.
References getTrainCostNames().
{ return getTrainCostNames(); }
TVec< string > PLearn::BinaryStump::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 434 of file BinaryStump.cc.
References PLearn::TVec< T >::append(), and PLearn::TVec< T >::isEmpty().
Referenced by getTestCostNames().
{ static TVec<string> costs; if (costs.isEmpty()) costs.append("binary_class_error"); return costs; }
void PLearn::BinaryStump::makeDeepCopyFromShallowCopy | ( | CopiesMap & | copies | ) | [virtual] |
Transforms a shallow copy into a deep copy.
Reimplemented from PLearn::PLearner.
Definition at line 182 of file BinaryStump.cc.
References PLearn::PLearner::makeDeepCopyFromShallowCopy().
{ inherited::makeDeepCopyFromShallowCopy(copies); }
int PLearn::BinaryStump::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 191 of file BinaryStump.cc.
References one_hot_output.
Referenced by computeOutput().
{ if (one_hot_output) return 2; else return 1; }
void PLearn::BinaryStump::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 213 of file BinaryStump.cc.
References d, PLearn::endl(), PLearn::fast_exact_is_equal(), feature, PLearn::TVec< T >::fill(), PLearn::TVec< T >::first(), i, PLearn::PLearner::inputsize(), PLearn::PLearner::inputsize_, j, PLearn::TVec< T >::length(), PLearn::VMat::length(), n, PLERROR, PLearn::qsort_vec(), PLearn::PLearner::report_progress, PLearn::TVec< T >::resize(), PLearn::safeflog(), sf, tag, PLearn::PLearner::targetsize_, threshold, PLearn::PLearner::train_set, PLearn::PLearner::train_stats, and PLearn::PLearner::verbosity.
{ if(!train_set) PLERROR("In BinaryStump:train() : train_set not specified"); if(!train_stats) // make a default stats collector, in case there's none train_stats = new VecStatsCollector(); train_stats->forget(); int n = train_set->length(); sf.resize(n); //static Vec input; input.resize(inputsize()); //static Vec target; target.resize(targetsize()); real input; //real weight; Vec train_target(n); TVec< pair<int,int> > buffer((int)(n*safeflog(n))); static Vec example_weights; example_weights.resize(n); // Extracting weights if(train_set->weightsize() > 0) { for (int i=0; i<n; ++i) { //train_set->getExample(i, input, target, weight); //example_weights[i]=weight; example_weights[i]= train_set->get(i,inputsize_+targetsize_); } } else { example_weights.fill(1.0/n); } for (int i=0; i<n; ++i) { train_target[i]= train_set->get(i,inputsize_); if(!fast_exact_is_equal(train_target[i], 0) && !fast_exact_is_equal(train_target[i], 1)) PLERROR("In BinaryStump:train() : target should be either 1 or 0"); } // Choosing best stump real best_error = 0; { real w_sum_1 = 0; real w_sum_error = 0; real w_sum = 0; for(int i=0; i<n; i++) { w_sum += example_weights[i]; //train_set->getExample(i,input,target,weight); //if(target[0] == 1) if(fast_exact_is_equal(train_target[i], 1)) w_sum_1 += example_weights[i]; } if(w_sum_1 > w_sum - w_sum_1) { tag = 0; w_sum_error = w_sum - w_sum_1; } else { tag = 1; w_sum_error = w_sum_1; } best_error = w_sum_error; // We choose as the first stump to consider, the stump that classifies // in the most frequent class // every points which have their first coordinate greater than // the smallest value for this coordinate in the training set MINUS ONE. // This approximatly corresponds to classify any points to the most // frequent class. feature = 0; threshold = sf[0].second-1; // TODO Why? (done below already?) PP<ProgressBar> pb; if(report_progress) pb = new ProgressBar("Finding best stump",inputsize()*sf.length()); int prog = 0; for(int d=0; d<inputsize(); d++) { // Copying input for(int j=0; j<n; j++) { //train_set->getExample(j,input, target, weight); input = train_set->get(j,d); //if(target[0] != 0 & target[0] != 1) sf[j].first = j; //sf[j].second = input[d]; sf[j].second = input; } // Sorting features //for(int i=0; i<sf.length();i++) qsort_vec(sf,buffer); if(d==0) { // initialize threshold threshold = sf[0].second-1; DBG_MODULE_LOG << "Initializing threshold <- " << threshold << endl; } real w_sum_l_1 = 0; real w_sum_l = 0; for(int i=0; i<sf.length()-1; i++) { real f1 = sf[i].second; real f2 = sf[i+1].second; //train_set->getExample(sf[i].first,input,target,weight); //target = train_set->getExample(sf[i].first,inputsize_); //real classe = target[0]; real classe = train_target[sf[i].first]; if(fast_exact_is_equal(classe, 1)) w_sum_l_1+=example_weights[sf[i].first]; w_sum_l += example_weights[sf[i].first]; if(fast_exact_is_equal(f1, f2)) continue; real w_sum_error_1 = w_sum_l - w_sum_l_1 + w_sum_1 - w_sum_l_1; real c_w_sum_error = 0; if(w_sum_error_1 > w_sum - w_sum_error_1) { c_w_sum_error = w_sum - w_sum_error_1; } else { c_w_sum_error = w_sum_error_1; } // We choose the first stump that minimizes the // weighted error. if (best_error > c_w_sum_error) { best_error = c_w_sum_error; tag = w_sum_error_1 > w_sum - w_sum_error_1 ? 0 : 1; threshold = (f1+f2)/2; DBG_MODULE_LOG << "Updating treshold <- " << threshold << " (c_w_sum_error = " << c_w_sum_error << ", best_error = " << best_error << ")" << endl; feature = d; } else { DBG_MODULE_LOG << "No update (c_w_sum_error = " << c_w_sum_error << ", best_error = " << best_error << ")" << endl; } } prog++; if(report_progress) pb->update(prog); } } Vec costs(1); costs[0] = best_error; train_stats->update(costs); train_stats->finalize(); if(verbosity > 1) cout << "Weighted error = " << best_error << endl; sf = TVec< pair<int, real> >(0); }
Reimplemented from PLearn::PLearner.
Definition at line 120 of file BinaryStump.h.
int PLearn::BinaryStump::feature [protected] |
Definition at line 66 of file BinaryStump.h.
Referenced by computeOutput(), declareOptions(), forget(), and train().
Definition at line 76 of file BinaryStump.h.
Referenced by computeCostsFromOutputs(), computeOutput(), declareOptions(), and outputsize().
TVec< pair<int, real> > PLearn::BinaryStump::sf [private] |
Definition at line 58 of file BinaryStump.h.
Referenced by train().
int PLearn::BinaryStump::tag [protected] |
Definition at line 67 of file BinaryStump.h.
Referenced by computeOutput(), declareOptions(), forget(), and train().
real PLearn::BinaryStump::threshold [protected] |
Definition at line 68 of file BinaryStump.h.
Referenced by computeOutput(), declareOptions(), forget(), and train().