PLearn 0.1
|
#include <BaseRegressorConfidence.h>
Public Member Functions | |
BaseRegressorConfidence () | |
virtual | ~BaseRegressorConfidence () |
virtual string | classname () const |
virtual OptionList & | getOptionList () const |
virtual OptionMap & | getOptionMap () const |
virtual RemoteMethodMap & | getRemoteMethodMap () const |
virtual BaseRegressorConfidence * | deepCopy (CopiesMap &copies) const |
virtual void | makeDeepCopyFromShallowCopy (CopiesMap &copies) |
Transforms a shallow copy into a deep copy. | |
virtual void | build () |
Finish building the object; just call inherited::build followed by build_() | |
virtual void | train () |
*** SUBCLASS WRITING: *** | |
virtual void | forget () |
*** SUBCLASS WRITING: *** | |
virtual int | outputsize () const |
SUBCLASS WRITING: override this so that it returns the size of this learner's output, as a function of its inputsize(), targetsize() and set options. | |
virtual TVec< string > | getTrainCostNames () const |
*** SUBCLASS WRITING: *** | |
virtual TVec< string > | getTestCostNames () const |
*** SUBCLASS WRITING: *** | |
virtual void | computeOutput (const Vec &input, Vec &output) const |
*** SUBCLASS WRITING: *** | |
virtual void | computeOutputAndCosts (const Vec &input, const Vec &target, Vec &output, Vec &costs) const |
Default calls computeOutput and computeCostsFromOutputs. | |
virtual void | computeCostsFromOutputs (const Vec &input, const Vec &output, const Vec &target, Vec &costs) const |
*** SUBCLASS WRITING: *** | |
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 () |
static void | declareOptions (OptionList &ol) |
Declares this class' options. | |
Static Public Attributes | |
static StaticInitializer | _static_initializer_ |
Private Types | |
typedef PLearner | inherited |
Private Member Functions | |
void | build_ () |
**** SUBCLASS WRITING: **** | |
void | verbose (string msg, int level) |
Private Attributes | |
int | number_of_neighbors |
real | sigma |
real | raise_confidence |
real | lower_confidence |
TMat< int > | neighbors |
TVec< real > | nearest_neighbbors_target_mean |
Vec | input_to_search |
Vec | target_to_search |
real | weight_to_search |
Vec | input_to_compare |
Vec | target_to_compare |
real | weight_to_compare |
real | two_sigma_square |
real | root_two_pi_sigma_square |
Definition at line 54 of file BaseRegressorConfidence.h.
typedef PLearner PLearn::BaseRegressorConfidence::inherited [private] |
Reimplemented from PLearn::PLearner.
Definition at line 56 of file BaseRegressorConfidence.h.
PLearn::BaseRegressorConfidence::BaseRegressorConfidence | ( | ) |
Definition at line 52 of file BaseRegressorConfidence.cc.
: number_of_neighbors(1), sigma(0.1), raise_confidence(1.0), lower_confidence(0.0) { }
PLearn::BaseRegressorConfidence::~BaseRegressorConfidence | ( | ) | [virtual] |
Definition at line 60 of file BaseRegressorConfidence.cc.
{ }
string PLearn::BaseRegressorConfidence::_classname_ | ( | ) | [static] |
Reimplemented from PLearn::PLearner.
Definition at line 50 of file BaseRegressorConfidence.cc.
OptionList & PLearn::BaseRegressorConfidence::_getOptionList_ | ( | ) | [static] |
Reimplemented from PLearn::PLearner.
Definition at line 50 of file BaseRegressorConfidence.cc.
RemoteMethodMap & PLearn::BaseRegressorConfidence::_getRemoteMethodMap_ | ( | ) | [static] |
Reimplemented from PLearn::PLearner.
Definition at line 50 of file BaseRegressorConfidence.cc.
Reimplemented from PLearn::PLearner.
Definition at line 50 of file BaseRegressorConfidence.cc.
Object * PLearn::BaseRegressorConfidence::_new_instance_for_typemap_ | ( | ) | [static] |
Reimplemented from PLearn::Object.
Definition at line 50 of file BaseRegressorConfidence.cc.
StaticInitializer BaseRegressorConfidence::_static_initializer_ & PLearn::BaseRegressorConfidence::_static_initialize_ | ( | ) | [static] |
Reimplemented from PLearn::PLearner.
Definition at line 50 of file BaseRegressorConfidence.cc.
void PLearn::BaseRegressorConfidence::build | ( | ) | [virtual] |
Finish building the object; just call inherited::build followed by build_()
Reimplemented from PLearn::PLearner.
Definition at line 93 of file BaseRegressorConfidence.cc.
References PLearn::PLearner::build(), and build_().
{ inherited::build(); build_(); }
void PLearn::BaseRegressorConfidence::build_ | ( | ) | [private] |
**** SUBCLASS WRITING: ****
This method should finish building of the object, according to set 'options', in *any* situation.
Typical situations include:
You can assume that the parent class' build_() has already been called.
A typical build method will want to know the inputsize(), targetsize() and outputsize(), and may also want to check whether train_set->hasWeights(). All these methods require a train_set to be set, so the first thing you may want to do, is check if(train_set), before doing any heavy building...
Note: build() is always called by setTrainingSet.
Reimplemented from PLearn::PLearner.
Definition at line 99 of file BaseRegressorConfidence.cc.
References input_to_compare, input_to_search, PLearn::VMat::length(), nearest_neighbbors_target_mean, neighbors, number_of_neighbors, PLearn::pow(), PLearn::TMat< T >::resize(), PLearn::TVec< T >::resize(), root_two_pi_sigma_square, sigma, target_to_compare, target_to_search, PLearn::PLearner::train_set, and two_sigma_square.
Referenced by build().
{ if (train_set) { input_to_search.resize(train_set->inputsize()); target_to_search.resize(1); input_to_compare.resize(train_set->inputsize()); target_to_compare.resize(1); neighbors.resize(train_set->length(), number_of_neighbors); nearest_neighbbors_target_mean.resize(train_set->length()); two_sigma_square = 2.0 * pow(sigma, 2); root_two_pi_sigma_square = sigma * 2.506628274631; } }
string PLearn::BaseRegressorConfidence::classname | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 50 of file BaseRegressorConfidence.cc.
void PLearn::BaseRegressorConfidence::computeCostsFromOutputs | ( | const Vec & | input, |
const Vec & | output, | ||
const Vec & | target, | ||
Vec & | costs | ||
) | const [virtual] |
*** SUBCLASS WRITING: ***
This should be defined in subclasses to compute the weighted costs from already computed output. The costs should correspond to the cost names returned by getTestCostNames().
NOTE: In exotic cases, the cost may also depend on some info in the input, that's why the method also gets so see it.
Implements PLearn::PLearner.
Definition at line 195 of file BaseRegressorConfidence.cc.
References PLearn::pow().
Referenced by computeOutputAndCosts().
{ costsv[0] = pow((outputv[0] - targetv[0]), 2); }
void PLearn::BaseRegressorConfidence::computeOutput | ( | const Vec & | input, |
Vec & | output | ||
) | const [virtual] |
*** SUBCLASS WRITING: ***
This should be defined in subclasses to compute the output from the input.
Reimplemented from PLearn::PLearner.
Definition at line 165 of file BaseRegressorConfidence.cc.
References PLearn::distance(), PLearn::exp(), PLearn::VMat::getExample(), PLearn::VMat::length(), lower_confidence, nearest_neighbbors_target_mean, PLearn::pow(), PLearn::powdistance(), raise_confidence, PLearn::TVec< T >::resize(), PLearn::PLearner::train_set, and two_sigma_square.
Referenced by computeOutputAndCosts().
{ Vec train_set_inputv; Vec train_set_targetv; real train_set_weight; train_set_inputv.resize(train_set->inputsize()); train_set_targetv.resize(1); real distance = REAL_MAX; int nearest_neighbor = -1; for (int row = 0; row < train_set->length(); row++) { train_set->getExample(row, train_set_inputv, train_set_targetv, train_set_weight); if (powdistance(inputv, train_set_inputv) < distance) { distance = powdistance(inputv, train_set_inputv); nearest_neighbor = row; } } outputv[1] = exp(-1.0 * pow((outputv[0] - nearest_neighbbors_target_mean[nearest_neighbor]), 2) / two_sigma_square); // / root_two_pi_sigma_square? outputv[0] = nearest_neighbbors_target_mean[nearest_neighbor]; if (outputv[1] >= raise_confidence) outputv[1] = 1.0; if (outputv[1] < lower_confidence) outputv[1] = 0.0; }
void PLearn::BaseRegressorConfidence::computeOutputAndCosts | ( | const Vec & | input, |
const Vec & | target, | ||
Vec & | output, | ||
Vec & | costs | ||
) | const [virtual] |
Default calls computeOutput and computeCostsFromOutputs.
You may override this if you have a more efficient way to compute both output and weighted costs at the same time.
Reimplemented from PLearn::PLearner.
Definition at line 189 of file BaseRegressorConfidence.cc.
References computeCostsFromOutputs(), and computeOutput().
{ computeOutput(inputv, outputv); computeCostsFromOutputs(inputv, outputv, targetv, costsv); }
void PLearn::BaseRegressorConfidence::declareOptions | ( | OptionList & | ol | ) | [static] |
Declares this class' options.
Reimplemented from PLearn::PLearner.
Definition at line 64 of file BaseRegressorConfidence.cc.
References PLearn::OptionBase::buildoption, PLearn::declareOption(), PLearn::PLearner::declareOptions(), PLearn::OptionBase::learntoption, lower_confidence, nearest_neighbbors_target_mean, neighbors, number_of_neighbors, raise_confidence, and sigma.
{ declareOption(ol, "number_of_neighbors", &BaseRegressorConfidence::number_of_neighbors, OptionBase::buildoption, "The number of nearest neighbors to consider.\n"); declareOption(ol, "sigma", &BaseRegressorConfidence::sigma, OptionBase::buildoption, "The variance of the distribution on the target.\n"); declareOption(ol, "raise_confidence", &BaseRegressorConfidence::raise_confidence, OptionBase::buildoption, "If the computed confidence is greater or equal to this level, it will be raised to 1.0.\n"); declareOption(ol, "lower_confidence", &BaseRegressorConfidence::lower_confidence, OptionBase::buildoption, "If the computed confidence is lower than this level, it will be lowered to 0.0.\n"); declareOption(ol, "neighbors", &BaseRegressorConfidence::neighbors, OptionBase::learntoption, "The matrice of indices of nearest neighbors rows from the training set.\n"); declareOption(ol, "nearest_neighbbors_target_mean", &BaseRegressorConfidence::nearest_neighbbors_target_mean, OptionBase::learntoption, "The vector of neairest neighborstarget means from the trainingset.\n");; inherited::declareOptions(ol); }
static const PPath& PLearn::BaseRegressorConfidence::declaringFile | ( | ) | [inline, static] |
Reimplemented from PLearn::PLearner.
Definition at line 94 of file BaseRegressorConfidence.h.
: void build_();
BaseRegressorConfidence * PLearn::BaseRegressorConfidence::deepCopy | ( | CopiesMap & | copies | ) | const [virtual] |
Reimplemented from PLearn::PLearner.
Definition at line 50 of file BaseRegressorConfidence.cc.
void PLearn::BaseRegressorConfidence::forget | ( | ) | [virtual] |
*** SUBCLASS WRITING: ***
(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!)
A typical forget() method should do the following:
This method is typically called by the build_() method, after it has finished setting up the parameters, and if it deemed useful to set or reset the learner in its fresh state. (remember build may be called after modifying options that do not necessarily require the learner to restart from a fresh state...) forget is also called by the setTrainingSet method, after calling build(), so it will generally be called TWICE during setTrainingSet!
Reimplemented from PLearn::PLearner.
Definition at line 144 of file BaseRegressorConfidence.cc.
{ }
OptionList & PLearn::BaseRegressorConfidence::getOptionList | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 50 of file BaseRegressorConfidence.cc.
OptionMap & PLearn::BaseRegressorConfidence::getOptionMap | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 50 of file BaseRegressorConfidence.cc.
RemoteMethodMap & PLearn::BaseRegressorConfidence::getRemoteMethodMap | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 50 of file BaseRegressorConfidence.cc.
TVec< string > PLearn::BaseRegressorConfidence::getTestCostNames | ( | ) | const [virtual] |
*** SUBCLASS WRITING: ***
This should return the names of the costs computed by computeCostsFromOutputs.
Implements PLearn::PLearner.
Definition at line 160 of file BaseRegressorConfidence.cc.
References getTrainCostNames().
{ return getTrainCostNames(); }
TVec< string > PLearn::BaseRegressorConfidence::getTrainCostNames | ( | ) | const [virtual] |
*** SUBCLASS WRITING: ***
This should return 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 153 of file BaseRegressorConfidence.cc.
Referenced by getTestCostNames().
{ TVec<string> return_msg(1); return_msg[0] = "mse"; return return_msg; }
void PLearn::BaseRegressorConfidence::makeDeepCopyFromShallowCopy | ( | CopiesMap & | copies | ) | [virtual] |
Transforms a shallow copy into a deep copy.
Reimplemented from PLearn::PLearner.
Definition at line 82 of file BaseRegressorConfidence.cc.
References PLearn::deepCopyField(), lower_confidence, PLearn::PLearner::makeDeepCopyFromShallowCopy(), nearest_neighbbors_target_mean, neighbors, number_of_neighbors, raise_confidence, and sigma.
{ inherited::makeDeepCopyFromShallowCopy(copies); deepCopyField(number_of_neighbors, copies); deepCopyField(sigma, copies); deepCopyField(raise_confidence, copies); deepCopyField(lower_confidence, copies); deepCopyField(neighbors, copies); deepCopyField(nearest_neighbbors_target_mean, copies); }
int PLearn::BaseRegressorConfidence::outputsize | ( | ) | const [virtual] |
SUBCLASS WRITING: override this so that it returns the size of this learner's output, as a function of its inputsize(), targetsize() and set options.
Implements PLearn::PLearner.
Definition at line 148 of file BaseRegressorConfidence.cc.
{
return 2;
}
void PLearn::BaseRegressorConfidence::train | ( | ) | [virtual] |
*** SUBCLASS WRITING: ***
The role of the train method is to bring the learner up to stage==nstages, updating the stats with training costs measured on-line in the process.
TYPICAL CODE:
static Vec input; // static so we don't reallocate/deallocate 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; if(!train_stats) // make a default stats collector, in case there's none train_stats = new VecStatsCollector(); if(nstages<stage) // asking to revert to a previous stage! forget(); // reset the learner to stage=0 while(stage<nstages) { // clear statistics of previous epoch train_stats->forget(); //... train for 1 stage, and update train_stats, // using train_set->getSample(input, target, weight); // and train_stats->update(train_costs) ++stage; train_stats->finalize(); // finalize statistics for this epoch }
Implements PLearn::PLearner.
Definition at line 114 of file BaseRegressorConfidence.cc.
References PLearn::BottomNI< T >::getBottomN(), PLearn::VMat::getExample(), input_to_compare, input_to_search, PLearn::VMat::length(), nearest_neighbbors_target_mean, neighbors, number_of_neighbors, PLearn::powdistance(), PLearn::BottomNI< T >::sort(), target_to_compare, target_to_search, PLearn::PLearner::train_set, PLearn::BottomNI< T >::update(), weight_to_compare, and weight_to_search.
{ for(int row_to_search = 0; row_to_search < train_set.length(); row_to_search++) { BottomNI<real> neighbors_search(number_of_neighbors); nearest_neighbbors_target_mean[row_to_search] = 0.0; train_set->getExample(row_to_search, input_to_search, target_to_search, weight_to_search); for(int row_to_compare = 0; row_to_compare < train_set.length(); row_to_compare++) { train_set.getExample(row_to_compare, input_to_compare, target_to_compare, weight_to_compare); neighbors_search.update(powdistance(input_to_search, input_to_compare), row_to_compare); } neighbors_search.sort(); for(int row_to_compare = 0; row_to_compare < number_of_neighbors; row_to_compare++) { TVec< pair<real,int> > indices = neighbors_search.getBottomN(); neighbors(row_to_search, row_to_compare) = indices[row_to_compare].second; train_set->getExample(neighbors(row_to_search, row_to_compare), input_to_compare, target_to_compare, weight_to_compare); nearest_neighbbors_target_mean[row_to_search] += target_to_compare[0]; } nearest_neighbbors_target_mean[row_to_search] = nearest_neighbbors_target_mean[row_to_search] / number_of_neighbors; } }
void PLearn::BaseRegressorConfidence::verbose | ( | string | msg, |
int | level | ||
) | [private] |
Definition at line 138 of file BaseRegressorConfidence.cc.
References PLearn::endl(), and PLearn::PLearner::verbosity.
Reimplemented from PLearn::PLearner.
Definition at line 94 of file BaseRegressorConfidence.h.
Definition at line 83 of file BaseRegressorConfidence.h.
Definition at line 80 of file BaseRegressorConfidence.h.
Definition at line 67 of file BaseRegressorConfidence.h.
Referenced by computeOutput(), declareOptions(), and makeDeepCopyFromShallowCopy().
Definition at line 74 of file BaseRegressorConfidence.h.
Referenced by build_(), computeOutput(), declareOptions(), makeDeepCopyFromShallowCopy(), and train().
TMat<int> PLearn::BaseRegressorConfidence::neighbors [private] |
Definition at line 73 of file BaseRegressorConfidence.h.
Referenced by build_(), declareOptions(), makeDeepCopyFromShallowCopy(), and train().
Definition at line 64 of file BaseRegressorConfidence.h.
Referenced by build_(), declareOptions(), makeDeepCopyFromShallowCopy(), and train().
Definition at line 66 of file BaseRegressorConfidence.h.
Referenced by computeOutput(), declareOptions(), and makeDeepCopyFromShallowCopy().
Definition at line 87 of file BaseRegressorConfidence.h.
Referenced by build_().
real PLearn::BaseRegressorConfidence::sigma [private] |
Definition at line 65 of file BaseRegressorConfidence.h.
Referenced by build_(), declareOptions(), and makeDeepCopyFromShallowCopy().
Definition at line 84 of file BaseRegressorConfidence.h.
Definition at line 81 of file BaseRegressorConfidence.h.
Definition at line 86 of file BaseRegressorConfidence.h.
Referenced by build_(), and computeOutput().
Definition at line 85 of file BaseRegressorConfidence.h.
Referenced by train().
Definition at line 82 of file BaseRegressorConfidence.h.
Referenced by train().