PLearn 0.1
Public Member Functions | Static Public Member Functions | Static Public Attributes | Static Protected Member Functions | Protected Attributes | Private Types | Private Member Functions
PLearn::RankLearner Class Reference

#include <RankLearner.h>

Inheritance diagram for PLearn::RankLearner:
Inheritance graph
[legend]
Collaboration diagram for PLearn::RankLearner:
Collaboration graph
[legend]

List of all members.

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 OptionListgetOptionList () const
virtual OptionMapgetOptionMap () const
virtual RemoteMethodMapgetRemoteMethodMap () const
virtual RankLearnerdeepCopy (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 PPathdeclaringFile ()

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< RankedVMatrixranked_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.

Detailed Description

Definition at line 52 of file RankLearner.h.


Member Typedef Documentation

Reimplemented from PLearn::EmbeddedLearner.

Definition at line 56 of file RankLearner.h.


Constructor & Destructor Documentation

PLearn::RankLearner::RankLearner ( )

Default constructor.

Definition at line 52 of file RankLearner.cc.

{}

Member Function Documentation

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.

bool PLearn::RankLearner::_isa_ ( const Object o) [static]

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.

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);
}                                

Here is the call graph for this function:

void PLearn::RankLearner::computeOutput ( const Vec input,
Vec output 
) const [virtual]

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);
}

Here is the call graph for this function:

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);
}

Here is the call graph for this function:

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.

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;
}

Here is the call graph for this function:

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;
}

Here is the call graph for this function:

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().

Here is the call graph for this function:

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;
}
void PLearn::RankLearner::setTrainingSet ( VMat  training_set,
bool  call_forget = true 
) [virtual]

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);
}

Here is the call graph for this function:

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();
}

Here is the call graph for this function:


Member Data Documentation

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.

Used to store the sub-learner's output.

Definition at line 73 of file RankLearner.h.

A vector used to store the desired sub-learner's target when computing the output.

Definition at line 77 of file RankLearner.h.

A pointer to the ranked training set given to the sub-learner.

Definition at line 80 of file RankLearner.h.

Definition at line 64 of file RankLearner.h.

Referenced by declareOptions().


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines