PLearn 0.1
|
#include <RegressionTreeMulticlassLeave.h>
Public Member Functions | |
RegressionTreeMulticlassLeave () | |
virtual | ~RegressionTreeMulticlassLeave () |
virtual string | classname () const |
virtual OptionList & | getOptionList () const |
virtual OptionMap & | getOptionMap () const |
virtual RemoteMethodMap & | getRemoteMethodMap () 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 PPath & | declaringFile () |
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 |
Definition at line 50 of file RegressionTreeMulticlassLeave.h.
typedef RegressionTreeLeave PLearn::RegressionTreeMulticlassLeave::inherited [private] |
Reimplemented from PLearn::RegressionTreeLeave.
Definition at line 52 of file RegressionTreeMulticlassLeave.h.
PLearn::RegressionTreeMulticlassLeave::RegressionTreeMulticlassLeave | ( | ) |
Definition at line 55 of file RegressionTreeMulticlassLeave.cc.
References build().
: objective_function("l1") { build(); }
PLearn::RegressionTreeMulticlassLeave::~RegressionTreeMulticlassLeave | ( | ) | [virtual] |
Definition at line 61 of file RegressionTreeMulticlassLeave.cc.
{ }
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.
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()); }
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); }
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); }
Reimplemented from PLearn::RegressionTreeLeave.
Definition at line 167 of file RegressionTreeMulticlassLeave.cc.
References addRow(), and getOutputAndError().
{ addRow(row); getOutputAndError(outputv,errorv); }
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); }
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().
{ inherited::build(); build_(); }
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().
{ }
string PLearn::RegressionTreeMulticlassLeave::classname | ( | ) | const [virtual] |
Reimplemented from PLearn::RegressionTreeLeave.
Definition at line 53 of file RegressionTreeMulticlassLeave.cc.
Referenced by addLeave(), and removeLeave().
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); }
ol | List 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); }
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; }
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; }
RemoteMethodMap & PLearn::RegressionTreeMulticlassLeave::getRemoteMethodMap | ( | ) | const [virtual] |
Reimplemented from PLearn::RegressionTreeLeave.
Definition at line 53 of file RegressionTreeMulticlassLeave.cc.
void PLearn::RegressionTreeMulticlassLeave::initStats | ( | ) | [virtual] |
Reimplemented from PLearn::RegressionTreeLeave.
Definition at line 114 of file RegressionTreeMulticlassLeave.cc.
References PLearn::TVec< T >::fill(), l1_loss_function_factor, l2_loss_function_factor, PLearn::TVec< T >::length(), PLearn::RegressionTreeLeave::length_, PLearn::RegressionTreeLeave::loss_function_weight, multiclass_outputs, multiclass_weights_sum, PLearn::pow(), PLearn::TVec< T >::resize(), and PLearn::RegressionTreeLeave::weights_sum.
{ length_ = 0; weights_sum = 0.0; if (loss_function_weight != 0.0) { l1_loss_function_factor = 2.0 / loss_function_weight; l2_loss_function_factor = 2.0 / pow(loss_function_weight, 2); } else { l1_loss_function_factor = 1.0; l2_loss_function_factor = 1.0; } multiclass_weights_sum.resize(multiclass_outputs.length()); multiclass_weights_sum.fill(0); }
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); ... }
copies | A 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.
{ inherited::makeDeepCopyFromShallowCopy(copies); deepCopyField(multiclass_outputs, copies); deepCopyField(objective_function, copies); deepCopyField(l1_loss_function_factor, copies); deepCopyField(l2_loss_function_factor, copies); deepCopyField(multiclass_weights_sum, copies); }
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; }
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()); }
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); }
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); }
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); }
Reimplemented from PLearn::RegressionTreeLeave.
Definition at line 74 of file RegressionTreeMulticlassLeave.h.
Definition at line 67 of file RegressionTreeMulticlassLeave.h.
Referenced by declareOptions(), getOutputAndError(), initStats(), and makeDeepCopyFromShallowCopy().
Definition at line 68 of file RegressionTreeMulticlassLeave.h.
Referenced by declareOptions(), getOutputAndError(), initStats(), and makeDeepCopyFromShallowCopy().
Definition at line 60 of file RegressionTreeMulticlassLeave.h.
Referenced by addRow(), declareOptions(), getOutputAndError(), initStats(), makeDeepCopyFromShallowCopy(), and removeRow().
Definition at line 69 of file RegressionTreeMulticlassLeave.h.
Referenced by addLeave(), addRow(), declareOptions(), getOutputAndError(), initStats(), makeDeepCopyFromShallowCopy(), printStats(), removeLeave(), and removeRow().
string PLearn::RegressionTreeMulticlassLeave::objective_function [private] |
Definition at line 61 of file RegressionTreeMulticlassLeave.h.
Referenced by declareOptions(), getOutputAndError(), and makeDeepCopyFromShallowCopy().