PLearn 0.1
|
#include <CompareLearner.h>
Public Member Functions | |
CompareLearner () | |
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 CompareLearner * | 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 | 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 void | setTrainStatsCollector (PP< VecStatsCollector > statscol) |
Sets the statistics collector whose update() method will be called during training. | |
virtual void | setTrainingSet (VMat training_set, bool call_forget=true) |
Declares the training set. | |
virtual void | setValidationSet (VMat validset) |
Set the validation set (optionally) for learners that are able to use it directly. | |
virtual void | setExperimentDirectory (const PPath &the_expdir) |
The experiment directory is the directory in which files related to this model are to be saved. | |
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 | |
TVec< PP< PLearner > > | learners |
Learners to compare. | |
TVec< string > | learner_names |
Names of the learners. | |
TVec< string > | common_costs |
Common costs of the learners. | |
Static Public Attributes | |
static StaticInitializer | _static_initializer_ |
Static Protected Member Functions | |
static void | declareOptions (OptionList &ol) |
Declares this class' options. | |
Protected Attributes | |
int | n_learners |
Number of learners to compare. | |
int | learners_outputsize |
Output size of the learners. | |
TMat< int > | costs_indexes |
Indexes of the different costs. | |
Private Types | |
typedef PLearner | inherited |
Private Member Functions | |
void | build_ () |
This does the actual building. |
Definition at line 51 of file CompareLearner.h.
typedef PLearner PLearn::CompareLearner::inherited [private] |
Reimplemented from PLearn::PLearner.
Definition at line 56 of file CompareLearner.h.
PLearn::CompareLearner::CompareLearner | ( | ) |
Default constructor.
Definition at line 50 of file CompareLearner.cc.
References build_().
string PLearn::CompareLearner::_classname_ | ( | ) | [static] |
Reimplemented from PLearn::PLearner.
Definition at line 67 of file CompareLearner.cc.
OptionList & PLearn::CompareLearner::_getOptionList_ | ( | ) | [static] |
Reimplemented from PLearn::PLearner.
Definition at line 67 of file CompareLearner.cc.
RemoteMethodMap & PLearn::CompareLearner::_getRemoteMethodMap_ | ( | ) | [static] |
Reimplemented from PLearn::PLearner.
Definition at line 67 of file CompareLearner.cc.
Reimplemented from PLearn::PLearner.
Definition at line 67 of file CompareLearner.cc.
Object * PLearn::CompareLearner::_new_instance_for_typemap_ | ( | ) | [static] |
Reimplemented from PLearn::Object.
Definition at line 67 of file CompareLearner.cc.
StaticInitializer CompareLearner::_static_initializer_ & PLearn::CompareLearner::_static_initialize_ | ( | ) | [static] |
Reimplemented from PLearn::PLearner.
Definition at line 67 of file CompareLearner.cc.
void PLearn::CompareLearner::build | ( | ) | [virtual] |
Simply calls inherited::build() then build_().
Reimplemented from PLearn::PLearner.
Definition at line 101 of file CompareLearner.cc.
References PLearn::PLearner::build(), and build_().
{ inherited::build(); build_(); }
void PLearn::CompareLearner::build_ | ( | ) | [private] |
This does the actual building.
Reimplemented from PLearn::PLearner.
Definition at line 80 of file CompareLearner.cc.
References c, common_costs, costs_indexes, PLearn::TVec< T >::find(), i, learner_names, learners, learners_outputsize, PLearn::TVec< T >::length(), n_learners, PLERROR, and PLearn::TMat< T >::resize().
Referenced by build(), and CompareLearner().
{ n_learners = learners.length(); if(n_learners > 0) { learners_outputsize = learners[0]->outputsize(); if(learner_names.length() != n_learners) PLERROR("Number of learner names is different from number of learners"); costs_indexes.resize(n_learners,common_costs.length()); TVec<string> test_costs; for(int i=0; i<n_learners; i++) { learners[i]->build(); test_costs = learners[i]->getTestCostNames(); for(int c=0; c<common_costs.length(); c++) { costs_indexes(i,c) = test_costs.find(common_costs[c]); } } } }
string PLearn::CompareLearner::classname | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 67 of file CompareLearner.cc.
void PLearn::CompareLearner::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 151 of file CompareLearner.cc.
References PLearn::TVec< T >::append(), c, common_costs, costs_indexes, PLearn::diff(), getTestCostNames(), i, learners, learners_outputsize, PLearn::TVec< T >::length(), MISSING_VALUE, n_learners, PLearn::TVec< T >::resize(), and PLearn::TVec< T >::subVec().
{ Vec one_learner_costs; costs.resize(0); Vec output_i; for(int i=0; i<n_learners; i++) { one_learner_costs.resize(learners[i]->getTestCostNames().length()); output_i = output.subVec(i*learners_outputsize,learners_outputsize); learners[i]->computeCostsFromOutputs(input, output_i,target,one_learner_costs); for(int c=0; c<common_costs.length(); c++) { if(costs_indexes(i,c) == -1) costs.append(MISSING_VALUE); else costs.append(one_learner_costs[costs_indexes(i,c)]); } } for ( int m1=0; m1 < n_learners; m1++ ) for ( int m2=(m1+1); m2 < n_learners; m2++ ) for ( int cc=0; cc < common_costs.length(); cc++ ) { real diff = costs[m1*common_costs.length()+cc]-costs[m2*common_costs.length()+cc]; costs.append(diff); costs.append(fabs(diff)); } }
Computes the output from the input.
Reimplemented from PLearn::PLearner.
Definition at line 140 of file CompareLearner.cc.
References i, learners, learners_outputsize, n_learners, outputsize(), PLearn::TVec< T >::resize(), and PLearn::TVec< T >::subVec().
{ output.resize(outputsize()); Vec output_i; for(int i=0; i<n_learners; i++) { output_i = output.subVec(i*learners_outputsize,learners_outputsize); learners[i]->computeOutput(input,output_i); } }
void PLearn::CompareLearner::declareOptions | ( | OptionList & | ol | ) | [static, protected] |
Declares this class' options.
Reimplemented from PLearn::PLearner.
Definition at line 68 of file CompareLearner.cc.
References PLearn::OptionBase::buildoption, common_costs, PLearn::declareOption(), PLearn::PLearner::declareOptions(), learner_names, and learners.
{ declareOption(ol, "learners", &CompareLearner::learners, OptionBase::buildoption, "learners to compare"); declareOption(ol, "common_costs", &CompareLearner::common_costs, OptionBase::buildoption, "common costs of the learners to compare"); declareOption(ol, "learner_names", &CompareLearner::learner_names, OptionBase::buildoption, "names of the learners"); inherited::declareOptions(ol); }
static const PPath& PLearn::CompareLearner::declaringFile | ( | ) | [inline, static] |
Reimplemented from PLearn::PLearner.
Definition at line 124 of file CompareLearner.h.
CompareLearner * PLearn::CompareLearner::deepCopy | ( | CopiesMap & | copies | ) | const [virtual] |
Reimplemented from PLearn::PLearner.
Definition at line 67 of file CompareLearner.cc.
void PLearn::CompareLearner::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 127 of file CompareLearner.cc.
References i, learners, and PLearn::TVec< T >::length().
Referenced by setTrainingSet().
OptionList & PLearn::CompareLearner::getOptionList | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 67 of file CompareLearner.cc.
OptionMap & PLearn::CompareLearner::getOptionMap | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 67 of file CompareLearner.cc.
RemoteMethodMap & PLearn::CompareLearner::getRemoteMethodMap | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 67 of file CompareLearner.cc.
TVec< string > PLearn::CompareLearner::getTestCostNames | ( | ) | const [virtual] |
Returns the names of the costs computed by computeCostsFromOutpus (and thus the test method).
Implements PLearn::PLearner.
Definition at line 182 of file CompareLearner.cc.
References PLearn::TVec< T >::append(), c, common_costs, i, learner_names, PLearn::TVec< T >::length(), and n_learners.
Referenced by computeCostsFromOutputs().
{ TVec<string> cost_names(0); for(int i=0; i<n_learners; i++) { for(int c=0; c<common_costs.length(); c++) { cost_names.append(learner_names[i] + "::" + common_costs[c]); } } for ( int m1=0; m1 < n_learners; m1++ ) for ( int m2=(m1+1); m2 < n_learners; m2++ ) for ( int cc=0; cc < common_costs.length(); cc++ ) { string postfix = common_costs[cc] + "_" + learner_names[m1] + "_VS_" + learner_names[m2]; cost_names.append( "difference_" + postfix ); cost_names.append( "abs_difference_" + postfix ); } return cost_names; }
TVec< string > PLearn::CompareLearner::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 206 of file CompareLearner.cc.
References PLearn::TVec< T >::append(), c, i, learner_names, learners, PLearn::TVec< T >::length(), and n_learners.
{ TVec<string> cost_names(0); TVec<string> cost_names_i; for(int i=0; i<n_learners; i++) { cost_names_i = learners[i]->getTrainCostNames(); for(int c=0; c<cost_names_i.length(); c++) cost_names.append(learner_names[i] + "::" + cost_names_i[c]); } return cost_names; }
void PLearn::CompareLearner::makeDeepCopyFromShallowCopy | ( | CopiesMap & | copies | ) | [virtual] |
Transforms a shallow copy into a deep copy.
Reimplemented from PLearn::PLearner.
Definition at line 108 of file CompareLearner.cc.
References common_costs, costs_indexes, PLearn::deepCopyField(), learner_names, learners, and PLearn::PLearner::makeDeepCopyFromShallowCopy().
{ inherited::makeDeepCopyFromShallowCopy(copies); deepCopyField(learners, copies); deepCopyField(learner_names, copies); deepCopyField(common_costs, copies); deepCopyField(costs_indexes, copies); //PLERROR("CompareLearner::makeDeepCopyFromShallowCopy not fully (correctly) implemented yet!"); }
int PLearn::CompareLearner::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 121 of file CompareLearner.cc.
References learners_outputsize, and n_learners.
Referenced by computeOutput().
{ if(n_learners == 0) return 0; else return n_learners * learners_outputsize; }
void PLearn::CompareLearner::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 248 of file CompareLearner.cc.
References PLearn::PPath::absolute(), PLearn::PLearner::expdir, PLearn::force_mkdir(), i, learner_names, learners, n_learners, and PLERROR.
{ if(the_expdir=="") { expdir = ""; for(int i=0; i<n_learners; i++) { learners[i]->setExperimentDirectory(the_expdir); } } else { if(!force_mkdir(the_expdir)) PLERROR("In PLearner::setExperimentDirectory Could not create experiment directory %s",the_expdir.c_str()); expdir = the_expdir.absolute(); string learner_expdir; for(int i=0; i<n_learners; i++) { learner_expdir = the_expdir.absolute() + "_" + learner_names[i]; learners[i]->setExperimentDirectory(learner_expdir); } } }
void PLearn::CompareLearner::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 230 of file CompareLearner.cc.
References forget(), i, learners, and n_learners.
{ for(int i=0; i<n_learners; i++) { learners[i]->setTrainingSet(training_set,call_forget); } if (call_forget) forget(); }
void PLearn::CompareLearner::setTrainStatsCollector | ( | PP< VecStatsCollector > | statscol | ) | [virtual] |
Sets the statistics collector whose update() method will be called during training.
Note: You shouldn't have to override this in subclasses, except maybe to forward the call to an underlying learner.
Reimplemented from PLearn::PLearner.
Definition at line 220 of file CompareLearner.cc.
References i, learners, and n_learners.
void PLearn::CompareLearner::setValidationSet | ( | VMat | validset | ) | [virtual] |
Set the validation set (optionally) for learners that are able to use it directly.
Reimplemented from PLearn::PLearner.
Definition at line 240 of file CompareLearner.cc.
References i, learners, and n_learners.
void PLearn::CompareLearner::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 133 of file CompareLearner.cc.
References i, learners, and n_learners.
Reimplemented from PLearn::PLearner.
Definition at line 124 of file CompareLearner.h.
Common costs of the learners.
Definition at line 85 of file CompareLearner.h.
Referenced by build_(), computeCostsFromOutputs(), declareOptions(), getTestCostNames(), and makeDeepCopyFromShallowCopy().
TMat<int> PLearn::CompareLearner::costs_indexes [protected] |
Indexes of the different costs.
Definition at line 72 of file CompareLearner.h.
Referenced by build_(), computeCostsFromOutputs(), and makeDeepCopyFromShallowCopy().
Names of the learners.
Definition at line 83 of file CompareLearner.h.
Referenced by build_(), declareOptions(), getTestCostNames(), getTrainCostNames(), makeDeepCopyFromShallowCopy(), and setExperimentDirectory().
Learners to compare.
Definition at line 81 of file CompareLearner.h.
Referenced by build_(), computeCostsFromOutputs(), computeOutput(), declareOptions(), forget(), getTrainCostNames(), makeDeepCopyFromShallowCopy(), setExperimentDirectory(), setTrainingSet(), setTrainStatsCollector(), setValidationSet(), and train().
int PLearn::CompareLearner::learners_outputsize [protected] |
Output size of the learners.
Definition at line 70 of file CompareLearner.h.
Referenced by build_(), computeCostsFromOutputs(), computeOutput(), and outputsize().
int PLearn::CompareLearner::n_learners [protected] |
Number of learners to compare.
Definition at line 68 of file CompareLearner.h.
Referenced by build_(), computeCostsFromOutputs(), computeOutput(), getTestCostNames(), getTrainCostNames(), outputsize(), setExperimentDirectory(), setTrainingSet(), setTrainStatsCollector(), setValidationSet(), and train().