PLearn 0.1
|
#include <BaseRegressorWrapper.h>
Public Member Functions | |
BaseRegressorWrapper () | |
virtual | ~BaseRegressorWrapper () |
virtual string | classname () const |
virtual OptionList & | getOptionList () const |
virtual OptionMap & | getOptionMap () const |
virtual RemoteMethodMap & | getRemoteMethodMap () const |
virtual BaseRegressorWrapper * | 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: *** | |
void | setSortedTrainSet (PP< RegressionTreeRegisters > the_sorted_train_set) |
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 | |
real | loss_function_weight |
int | mean_imputation |
int | regression_tree |
int | use_confidence_function |
int | use_base_regressor_confidence |
PP< PLearner > | base_regressor_template |
PP< RegressionTree > | tree_regressor_template |
PP< BaseRegressorConfidence > | confidence_function_template |
PP< RegressionTreeRegisters > | sorted_train_set |
PP< PLearner > | base_regressor |
PP< RegressionTree > | tree_regressor |
PP< BaseRegressorConfidence > | confidence_function |
VMat | base_regressor_train_set |
TVec< real > | variable_means |
Definition at line 55 of file BaseRegressorWrapper.h.
typedef PLearner PLearn::BaseRegressorWrapper::inherited [private] |
Reimplemented from PLearn::PLearner.
Definition at line 57 of file BaseRegressorWrapper.h.
PLearn::BaseRegressorWrapper::BaseRegressorWrapper | ( | ) |
Definition at line 62 of file BaseRegressorWrapper.cc.
: loss_function_weight(1.0), mean_imputation(0), regression_tree(0), use_confidence_function(0), use_base_regressor_confidence(0) { }
PLearn::BaseRegressorWrapper::~BaseRegressorWrapper | ( | ) | [virtual] |
Definition at line 71 of file BaseRegressorWrapper.cc.
{ }
string PLearn::BaseRegressorWrapper::_classname_ | ( | ) | [static] |
Reimplemented from PLearn::PLearner.
Definition at line 60 of file BaseRegressorWrapper.cc.
OptionList & PLearn::BaseRegressorWrapper::_getOptionList_ | ( | ) | [static] |
Reimplemented from PLearn::PLearner.
Definition at line 60 of file BaseRegressorWrapper.cc.
RemoteMethodMap & PLearn::BaseRegressorWrapper::_getRemoteMethodMap_ | ( | ) | [static] |
Reimplemented from PLearn::PLearner.
Definition at line 60 of file BaseRegressorWrapper.cc.
Reimplemented from PLearn::PLearner.
Definition at line 60 of file BaseRegressorWrapper.cc.
Object * PLearn::BaseRegressorWrapper::_new_instance_for_typemap_ | ( | ) | [static] |
Reimplemented from PLearn::Object.
Definition at line 60 of file BaseRegressorWrapper.cc.
StaticInitializer BaseRegressorWrapper::_static_initializer_ & PLearn::BaseRegressorWrapper::_static_initialize_ | ( | ) | [static] |
Reimplemented from PLearn::PLearner.
Definition at line 60 of file BaseRegressorWrapper.cc.
void PLearn::BaseRegressorWrapper::build | ( | ) | [virtual] |
Finish building the object; just call inherited::build followed by build_()
Reimplemented from PLearn::PLearner.
Definition at line 128 of file BaseRegressorWrapper.cc.
References PLearn::PLearner::build(), and build_().
{ inherited::build(); build_(); }
void PLearn::BaseRegressorWrapper::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 134 of file BaseRegressorWrapper.cc.
References base_regressor, base_regressor_template, base_regressor_train_set, confidence_function, confidence_function_template, PLearn::deepCopy(), loss_function_weight, mean_imputation, regression_tree, sorted_train_set, PLearn::tostring(), PLearn::PLearner::train_set, tree_regressor, tree_regressor_template, use_confidence_function, and variable_means.
Referenced by build().
{ if (train_set) { if (mean_imputation > 0) { PP<MeanImputationVMatrix> new_train_set = new MeanImputationVMatrix(train_set, 0.0); variable_means = new_train_set->getMeanVector(); base_regressor_train_set = VMat(new_train_set); } else { base_regressor_train_set = train_set; } if (regression_tree > 0) { tree_regressor = ::PLearn::deepCopy(tree_regressor_template); tree_regressor->setTrainingSet(VMat(sorted_train_set)); tree_regressor->setOption("loss_function_weight", tostring(loss_function_weight)); base_regressor = tree_regressor; } else { base_regressor = ::PLearn::deepCopy(base_regressor_template); } base_regressor->setTrainingSet(base_regressor_train_set, true); base_regressor->setTrainStatsCollector(new VecStatsCollector); if (use_confidence_function > 0) { confidence_function = ::PLearn::deepCopy(confidence_function_template); confidence_function->setTrainingSet(base_regressor_train_set, true); confidence_function->setTrainStatsCollector(new VecStatsCollector); confidence_function->train(); } } }
string PLearn::BaseRegressorWrapper::classname | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 60 of file BaseRegressorWrapper.cc.
void PLearn::BaseRegressorWrapper::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 265 of file BaseRegressorWrapper.cc.
References loss_function_weight, and PLearn::pow().
Referenced by computeOutputAndCosts().
{ costsv[0] = pow((outputv[0] - targetv[0]), 2); costsv[1] = outputv[1]; costsv[2] = 1.0 - (2.0 * loss_function_weight * costsv[0]); }
void PLearn::BaseRegressorWrapper::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 222 of file BaseRegressorWrapper.cc.
References base_regressor, confidence_function, PLearn::is_missing(), mean_imputation, PLearn::TVec< T >::resize(), PLearn::PLearner::train_set, use_base_regressor_confidence, use_confidence_function, and variable_means.
Referenced by computeOutputAndCosts().
{ Vec base_regressor_inputv; Vec base_regressor_outputv; Vec confidence_outputv; base_regressor_inputv.resize(train_set->inputsize()); base_regressor_outputv.resize(base_regressor->outputsize()); for (int variable = 0; variable < train_set->inputsize(); variable++) { base_regressor_inputv[variable] = inputv[variable]; if (mean_imputation > 0 && is_missing(inputv[variable]) ) base_regressor_inputv[variable] = variable_means[variable]; } base_regressor->computeOutput(base_regressor_inputv, base_regressor_outputv); outputv[0] = base_regressor_outputv[0]; // cout << "base regressor output: " << outputv[0]; if (use_base_regressor_confidence > 0) { outputv[1] = base_regressor_outputv[1]; } else { if (use_confidence_function > 0) { confidence_outputv.resize(confidence_function->outputsize()); confidence_outputv[0] = base_regressor_outputv[0]; confidence_function->computeOutput(base_regressor_inputv, confidence_outputv); outputv[1] = confidence_outputv[1]; } else { outputv[1] = 1.0; } } // cout << "confidence output: " << confidence_outputv[0]; // cout << " confidence: " << outputv[1] << endl; }
void PLearn::BaseRegressorWrapper::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 259 of file BaseRegressorWrapper.cc.
References computeCostsFromOutputs(), and computeOutput().
{ computeOutput(inputv, outputv); computeCostsFromOutputs(inputv, outputv, targetv, costsv); }
void PLearn::BaseRegressorWrapper::declareOptions | ( | OptionList & | ol | ) | [static] |
Declares this class' options.
Reimplemented from PLearn::PLearner.
Definition at line 75 of file BaseRegressorWrapper.cc.
References base_regressor, base_regressor_template, base_regressor_train_set, PLearn::OptionBase::buildoption, confidence_function, confidence_function_template, PLearn::declareOption(), PLearn::PLearner::declareOptions(), PLearn::OptionBase::learntoption, loss_function_weight, mean_imputation, regression_tree, sorted_train_set, tree_regressor_template, use_base_regressor_confidence, use_confidence_function, and variable_means.
{ declareOption(ol, "loss_function_weight", &BaseRegressorWrapper::loss_function_weight, OptionBase::buildoption, "The hyper parameter to balance the error and the confidence factor.\n"); declareOption(ol, "mean_imputation", &BaseRegressorWrapper::mean_imputation, OptionBase::buildoption, "If set to 1, the algorithm will compute the mean vector of the input variables from the training set,\n" "and replace missing values with the computed means.\n"); declareOption(ol, "regression_tree", &BaseRegressorWrapper::regression_tree, OptionBase::buildoption, "If set to 1, the tree_regressor_template is used instead of the base_regressor_template.\n" "It permits to sort the train set only once for all boosting iterations.\n"); declareOption(ol, "use_confidence_function", &BaseRegressorWrapper::use_confidence_function, OptionBase::buildoption, "If set to 1, the confidence_function_template is used to build a confidence estimates on the base regressor prediction.\n" "Otherwise, if the confidence from the base regressor is not used, the confidence is always set to 1.0.\n"); declareOption(ol, "use_base_regressor_confidence", &BaseRegressorWrapper::use_base_regressor_confidence, OptionBase::buildoption, "If set to 1, the confidence is taken from the second output of the base regressor.\n"); declareOption(ol, "base_regressor_template", &BaseRegressorWrapper::base_regressor_template, OptionBase::buildoption, "The template for the base regressor to be boosted.\n"); declareOption(ol, "tree_regressor_template", &BaseRegressorWrapper::tree_regressor_template, OptionBase::buildoption, "The template for a RegressionTree base regressor when the regression_tree option is set to 1.\n"); declareOption(ol, "confidence_function_template", &BaseRegressorWrapper::confidence_function_template, OptionBase::buildoption, "The template for the confidence function to be learnt from the train set.\n"); declareOption(ol, "sorted_train_set", &BaseRegressorWrapper::sorted_train_set, OptionBase::buildoption, "A sorted train set when using a tree as a base regressor\n"); declareOption(ol, "base_regressor", &BaseRegressorWrapper::base_regressor, OptionBase::learntoption, "The base regressor built from the template.\n"); declareOption(ol, "confidence_function", &BaseRegressorWrapper::confidence_function, OptionBase::learntoption, "The confidence function learnt from the train set.\n"); declareOption(ol, "base_regressor_train_set", &BaseRegressorWrapper::base_regressor_train_set, OptionBase::learntoption, "The VMat train set prepared for the base regressor.\n" "It apllies the chosen missing value management strategies.\n"); declareOption(ol, "variable_means", &BaseRegressorWrapper::variable_means, OptionBase::learntoption, "The vector with the computed means on all input dimension to perform mean imputation if applicable.\n"); inherited::declareOptions(ol); }
static const PPath& PLearn::BaseRegressorWrapper::declaringFile | ( | ) | [inline, static] |
Reimplemented from PLearn::PLearner.
Definition at line 96 of file BaseRegressorWrapper.h.
: void build_();
BaseRegressorWrapper * PLearn::BaseRegressorWrapper::deepCopy | ( | CopiesMap & | copies | ) | const [virtual] |
Reimplemented from PLearn::PLearner.
Definition at line 60 of file BaseRegressorWrapper.cc.
void PLearn::BaseRegressorWrapper::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 199 of file BaseRegressorWrapper.cc.
{ }
OptionList & PLearn::BaseRegressorWrapper::getOptionList | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 60 of file BaseRegressorWrapper.cc.
OptionMap & PLearn::BaseRegressorWrapper::getOptionMap | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 60 of file BaseRegressorWrapper.cc.
RemoteMethodMap & PLearn::BaseRegressorWrapper::getRemoteMethodMap | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 60 of file BaseRegressorWrapper.cc.
TVec< string > PLearn::BaseRegressorWrapper::getTestCostNames | ( | ) | const [virtual] |
*** SUBCLASS WRITING: ***
This should return the names of the costs computed by computeCostsFromOutputs.
Implements PLearn::PLearner.
Definition at line 217 of file BaseRegressorWrapper.cc.
References getTrainCostNames().
{ return getTrainCostNames(); }
TVec< string > PLearn::BaseRegressorWrapper::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 208 of file BaseRegressorWrapper.cc.
Referenced by getTestCostNames().
{ TVec<string> return_msg(3); return_msg[0] = "mse"; return_msg[1] = "base confidence"; return_msg[2] = "base reward"; return return_msg; }
void PLearn::BaseRegressorWrapper::makeDeepCopyFromShallowCopy | ( | CopiesMap & | copies | ) | [virtual] |
Transforms a shallow copy into a deep copy.
Reimplemented from PLearn::PLearner.
Definition at line 111 of file BaseRegressorWrapper.cc.
References base_regressor, base_regressor_template, base_regressor_train_set, confidence_function, confidence_function_template, PLearn::deepCopyField(), loss_function_weight, PLearn::PLearner::makeDeepCopyFromShallowCopy(), mean_imputation, regression_tree, sorted_train_set, tree_regressor_template, use_base_regressor_confidence, and use_confidence_function.
{ inherited::makeDeepCopyFromShallowCopy(copies); deepCopyField(loss_function_weight, copies); deepCopyField(mean_imputation, copies); deepCopyField(regression_tree, copies); deepCopyField(use_confidence_function, copies); deepCopyField(use_base_regressor_confidence, copies); deepCopyField(base_regressor_template, copies); deepCopyField(tree_regressor_template, copies); deepCopyField(confidence_function_template, copies); deepCopyField(sorted_train_set, copies); deepCopyField(base_regressor, copies); deepCopyField(confidence_function, copies); deepCopyField(base_regressor_train_set, copies); }
int PLearn::BaseRegressorWrapper::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 203 of file BaseRegressorWrapper.cc.
{
return 2;
}
void PLearn::BaseRegressorWrapper::setSortedTrainSet | ( | PP< RegressionTreeRegisters > | the_sorted_train_set | ) |
Definition at line 272 of file BaseRegressorWrapper.cc.
References sorted_train_set.
{ sorted_train_set = the_sorted_train_set; }
void PLearn::BaseRegressorWrapper::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 171 of file BaseRegressorWrapper.cc.
References base_regressor.
{ base_regressor->train(); /* cout << "testing the confidence function" << endl; Vec train_input; Vec train_target; Vec train_output; real train_weight; train_input->resize(train_set->inputsize()); train_target->resize(train_set->targetsize()); train_output->resize(2); for (int row = 0; row < 25; row++) { train_set->getExample(row, train_input, train_target, train_weight); cout << "row: " << row << " target: " << train_target[0]; computeOutput(train_input, train_output); } PLERROR("We are done for now"); */ }
void PLearn::BaseRegressorWrapper::verbose | ( | string | msg, |
int | level | ||
) | [private] |
Definition at line 193 of file BaseRegressorWrapper.cc.
References PLearn::endl(), and PLearn::PLearner::verbosity.
Reimplemented from PLearn::PLearner.
Definition at line 96 of file BaseRegressorWrapper.h.
Definition at line 80 of file BaseRegressorWrapper.h.
Referenced by build_(), computeOutput(), declareOptions(), makeDeepCopyFromShallowCopy(), and train().
Definition at line 70 of file BaseRegressorWrapper.h.
Referenced by build_(), declareOptions(), and makeDeepCopyFromShallowCopy().
Definition at line 83 of file BaseRegressorWrapper.h.
Referenced by build_(), declareOptions(), and makeDeepCopyFromShallowCopy().
Definition at line 82 of file BaseRegressorWrapper.h.
Referenced by build_(), computeOutput(), declareOptions(), and makeDeepCopyFromShallowCopy().
Definition at line 72 of file BaseRegressorWrapper.h.
Referenced by build_(), declareOptions(), and makeDeepCopyFromShallowCopy().
Definition at line 65 of file BaseRegressorWrapper.h.
Referenced by build_(), computeCostsFromOutputs(), declareOptions(), and makeDeepCopyFromShallowCopy().
Definition at line 66 of file BaseRegressorWrapper.h.
Referenced by build_(), computeOutput(), declareOptions(), and makeDeepCopyFromShallowCopy().
Definition at line 67 of file BaseRegressorWrapper.h.
Referenced by build_(), declareOptions(), and makeDeepCopyFromShallowCopy().
Definition at line 73 of file BaseRegressorWrapper.h.
Referenced by build_(), declareOptions(), makeDeepCopyFromShallowCopy(), and setSortedTrainSet().
Definition at line 81 of file BaseRegressorWrapper.h.
Referenced by build_().
Definition at line 71 of file BaseRegressorWrapper.h.
Referenced by build_(), declareOptions(), and makeDeepCopyFromShallowCopy().
Definition at line 69 of file BaseRegressorWrapper.h.
Referenced by computeOutput(), declareOptions(), and makeDeepCopyFromShallowCopy().
Definition at line 68 of file BaseRegressorWrapper.h.
Referenced by build_(), computeOutput(), declareOptions(), and makeDeepCopyFromShallowCopy().
TVec<real> PLearn::BaseRegressorWrapper::variable_means [private] |
Definition at line 84 of file BaseRegressorWrapper.h.
Referenced by build_(), computeOutput(), and declareOptions().