PLearn 0.1
|
The first sentence should be a BRIEF DESCRIPTION of what the class does. More...
#include <TargetEncodingLearner.h>
Public Member Functions | |
TargetEncodingLearner () | |
Default constructor. | |
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 | train () |
The role of the train method is to bring the learner up to stage==nstages, updating the train_stats collector with training costs measured on-line in the process. | |
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 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 TVec< string > | getOutputNames () const |
outputnames are the same as the inputnames | |
virtual string | classname () const |
virtual OptionList & | getOptionList () const |
virtual OptionMap & | getOptionMap () const |
virtual RemoteMethodMap & | getRemoteMethodMap () const |
virtual TargetEncodingLearner * | deepCopy (CopiesMap &copies) const |
virtual void | build () |
Finish building the object; just call inherited::build followed by build_() | |
virtual void | makeDeepCopyFromShallowCopy (CopiesMap &copies) |
Transforms a shallow copy into a deep copy. | |
virtual void | setExperimentDirectory (const PPath &the_expdir) |
The experiment directory is the directory in which files related to this model are to be saved. | |
virtual void | setTrainingSet (VMat training_set, bool call_forget=true) |
Declares the training 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 () |
Public Attributes | |
int | max_nvals_for_encoding |
### declare public option fields (such as build options) here Start your comments with Doxygen-compatible comments such as //! | |
PP< PLearner > | sublearner |
TVec< map< real, real > > | encodings |
TVec< bool > | encode_col |
TVec< real > | defaults |
real | mean |
bool | encodings_learnt |
Static Public Attributes | |
static StaticInitializer | _static_initializer_ |
Protected Member Functions | |
void | buildEncodingsFromTrainset () |
Static Protected Member Functions | |
static void | declareOptions (OptionList &ol) |
Declares the class options. | |
Private Types | |
typedef PLearner | inherited |
Private Member Functions | |
void | build_ () |
This does the actual building. |
The first sentence should be a BRIEF DESCRIPTION of what the class does.
Place the rest of the class programmer documentation here. Doxygen supports Javadoc-style comments. See http://www.doxygen.org/manual.html
Definition at line 57 of file TargetEncodingLearner.h.
typedef PLearner PLearn::TargetEncodingLearner::inherited [private] |
Reimplemented from PLearn::PLearner.
Definition at line 59 of file TargetEncodingLearner.h.
PLearn::TargetEncodingLearner::TargetEncodingLearner | ( | ) |
Default constructor.
Definition at line 50 of file TargetEncodingLearner.cc.
:max_nvals_for_encoding(1000), sublearner(0), encodings(), encode_col(), mean(0.), encodings_learnt(false) { }
string PLearn::TargetEncodingLearner::_classname_ | ( | ) | [static] |
Reimplemented from PLearn::PLearner.
Definition at line 48 of file TargetEncodingLearner.cc.
OptionList & PLearn::TargetEncodingLearner::_getOptionList_ | ( | ) | [static] |
Reimplemented from PLearn::PLearner.
Definition at line 48 of file TargetEncodingLearner.cc.
RemoteMethodMap & PLearn::TargetEncodingLearner::_getRemoteMethodMap_ | ( | ) | [static] |
Reimplemented from PLearn::PLearner.
Definition at line 48 of file TargetEncodingLearner.cc.
Reimplemented from PLearn::PLearner.
Definition at line 48 of file TargetEncodingLearner.cc.
Object * PLearn::TargetEncodingLearner::_new_instance_for_typemap_ | ( | ) | [static] |
Reimplemented from PLearn::Object.
Definition at line 48 of file TargetEncodingLearner.cc.
StaticInitializer TargetEncodingLearner::_static_initializer_ & PLearn::TargetEncodingLearner::_static_initialize_ | ( | ) | [static] |
Reimplemented from PLearn::PLearner.
Definition at line 48 of file TargetEncodingLearner.cc.
void PLearn::TargetEncodingLearner::build | ( | ) | [virtual] |
Finish building the object; just call inherited::build followed by build_()
Reimplemented from PLearn::PLearner.
Definition at line 106 of file TargetEncodingLearner.cc.
References PLearn::PLearner::build(), and build_().
{ inherited::build(); build_(); }
void PLearn::TargetEncodingLearner::build_ | ( | ) | [private] |
This does the actual building.
Reimplemented from PLearn::PLearner.
Definition at line 90 of file TargetEncodingLearner.cc.
References encodings_learnt.
Referenced by build().
{ // ### This method should do the real building of the object, // ### according to set 'options', in *any* situation. // ### Typical situations include: // ### - Initial building of an object from a few user-specified options // ### - Building of a "reloaded" object: i.e. from the complete set of // ### all serialised options. // ### - Updating or "re-building" of an object after a few "tuning" // ### options have been modified. // ### You should assume that the parent class' build_() has already been // ### called. encodings_learnt= false; }
void PLearn::TargetEncodingLearner::buildEncodingsFromTrainset | ( | ) | [protected] |
Definition at line 166 of file TargetEncodingLearner.cc.
References PLearn::TVec< T >::clear(), defaults, encode_col, encodings, encodings_learnt, PLearn::TVec< T >::end(), PLearn::TVec< T >::fill(), PLearn::TVec< T >::find(), PLearn::VMat::getExample(), i, PLearn::TVec< T >::insert(), j, PLearn::TVec< T >::length(), PLearn::VMat::length(), max_nvals_for_encoding, mean, n, PLERROR, PLearn::PLearner::report_progress, PLearn::TVec< T >::resize(), PLearn::sum(), and PLearn::PLearner::train_set.
Referenced by train().
{ int l = train_set->length(); int n = train_set->inputsize(); Vec input; Vec target; real weight; encodings.resize(n); TVec<map<real, pair<real, real> > > stats(n); encode_col.resize(n); encode_col.fill(true); PP<ProgressBar> pb; if(report_progress) pb = new ProgressBar("TargetEncodingLearner computing statistics ",l); real tot_weight= 0.; real tot_wt_targ= 0.; for(int i= 0; i < l; ++i) { train_set->getExample(i, input, target, weight); if(target.length() != 1) PLERROR("TargetEncodingLearner supports only one target"); tot_weight+= weight; tot_wt_targ+= weight*target[0]; for(int j= 0; j < n; ++j) { if(encode_col[j]) { real val= input[j]; map<real, pair<real, real> >::iterator it= stats[j].find(val); if(it == stats[j].end()) stats[j].insert(make_pair(val, make_pair(weight, weight*target[0]))); else { it->second.first+= weight; it->second.second+= weight*target[0]; } if(static_cast<int>(stats[j].size()) > max_nvals_for_encoding) { encode_col[j]= false; stats[j].clear(); } } } if(pb) pb->update(i); } mean= tot_wt_targ / tot_weight; defaults.resize(n); defaults.fill(mean); for(int i= 0; i < n; ++i) { encodings[i].clear(); for(map<real, pair<real, real> >::iterator it= stats[i].begin(); it != stats[i].end(); ++it) { real count= it->second.first; real sum= it->second.second; real avg= sum/count; encodings[i].insert(make_pair(it->first, avg)); } } encodings_learnt= true; }
string PLearn::TargetEncodingLearner::classname | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 48 of file TargetEncodingLearner.cc.
void PLearn::TargetEncodingLearner::computeCostsFromOutputs | ( | const Vec & | input, |
const Vec & | output, | ||
const Vec & | target, | ||
Vec & | costs | ||
) | const [virtual] |
Computes the costs from already computed output.
Implements PLearn::PLearner.
Definition at line 251 of file TargetEncodingLearner.cc.
References encodings_learnt, PLERROR, and sublearner.
{ if(!encodings_learnt) PLERROR("TargetEncodingLearner::computeCostsFromOutputs encodings not learnt"); //EncodedVMatrix::encodeRow(encodings, defaults, encode_col, input); sublearner->computeCostsFromOutputs(input, output, target, costs); }
void PLearn::TargetEncodingLearner::computeOutput | ( | const Vec & | input, |
Vec & | output | ||
) | const [virtual] |
Computes the output from the input.
Reimplemented from PLearn::PLearner.
Definition at line 243 of file TargetEncodingLearner.cc.
References defaults, encode_col, PLearn::EncodedVMatrix::encodeRow(), encodings, encodings_learnt, PLERROR, and sublearner.
{ if(!encodings_learnt) PLERROR("TargetEncodingLearner::computeOutput encodings not learnt"); EncodedVMatrix::encodeRow(encodings, defaults, encode_col, input); sublearner->computeOutput(input, output); }
void PLearn::TargetEncodingLearner::declareOptions | ( | OptionList & | ol | ) | [static, protected] |
Declares the class options.
Reimplemented from PLearn::PLearner.
Definition at line 60 of file TargetEncodingLearner.cc.
References PLearn::OptionBase::buildoption, PLearn::declareOption(), PLearn::PLearner::declareOptions(), encode_col, encodings, encodings_learnt, PLearn::OptionBase::learntoption, max_nvals_for_encoding, mean, and sublearner.
{ declareOption(ol, "max_nvals_for_encoding", &TargetEncodingLearner::max_nvals_for_encoding, OptionBase::buildoption, "Max number of different values for a field to be encoded.\n"); declareOption(ol, "sublearner", &TargetEncodingLearner::sublearner, OptionBase::buildoption, "Learner to be trained/tested on the encoded dataset.\n"); declareOption(ol, "encodings", &TargetEncodingLearner::encodings, OptionBase::learntoption, "Learnt encodings for the trainset.\n"); declareOption(ol, "encode_col", &TargetEncodingLearner::encode_col, OptionBase::learntoption, "Wether each col. should be encoded.\n"); declareOption(ol, "mean", &TargetEncodingLearner::mean, OptionBase::learntoption, "Weighted mean all targets.\n"); declareOption(ol, "encodings_learnt", &TargetEncodingLearner::encodings_learnt, OptionBase::learntoption, "Wether encodings have already been learnt.\n"); // Now call the parent class' declareOptions inherited::declareOptions(ol); }
static const PPath& PLearn::TargetEncodingLearner::declaringFile | ( | ) | [inline, static] |
Reimplemented from PLearn::PLearner.
Definition at line 152 of file TargetEncodingLearner.h.
:
//##### Protected Options ###############################################
TargetEncodingLearner * PLearn::TargetEncodingLearner::deepCopy | ( | CopiesMap & | copies | ) | const [virtual] |
Reimplemented from PLearn::PLearner.
Definition at line 48 of file TargetEncodingLearner.cc.
void PLearn::TargetEncodingLearner::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::PLearner.
Definition at line 140 of file TargetEncodingLearner.cc.
References encodings_learnt, PLearn::PLearner::forget(), and PLearn::PLearner::stage.
{ inherited::forget(); stage = 0; encodings_learnt= false; }
OptionList & PLearn::TargetEncodingLearner::getOptionList | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 48 of file TargetEncodingLearner.cc.
OptionMap & PLearn::TargetEncodingLearner::getOptionMap | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 48 of file TargetEncodingLearner.cc.
TVec< string > PLearn::TargetEncodingLearner::getOutputNames | ( | ) | const [virtual] |
outputnames are the same as the inputnames
Reimplemented from PLearn::PLearner.
Definition at line 270 of file TargetEncodingLearner.cc.
References sublearner.
{ return sublearner->getOutputNames(); }
RemoteMethodMap & PLearn::TargetEncodingLearner::getRemoteMethodMap | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 48 of file TargetEncodingLearner.cc.
TVec< string > PLearn::TargetEncodingLearner::getTestCostNames | ( | ) | const [virtual] |
Returns the names of the costs computed by computeCostsFromOutpus (and thus the test method).
Implements PLearn::PLearner.
Definition at line 260 of file TargetEncodingLearner.cc.
References sublearner.
{ return sublearner->getTestCostNames(); }
TVec< string > PLearn::TargetEncodingLearner::getTrainCostNames | ( | ) | const [virtual] |
Returns 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 265 of file TargetEncodingLearner.cc.
References sublearner.
{ return sublearner->getTrainCostNames(); }
void PLearn::TargetEncodingLearner::makeDeepCopyFromShallowCopy | ( | CopiesMap & | copies | ) | [virtual] |
Transforms a shallow copy into a deep copy.
Reimplemented from PLearn::PLearner.
Definition at line 113 of file TargetEncodingLearner.cc.
References PLearn::PLearner::makeDeepCopyFromShallowCopy(), and PLERROR.
{ inherited::makeDeepCopyFromShallowCopy(copies); // ### Call deepCopyField on all "pointer-like" fields // ### that you wish to be deepCopied rather than // ### shallow-copied. // ### ex: // deepCopyField(trainvec, copies); // ### Remove this line when you have fully implemented this method. PLERROR("TargetEncodingLearner::makeDeepCopyFromShallowCopy not fully (correctly) implemented yet!"); }
int PLearn::TargetEncodingLearner::outputsize | ( | ) | const [virtual] |
Returns the size of this learner's output, (which typically may depend on its inputsize(), targetsize() and set options).
Implements PLearn::PLearner.
Definition at line 135 of file TargetEncodingLearner.cc.
References sublearner.
{ return sublearner->outputsize(); }
void PLearn::TargetEncodingLearner::setExperimentDirectory | ( | const PPath & | the_expdir | ) | [virtual] |
The experiment directory is the directory in which files related to this model are to be saved.
If it is an empty string, it is understood to mean that the user doesn't want any file created by this learner.
Reimplemented from PLearn::PLearner.
Definition at line 128 of file TargetEncodingLearner.cc.
References PLearn::PLearner::setExperimentDirectory(), and sublearner.
{ inherited::setExperimentDirectory(the_expdir); sublearner->setExperimentDirectory(the_expdir / "SubLearner"); }
void PLearn::TargetEncodingLearner::setTrainingSet | ( | VMat | training_set, |
bool | call_forget = true |
||
) | [virtual] |
Declares the training set.
Then calls build() and forget() if necessary. Also sets this learner's inputsize_ targetsize_ weightsize_ from those of the training_set. Note: You shouldn't have to override this in subclasses, except in maybe to forward the call to an underlying learner.
Reimplemented from PLearn::PLearner.
Definition at line 275 of file TargetEncodingLearner.cc.
References PLearn::PLearner::setTrainingSet(), and sublearner.
{ // temporarly set sublearner's train set (reset in train()) if(sublearner) sublearner->setTrainingSet(training_set, call_forget); inherited::setTrainingSet(training_set, call_forget); }
void PLearn::TargetEncodingLearner::train | ( | ) | [virtual] |
The role of the train method is to bring the learner up to stage==nstages, updating the train_stats collector with training costs measured on-line in the process.
Implements PLearn::PLearner.
Definition at line 147 of file TargetEncodingLearner.cc.
References buildEncodingsFromTrainset(), defaults, encode_col, encodings, PLearn::PLearner::getExperimentDirectory(), PLearn::PLearner::getTrainingSet(), PLearn::PLearner::initTrain(), PLearn::PLearner::stage, and sublearner.
{ if (!initTrain()) return; if(stage<1) { buildEncodingsFromTrainset(); VMat vm= new EncodedVMatrix(getTrainingSet(), encodings, defaults, encode_col); vm->setMetaDataDir(getExperimentDirectory() / "EncodedVMatrix.metadata"); sublearner->setTrainingSet(vm); ++stage; } sublearner->train(); }
Reimplemented from PLearn::PLearner.
Definition at line 152 of file TargetEncodingLearner.h.
Definition at line 75 of file TargetEncodingLearner.h.
Referenced by buildEncodingsFromTrainset(), computeOutput(), and train().
Definition at line 74 of file TargetEncodingLearner.h.
Referenced by buildEncodingsFromTrainset(), computeOutput(), declareOptions(), and train().
Definition at line 73 of file TargetEncodingLearner.h.
Referenced by buildEncodingsFromTrainset(), computeOutput(), declareOptions(), and train().
Definition at line 78 of file TargetEncodingLearner.h.
Referenced by build_(), buildEncodingsFromTrainset(), computeCostsFromOutputs(), computeOutput(), declareOptions(), and forget().
### declare public option fields (such as build options) here Start your comments with Doxygen-compatible comments such as //!
Definition at line 68 of file TargetEncodingLearner.h.
Referenced by buildEncodingsFromTrainset(), and declareOptions().
Definition at line 76 of file TargetEncodingLearner.h.
Referenced by buildEncodingsFromTrainset(), and declareOptions().
Definition at line 70 of file TargetEncodingLearner.h.
Referenced by computeCostsFromOutputs(), computeOutput(), declareOptions(), getOutputNames(), getTestCostNames(), getTrainCostNames(), outputsize(), setExperimentDirectory(), setTrainingSet(), and train().