PLearn 0.1
|
#include <RankLearner.h>
Public Member Functions | |
RankLearner () | |
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 RankLearner * | 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 | 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 void | computeOutputAndCosts (const Vec &input, const Vec &target, Vec &output, Vec &costs) const |
Overridden because no costs are computed (see help). | |
virtual TVec< std::string > | getTestCostNames () const |
Returns the names of the costs computed by computeCostsFromOutpus (and thus the test method). | |
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 void | setTrainingSet (VMat training_set, bool call_forget=true) |
Overridden to sort the targets by rank and provide the modified training set to the underlying learner. | |
virtual void | train () |
Overridden so as to learn the 'sorted_targets' option. | |
Static Public Member Functions | |
static string | _classname_ () |
Declares name and deepCopy methods. | |
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 Public Attributes | |
static StaticInitializer | _static_initializer_ |
Static Protected Member Functions | |
static void | declareOptions (OptionList &ol) |
Declares this class' options. | |
Protected Attributes | |
Vec | sorted_targets |
Vec | last_output |
Used to store the last output computed, in order not to have to recompute the sub-learner's output in computeCostsFromOutputs(). | |
Vec | learner_output |
Used to store the sub-learner's output. | |
Vec | learner_target |
A vector used to store the desired sub-learner's target when computing the output. | |
PP< RankedVMatrix > | ranked_trainset |
A pointer to the ranked training set given to the sub-learner. | |
Private Types | |
typedef EmbeddedLearner | inherited |
Private Member Functions | |
void | build_ () |
This does the actual building. |
Definition at line 52 of file RankLearner.h.
typedef EmbeddedLearner PLearn::RankLearner::inherited [private] |
Reimplemented from PLearn::EmbeddedLearner.
Definition at line 56 of file RankLearner.h.
PLearn::RankLearner::RankLearner | ( | ) |
string PLearn::RankLearner::_classname_ | ( | ) | [static] |
Declares name and deepCopy methods.
Reimplemented from PLearn::EmbeddedLearner.
Definition at line 67 of file RankLearner.cc.
OptionList & PLearn::RankLearner::_getOptionList_ | ( | ) | [static] |
Reimplemented from PLearn::EmbeddedLearner.
Definition at line 67 of file RankLearner.cc.
RemoteMethodMap & PLearn::RankLearner::_getRemoteMethodMap_ | ( | ) | [static] |
Reimplemented from PLearn::EmbeddedLearner.
Definition at line 67 of file RankLearner.cc.
Reimplemented from PLearn::EmbeddedLearner.
Definition at line 67 of file RankLearner.cc.
Object * PLearn::RankLearner::_new_instance_for_typemap_ | ( | ) | [static] |
Reimplemented from PLearn::EmbeddedLearner.
Definition at line 67 of file RankLearner.cc.
StaticInitializer RankLearner::_static_initializer_ & PLearn::RankLearner::_static_initialize_ | ( | ) | [static] |
Reimplemented from PLearn::EmbeddedLearner.
Definition at line 67 of file RankLearner.cc.
void PLearn::RankLearner::build | ( | ) | [virtual] |
Simply calls inherited::build() then build_().
Reimplemented from PLearn::EmbeddedLearner.
Definition at line 93 of file RankLearner.cc.
{ inherited::build(); build_(); }
void PLearn::RankLearner::build_ | ( | ) | [private] |
This does the actual building.
Reimplemented from PLearn::EmbeddedLearner.
Definition at line 102 of file RankLearner.cc.
{ if (learner_ && learner_->outputsize() >= 0) { learner_output.resize(learner_->outputsize()); } // The sub-learner's target is a rank, thus of dimension 1. learner_target.resize(1); // Currently, only works with 1-dimensional targets. last_output.resize(1); }
string PLearn::RankLearner::classname | ( | ) | const [virtual] |
Reimplemented from PLearn::EmbeddedLearner.
Definition at line 67 of file RankLearner.cc.
void PLearn::RankLearner::computeCostsFromOutputs | ( | const Vec & | input, |
const Vec & | output, | ||
const Vec & | target, | ||
Vec & | costs | ||
) | const [virtual] |
Computes the costs from already computed output.
Reimplemented from PLearn::EmbeddedLearner.
Definition at line 116 of file RankLearner.cc.
References PLearn::fast_exact_is_equal(), PLearn::left(), n, PLERROR, and PLearn::right().
{ static real desired_rank, val, frac; static int n, left, right, mid; // Find the desired rank. val = target[0]; n = sorted_targets.length(); if (val <= sorted_targets[0]) // Lowest than all targets. desired_rank = 0; else if (val >= sorted_targets[n - 1]) // Highest than all targets. desired_rank = n-1; else { // Looking for the closest targets by binary search. left = 0; right = n - 1; while (right > left + 1) { mid = (left + right) / 2; if (val < sorted_targets[mid]) right = mid; else left = mid; } if (right == left){ if (left == n - 1) left--; else right++; } frac = sorted_targets[right] - sorted_targets[left]; if (frac < 1e-30) // Equal targets, up to numerical precision. desired_rank = left; else desired_rank = left + (val - sorted_targets[left]) / frac; } learner_target[0] = desired_rank; if (!fast_exact_is_equal(last_output[0], output[0])) // This case is not handled yet. PLERROR("In RankLearner::computeCostsFromOutputs - Currently, one can only use computeCostsFromOutputs() " "after calling computeOutput."); // In this case, the sub-learner's output is the last one computed in computeOutput(). learner_->computeCostsFromOutputs(input, learner_output, learner_target, costs); }
Computes the output from the input.
Reimplemented from PLearn::EmbeddedLearner.
Definition at line 166 of file RankLearner.cc.
References PLERROR.
{ static real val; static int rank_inf; learner_->computeOutput(input, learner_output); #ifdef BOUNDCHECK // Safety check to ensure we are only working with 1-dimensional targets. if (learner_output.length() != 1) PLERROR("In RankLearner::computeOutput - Ranking can only work with 1-dimensional targets"); #endif val = learner_output[0]; if (val <= 0) output[0] = sorted_targets[0]; else if (val >= sorted_targets.length() - 1) output[0] = sorted_targets[sorted_targets.length() - 1]; else { rank_inf = int(val); output[0] = sorted_targets[rank_inf] + (val - rank_inf) * (sorted_targets[rank_inf + 1] - sorted_targets[rank_inf]); } last_output[0] = output[0]; }
void PLearn::RankLearner::computeOutputAndCosts | ( | const Vec & | input, |
const Vec & | target, | ||
Vec & | output, | ||
Vec & | costs | ||
) | const [virtual] |
Overridden because no costs are computed (see help).
Reimplemented from PLearn::EmbeddedLearner.
Definition at line 191 of file RankLearner.cc.
References PLearn::PLearner::computeOutputAndCosts().
{ // TODO Optimize to take advantage of the sub-learner's method. PLearner::computeOutputAndCosts(input, target, output, costs); }
void PLearn::RankLearner::declareOptions | ( | OptionList & | ol | ) | [static, protected] |
Declares this class' options.
Reimplemented from PLearn::EmbeddedLearner.
Definition at line 72 of file RankLearner.cc.
References PLearn::declareOption(), PLearn::OptionBase::learntoption, and sorted_targets.
{ // Build options. // declareOption(ol, "myoption", &RankLearner::myoption, OptionBase::buildoption, // "Help text describing this option"); // ... // Learnt options. declareOption(ol, "sorted_targets", &RankLearner::sorted_targets, OptionBase::learntoption, "The sorted targets of the training set."); // Now call the parent class' declareOptions. inherited::declareOptions(ol); }
static const PPath& PLearn::RankLearner::declaringFile | ( | ) | [inline, static] |
Reimplemented from PLearn::EmbeddedLearner.
Definition at line 122 of file RankLearner.h.
RankLearner * PLearn::RankLearner::deepCopy | ( | CopiesMap & | copies | ) | const [virtual] |
Reimplemented from PLearn::EmbeddedLearner.
Definition at line 67 of file RankLearner.cc.
void PLearn::RankLearner::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::EmbeddedLearner.
Definition at line 200 of file RankLearner.cc.
{ inherited::forget(); sorted_targets.resize(0); }
OptionList & PLearn::RankLearner::getOptionList | ( | ) | const [virtual] |
Reimplemented from PLearn::EmbeddedLearner.
Definition at line 67 of file RankLearner.cc.
OptionMap & PLearn::RankLearner::getOptionMap | ( | ) | const [virtual] |
Reimplemented from PLearn::EmbeddedLearner.
Definition at line 67 of file RankLearner.cc.
RemoteMethodMap & PLearn::RankLearner::getRemoteMethodMap | ( | ) | const [virtual] |
Reimplemented from PLearn::EmbeddedLearner.
Definition at line 67 of file RankLearner.cc.
TVec< string > PLearn::RankLearner::getTestCostNames | ( | ) | const [virtual] |
Returns the names of the costs computed by computeCostsFromOutpus (and thus the test method).
See help.
Reimplemented from PLearn::EmbeddedLearner.
Definition at line 209 of file RankLearner.cc.
References i, and PLearn::TVec< T >::length().
{ // Add 'learner.' in front of the sub-learner's costs. TVec<string> learner_costs = learner_->getTestCostNames(); TVec<string> costs(learner_costs.length()); for (int i = 0; i < costs.length(); i++) costs[i] = "learner." + learner_costs[i]; return costs; }
TVec< string > PLearn::RankLearner::getTrainCostNames | ( | ) | const [virtual] |
Returns the names of the objective costs that the train method computes and for which it updates the VecStatsCollector train_stats.
See help.
Reimplemented from PLearn::EmbeddedLearner.
Definition at line 222 of file RankLearner.cc.
References i, and PLearn::TVec< T >::length().
{ // Add 'learner.' in front of the sub-learner's costs. TVec<string> learner_costs = learner_->getTrainCostNames(); TVec<string> costs(learner_costs.length()); for (int i = 0; i < costs.length(); i++) costs[i] = "learner." + learner_costs[i]; return costs; }
void PLearn::RankLearner::makeDeepCopyFromShallowCopy | ( | CopiesMap & | copies | ) | [virtual] |
Transforms a shallow copy into a deep copy.
Reimplemented from PLearn::EmbeddedLearner.
Definition at line 235 of file RankLearner.cc.
References PLearn::deepCopyField().
{ inherited::makeDeepCopyFromShallowCopy(copies); deepCopyField(sorted_targets, copies); deepCopyField(last_output, copies); deepCopyField(learner_output, copies); deepCopyField(learner_target, copies); deepCopyField(ranked_trainset, copies); }
int PLearn::RankLearner::outputsize | ( | ) | const [virtual] |
Returns the size of this learner's output, (which typically may depend on its inputsize(), targetsize() and set options).
Reimplemented from PLearn::EmbeddedLearner.
Definition at line 248 of file RankLearner.cc.
{ // The outputsize is the usual outputsize (the one from the training set). // Currently this can only be one, because we only deal with real targets // (they are easier to sort). return 1; }
Overridden to sort the targets by rank and provide the modified training set to the underlying learner.
Reimplemented from PLearn::EmbeddedLearner.
Definition at line 259 of file RankLearner.cc.
References PLearn::PLearner::setTrainingSet().
{ // Some stuff similar to EmbeddedLearner. bool training_set_has_changed = !train_set || !(train_set->looksTheSameAs(training_set)); ranked_trainset = new RankedVMatrix(training_set); learner_->setTrainingSet((RankedVMatrix *) ranked_trainset, false); if (call_forget && !training_set_has_changed) learner_->build(); // Resize work variable. if (learner_->outputsize() >= 0) learner_output.resize(learner_->outputsize()); PLearner::setTrainingSet(training_set, call_forget); }
void PLearn::RankLearner::train | ( | ) | [virtual] |
Overridden so as to learn the 'sorted_targets' option.
Reimplemented from PLearn::EmbeddedLearner.
Definition at line 275 of file RankLearner.cc.
References PLearn::TMat< T >::column(), PLearn::TMat< T >::length(), and PLearn::TMat< T >::resize().
{ // Remember the sorted targets, because we will need them for prediction. Mat mat_sorted_targets = ranked_trainset->getSortedTargets().column(0); sorted_targets.resize(mat_sorted_targets.length()); sorted_targets << mat_sorted_targets; inherited::train(); }
Reimplemented from PLearn::EmbeddedLearner.
Definition at line 122 of file RankLearner.h.
Vec PLearn::RankLearner::last_output [mutable, protected] |
Used to store the last output computed, in order not to have to recompute the sub-learner's output in computeCostsFromOutputs().
Definition at line 70 of file RankLearner.h.
Vec PLearn::RankLearner::learner_output [mutable, protected] |
Used to store the sub-learner's output.
Definition at line 73 of file RankLearner.h.
Vec PLearn::RankLearner::learner_target [protected] |
A vector used to store the desired sub-learner's target when computing the output.
Definition at line 77 of file RankLearner.h.
PP<RankedVMatrix> PLearn::RankLearner::ranked_trainset [protected] |
A pointer to the ranked training set given to the sub-learner.
Definition at line 80 of file RankLearner.h.
Vec PLearn::RankLearner::sorted_targets [protected] |
Definition at line 64 of file RankLearner.h.
Referenced by declareOptions().