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

#include <RegressionTreeMulticlassLeave.h>

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

List of all members.

Public Member Functions

 RegressionTreeMulticlassLeave ()
virtual ~RegressionTreeMulticlassLeave ()
virtual string classname () const
virtual OptionListgetOptionList () const
virtual OptionMapgetOptionMap () const
virtual RemoteMethodMapgetRemoteMethodMap () const
virtual
RegressionTreeMulticlassLeave
deepCopy (CopiesMap &copies) const
virtual void makeDeepCopyFromShallowCopy (CopiesMap &copies)
 Does the necessary operations to transform a shallow copy (this) into a deep copy by deep-copying all the members that need to be.
virtual void build ()
 Post-constructor.
void initStats ()
void addRow (int row)
void addRow (int row, real target, real weight)
void addRow (int row, Vec outputv, Vec errorv)
void addRow (int row, real target, real weight, Vec outputv, Vec errorv)
void removeRow (int row, real target, real weight)
void removeRow (int row, Vec outputv, Vec errorv)
void removeRow (int row, real target, real weight, Vec outputv, Vec errorv)
void getOutputAndError (Vec &output, Vec &error) const
TVec< string > getOutputNames () const
void printStats ()
virtual void addLeave (PP< RegressionTreeLeave > leave)
virtual void removeLeave (PP< RegressionTreeLeave > leave)

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 PPathdeclaringFile ()
static void declareOptions (OptionList &ol)
 Declare options (data fields) for the class.

Static Public Attributes

static StaticInitializer _static_initializer_

Private Types

typedef RegressionTreeLeave inherited

Private Member Functions

void build_ ()
 Object-specific post-constructor.

Private Attributes

Vec multiclass_outputs
string objective_function
real l1_loss_function_factor
real l2_loss_function_factor
Vec multiclass_weights_sum

Detailed Description

Definition at line 50 of file RegressionTreeMulticlassLeave.h.


Member Typedef Documentation

Reimplemented from PLearn::RegressionTreeLeave.

Definition at line 52 of file RegressionTreeMulticlassLeave.h.


Constructor & Destructor Documentation

PLearn::RegressionTreeMulticlassLeave::RegressionTreeMulticlassLeave ( )

Definition at line 55 of file RegressionTreeMulticlassLeave.cc.

References build().

    : objective_function("l1")
{
    build();
}

Here is the call graph for this function:

PLearn::RegressionTreeMulticlassLeave::~RegressionTreeMulticlassLeave ( ) [virtual]

Definition at line 61 of file RegressionTreeMulticlassLeave.cc.

{
}

Member Function Documentation

string PLearn::RegressionTreeMulticlassLeave::_classname_ ( ) [static]

Reimplemented from PLearn::RegressionTreeLeave.

Definition at line 53 of file RegressionTreeMulticlassLeave.cc.

OptionList & PLearn::RegressionTreeMulticlassLeave::_getOptionList_ ( ) [static]

Reimplemented from PLearn::RegressionTreeLeave.

Definition at line 53 of file RegressionTreeMulticlassLeave.cc.

RemoteMethodMap & PLearn::RegressionTreeMulticlassLeave::_getRemoteMethodMap_ ( ) [static]

Reimplemented from PLearn::RegressionTreeLeave.

Definition at line 53 of file RegressionTreeMulticlassLeave.cc.

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

Reimplemented from PLearn::RegressionTreeLeave.

Definition at line 53 of file RegressionTreeMulticlassLeave.cc.

Object * PLearn::RegressionTreeMulticlassLeave::_new_instance_for_typemap_ ( ) [static]

Reimplemented from PLearn::RegressionTreeLeave.

Definition at line 53 of file RegressionTreeMulticlassLeave.cc.

StaticInitializer RegressionTreeMulticlassLeave::_static_initializer_ & PLearn::RegressionTreeMulticlassLeave::_static_initialize_ ( ) [static]

Reimplemented from PLearn::RegressionTreeLeave.

Definition at line 53 of file RegressionTreeMulticlassLeave.cc.

void PLearn::RegressionTreeMulticlassLeave::addLeave ( PP< RegressionTreeLeave leave) [virtual]

Reimplemented from PLearn::RegressionTreeLeave.

Definition at line 279 of file RegressionTreeMulticlassLeave.cc.

References classname(), PLearn::RegressionTreeLeave::length_, multiclass_weights_sum, PLERROR, and PLearn::RegressionTreeLeave::weights_sum.

                                                                          {
    PP<RegressionTreeMulticlassLeave> leave = (PP<RegressionTreeMulticlassLeave>) leave_;
    if(leave->classname() == classname()){
        length_ += leave->length_;
        weights_sum += leave->weights_sum;
        multiclass_weights_sum += leave->multiclass_weights_sum;
    }else
        PLERROR("In %s::addLeave the leave to add should have the same class. It have %s.",
                classname().c_str(), leave->classname().c_str());
}

Here is the call graph for this function:

void PLearn::RegressionTreeMulticlassLeave::addRow ( int  row) [virtual]

Reimplemented from PLearn::RegressionTreeLeave.

Definition at line 132 of file RegressionTreeMulticlassLeave.cc.

References PLearn::RegressionTreeLeave::train_set.

Referenced by addRow().

{
    real weight = train_set->getWeight(row);
    real target = train_set->getTarget(row);
    addRow(row, target, weight);
}

Here is the caller graph for this function:

void PLearn::RegressionTreeMulticlassLeave::addRow ( int  row,
real  target,
real  weight 
) [virtual]

Reimplemented from PLearn::RegressionTreeLeave.

Definition at line 146 of file RegressionTreeMulticlassLeave.cc.

References PLearn::TVec< T >::length(), PLearn::RegressionTreeLeave::length_, multiclass_outputs, multiclass_weights_sum, PLERROR, and PLearn::RegressionTreeLeave::weights_sum.

{
    length_ += 1;
    weights_sum += weight;
    int multiclass_found = 0;
    //if target are 0,1,2,... it can be optimized by multiclass_weights_sum[target]
    //for the general case: by using a table with index being the target and the value the needed index
    for (int mc_ind = 0; mc_ind < multiclass_outputs.length(); mc_ind++)
    {
        if (target == multiclass_outputs[mc_ind])
        {
            multiclass_weights_sum[mc_ind] += weight;
            multiclass_found = 1;      
            break;      
        }
    }
    if (multiclass_found < 1) 
        PLERROR("RegressionTreeMultilassLeave: Unknown target: %g row: %d\n",
                target, row);
}

Here is the call graph for this function:

void PLearn::RegressionTreeMulticlassLeave::addRow ( int  row,
Vec  outputv,
Vec  errorv 
) [virtual]

Reimplemented from PLearn::RegressionTreeLeave.

Definition at line 167 of file RegressionTreeMulticlassLeave.cc.

References addRow(), and getOutputAndError().

{
    addRow(row);
    getOutputAndError(outputv,errorv);    
}

Here is the call graph for this function:

void PLearn::RegressionTreeMulticlassLeave::addRow ( int  row,
real  target,
real  weight,
Vec  outputv,
Vec  errorv 
) [virtual]

Reimplemented from PLearn::RegressionTreeLeave.

Definition at line 139 of file RegressionTreeMulticlassLeave.cc.

References addRow(), and getOutputAndError().

{
    addRow(row, target, weight);
    getOutputAndError(outputv,errorv);
}

Here is the call graph for this function:

void PLearn::RegressionTreeMulticlassLeave::build ( ) [virtual]

Post-constructor.

The normal implementation should call simply inherited::build(), then this class's build_(). This method should be callable again at later times, after modifying some option fields to change the "architecture" of the object.

Reimplemented from PLearn::RegressionTreeLeave.

Definition at line 104 of file RegressionTreeMulticlassLeave.cc.

References PLearn::RegressionTreeLeave::build(), and build_().

Referenced by RegressionTreeMulticlassLeave().

Here is the call graph for this function:

Here is the caller graph for this function:

void PLearn::RegressionTreeMulticlassLeave::build_ ( ) [private]

Object-specific post-constructor.

This method should be redefined in subclasses and do the actual building of the object according to previously set option fields. Constructors can just set option fields, and then call build_. This method is NOT virtual, and will typically be called only from three places: a constructor, the public virtual build() method, and possibly the public virtual read method (which calls its parent's read). build_() can assume that its parent's build_() has already been called.

Reimplemented from PLearn::RegressionTreeLeave.

Definition at line 110 of file RegressionTreeMulticlassLeave.cc.

Referenced by build().

{
}

Here is the caller graph for this function:

string PLearn::RegressionTreeMulticlassLeave::classname ( ) const [virtual]

Reimplemented from PLearn::RegressionTreeLeave.

Definition at line 53 of file RegressionTreeMulticlassLeave.cc.

Referenced by addLeave(), and removeLeave().

Here is the caller graph for this function:

void PLearn::RegressionTreeMulticlassLeave::declareOptions ( OptionList ol) [static]

Declare options (data fields) for the class.

Redefine this in subclasses: call declareOption(...) for each option, and then call inherited::declareOptions(options). Please call the inherited method AT THE END to get the options listed in a consistent order (from most recently defined to least recently defined).

  static void MyDerivedClass::declareOptions(OptionList& ol)
  {
      declareOption(ol, "inputsize", &MyObject::inputsize_,
                    OptionBase::buildoption,
                    "The size of the input; it must be provided");
      declareOption(ol, "weights", &MyObject::weights,
                    OptionBase::learntoption,
                    "The learned model weights");
      inherited::declareOptions(ol);
  }
Parameters:
olList of options that is progressively being constructed for the current class.

Reimplemented from PLearn::RegressionTreeLeave.

Definition at line 65 of file RegressionTreeMulticlassLeave.cc.

References PLearn::OptionBase::buildoption, PLearn::declareOption(), PLearn::RegressionTreeLeave::declareOptions(), l1_loss_function_factor, l2_loss_function_factor, PLearn::OptionBase::learntoption, multiclass_outputs, multiclass_weights_sum, and objective_function.

{ 
    declareOption(ol, "multiclass_outputs", 
                  &RegressionTreeMulticlassLeave::multiclass_outputs,
                  OptionBase::buildoption,
                  "A vector of possible output values when solving a multiclass problem.\n"
                  "The leave will output the value with the largest weight sum.");
    declareOption(ol, "objective_function",
                  &RegressionTreeMulticlassLeave::objective_function,
                  OptionBase::buildoption,
                  "The function to be used to compute the leave error.\n"
                  "Current supported values are l1 and l2 (default is l1).");
      
    declareOption(ol, "multiclass_weights_sum",
                  &RegressionTreeMulticlassLeave::multiclass_weights_sum,
                  OptionBase::learntoption,
                  "A vector to count the weight sum of each possible output "
                  "for the sample in this leave.\n");
    declareOption(ol, "l1_loss_function_factor",
                  &RegressionTreeMulticlassLeave::l1_loss_function_factor,
                  OptionBase::learntoption,
                  "2 / loss_function_weight.\n");
    declareOption(ol, "l2_loss_function_factor",
                  &RegressionTreeMulticlassLeave::l2_loss_function_factor,
                  OptionBase::learntoption,
                  "2 / pow(loss_function_weight, 2.0).\n");
    inherited::declareOptions(ol);
}

Here is the call graph for this function:

static const PPath& PLearn::RegressionTreeMulticlassLeave::declaringFile ( ) [inline, static]

Reimplemented from PLearn::RegressionTreeLeave.

Definition at line 74 of file RegressionTreeMulticlassLeave.h.

:
    void         build_();
RegressionTreeMulticlassLeave * PLearn::RegressionTreeMulticlassLeave::deepCopy ( CopiesMap copies) const [virtual]

Reimplemented from PLearn::RegressionTreeLeave.

Definition at line 53 of file RegressionTreeMulticlassLeave.cc.

OptionList & PLearn::RegressionTreeMulticlassLeave::getOptionList ( ) const [virtual]

Reimplemented from PLearn::RegressionTreeLeave.

Definition at line 53 of file RegressionTreeMulticlassLeave.cc.

OptionMap & PLearn::RegressionTreeMulticlassLeave::getOptionMap ( ) const [virtual]

Reimplemented from PLearn::RegressionTreeLeave.

Definition at line 53 of file RegressionTreeMulticlassLeave.cc.

void PLearn::RegressionTreeMulticlassLeave::getOutputAndError ( Vec output,
Vec error 
) const [virtual]

Reimplemented from PLearn::RegressionTreeLeave.

Definition at line 207 of file RegressionTreeMulticlassLeave.cc.

References PLearn::abs(), PLearn::TVec< T >::clear(), l1_loss_function_factor, l2_loss_function_factor, PLearn::TVec< T >::length(), PLearn::RegressionTreeLeave::length_, PLearn::RegressionTreeLeave::missing_leave, MISSING_VALUE, multiclass_outputs, multiclass_weights_sum, objective_function, PLearn::RegressionTreeLeave::output_confidence_target, PLERROR, PLearn::pow(), RTR_HAVE_MISSING, and PLearn::RegressionTreeLeave::weights_sum.

Referenced by addRow(), printStats(), and removeRow().

{
#ifdef BOUNDCHECK
    if(multiclass_outputs.length()<=0)
        PLERROR("In RegressionTreeMulticlassLeave::getOutputAndError() -"
                " multiclass_outputs must not be empty");
#endif
    if(length_==0){        
        output.clear();
        output[0]=MISSING_VALUE;
        error.clear();
        return;
    }
    real conf = 0;
    int mc_winer = 0;
    //index of the max. Is their an optimized version?
    for (int mc_ind = 1; mc_ind < multiclass_outputs.length(); mc_ind++)
    {
        if (multiclass_weights_sum[mc_ind] > multiclass_weights_sum[mc_winer])
            mc_winer = mc_ind;
    }
    output[0] = multiclass_outputs[mc_winer];
    if (RTR_HAVE_MISSING && missing_leave)
    {
        error[0] = 0.0;
        error[1] = weights_sum;
        error[2] = 0.0;
    }
    else
    {
        conf = multiclass_weights_sum[mc_winer] / weights_sum;;
        error[0] = 0.0;
        if (objective_function == "l1")
        {
            for (int mc_ind = 0; mc_ind < multiclass_outputs.length();mc_ind++)
            {
                error[0] += abs(output[0] - multiclass_outputs[mc_ind]) 
                    * multiclass_weights_sum[mc_ind];
            }
            error[0] *= l1_loss_function_factor * length_ / weights_sum;
            if (error[0] < 1E-10) error[0] = 0.0;
            if (error[0] > weights_sum * l1_loss_function_factor)
                error[2] = weights_sum * l1_loss_function_factor;
            else error[2] = error[0];
        }
        else
        {
            for (int mc_ind = 0; mc_ind < multiclass_outputs.length();mc_ind++)
            {
                error[0] += pow(output[0] - multiclass_outputs[mc_ind], 2) 
                    * multiclass_weights_sum[mc_ind];
            }
            error[0] *= l2_loss_function_factor * length_ / weights_sum;
            if (error[0] < 1E-10) error[0] = 0.0;
            if (error[0] > weights_sum * l2_loss_function_factor) 
                error[2] = weights_sum * l2_loss_function_factor; 
            else error[2] = error[0];
        }
        error[1] = (1.0 - conf) * length_;
    }
    if(output_confidence_target) output[1] = conf;
}

Here is the call graph for this function:

Here is the caller graph for this function:

TVec< string > PLearn::RegressionTreeMulticlassLeave::getOutputNames ( ) const [virtual]

Reimplemented from PLearn::RegressionTreeLeave.

Definition at line 270 of file RegressionTreeMulticlassLeave.cc.

References PLearn::TVec< T >::append(), and PLearn::RegressionTreeLeave::output_confidence_target.

{
    TVec<string> ret;
    ret.append("class_pred");
    if(output_confidence_target)
        ret.append("confidence");
    return ret;
}

Here is the call graph for this function:

RemoteMethodMap & PLearn::RegressionTreeMulticlassLeave::getRemoteMethodMap ( ) const [virtual]

Reimplemented from PLearn::RegressionTreeLeave.

Definition at line 53 of file RegressionTreeMulticlassLeave.cc.

void PLearn::RegressionTreeMulticlassLeave::initStats ( ) [virtual]
void PLearn::RegressionTreeMulticlassLeave::makeDeepCopyFromShallowCopy ( CopiesMap copies) [virtual]

Does the necessary operations to transform a shallow copy (this) into a deep copy by deep-copying all the members that need to be.

This needs to be overridden by every class that adds "complex" data members to the class, such as Vec, Mat, PP<Something>, etc. Typical implementation:

  void CLASS_OF_THIS::makeDeepCopyFromShallowCopy(CopiesMap& copies)
  {
      inherited::makeDeepCopyFromShallowCopy(copies);
      deepCopyField(complex_data_member1, copies);
      deepCopyField(complex_data_member2, copies);
      ...
  }
Parameters:
copiesA map used by the deep-copy mechanism to keep track of already-copied objects.

Reimplemented from PLearn::RegressionTreeLeave.

Definition at line 94 of file RegressionTreeMulticlassLeave.cc.

References PLearn::deepCopyField(), l1_loss_function_factor, l2_loss_function_factor, PLearn::RegressionTreeLeave::makeDeepCopyFromShallowCopy(), multiclass_outputs, multiclass_weights_sum, and objective_function.

Here is the call graph for this function:

void PLearn::RegressionTreeMulticlassLeave::printStats ( ) [virtual]

Reimplemented from PLearn::RegressionTreeLeave.

Definition at line 301 of file RegressionTreeMulticlassLeave.cc.

References PLearn::endl(), getOutputAndError(), PLearn::RegressionTreeLeave::length_, multiclass_weights_sum, and PLearn::RegressionTreeLeave::weights_sum.

{
    cout << " l " << length_;
    Vec output(2);
    Vec error(3);
    getOutputAndError(output,error);
    cout << " o0 " << output[0];
    cout << " o1 " << output[1];
    cout << " e0 " << error[0];
    cout << " e1 " << error[1];
    cout << " ws " << weights_sum;
    cout << endl;
    cout << " mws " << multiclass_weights_sum << endl;
}

Here is the call graph for this function:

void PLearn::RegressionTreeMulticlassLeave::removeLeave ( PP< RegressionTreeLeave leave) [virtual]

Reimplemented from PLearn::RegressionTreeLeave.

Definition at line 290 of file RegressionTreeMulticlassLeave.cc.

References classname(), PLearn::RegressionTreeLeave::length_, multiclass_weights_sum, PLERROR, and PLearn::RegressionTreeLeave::weights_sum.

                                                                             {
    PP<RegressionTreeMulticlassLeave> leave = (PP<RegressionTreeMulticlassLeave>) leave_;
    if(leave->classname() == classname()){
        length_ -= leave->length_;
        weights_sum -= leave->weights_sum;
        multiclass_weights_sum -= leave->multiclass_weights_sum;
    }else
        PLERROR("In %s::addLeave the leave to add should have the same class. It have %s.",
                classname().c_str(), leave->classname().c_str());
}

Here is the call graph for this function:

void PLearn::RegressionTreeMulticlassLeave::removeRow ( int  row,
Vec  outputv,
Vec  errorv 
) [virtual]

Reimplemented from PLearn::RegressionTreeLeave.

Definition at line 173 of file RegressionTreeMulticlassLeave.cc.

References removeRow(), and PLearn::RegressionTreeLeave::train_set.

{
    real weight = train_set->getWeight(row);
    real target = train_set->getTarget(row);
    removeRow(row,target,weight,outputv,errorv);
}

Here is the call graph for this function:

void PLearn::RegressionTreeMulticlassLeave::removeRow ( int  row,
real  target,
real  weight 
) [virtual]

Reimplemented from PLearn::RegressionTreeLeave.

Definition at line 186 of file RegressionTreeMulticlassLeave.cc.

References PLearn::TVec< T >::length(), PLearn::RegressionTreeLeave::length_, multiclass_outputs, multiclass_weights_sum, PLASSERT, and PLearn::RegressionTreeLeave::weights_sum.

Referenced by removeRow().

{
    length_ -= 1;
    weights_sum -= weight;
    PLASSERT(length_>=0);
    PLASSERT(weights_sum>=0);
    PLASSERT(length_>0 || weights_sum==0);
    bool found=false;
    //can be optimized: see addRow
    for (int mc_ind = 0; mc_ind < multiclass_outputs.length(); mc_ind++)
    {
        if (target == multiclass_outputs[mc_ind])
        {
            multiclass_weights_sum[mc_ind] -= weight;
            found=true;
            break;      
        }
    }
    PLASSERT(found);
}

Here is the call graph for this function:

Here is the caller graph for this function:

void PLearn::RegressionTreeMulticlassLeave::removeRow ( int  row,
real  target,
real  weight,
Vec  outputv,
Vec  errorv 
) [virtual]

Reimplemented from PLearn::RegressionTreeLeave.

Definition at line 180 of file RegressionTreeMulticlassLeave.cc.

References getOutputAndError(), and removeRow().

                                                         {
    removeRow(row,target,weight);
    getOutputAndError(outputv,errorv);
}

Here is the call graph for this function:


Member Data Documentation

Reimplemented from PLearn::RegressionTreeLeave.

Definition at line 74 of file RegressionTreeMulticlassLeave.h.


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