PLearn 0.1
|
This SequentialLearner only takes the n previous target to predict the next one. More...
#include <MovingAverage.h>
Public Types | |
typedef SequentialLearner | inherited |
Does the necessary operations to transform a shallow copy (this) into a deep copy by deep-copying all the members that need to be. | |
Public Member Functions | |
MovingAverage () | |
Constructor. | |
virtual void | build () |
simply calls inherited::build() then build_() | |
virtual void | train () |
virtual void | test (VMat testset, PP< VecStatsCollector > test_stats, VMat testoutputs=0, VMat testcosts=0) const |
virtual void | computeCostsFromOutputs (const Vec &inputs, const Vec &outputs, const Vec &targets, Vec &costs) const |
*** SUBCLASS WRITING: *** | |
virtual TVec< string > | getTrainCostNames () const |
*** SUBCLASS WRITING: *** | |
virtual TVec< string > | getTestCostNames () const |
*** SUBCLASS WRITING: *** | |
virtual void | forget () |
*** SUBCLASS WRITING: *** | |
virtual string | classname () const |
virtual OptionList & | getOptionList () const |
virtual OptionMap & | getOptionMap () const |
virtual RemoteMethodMap & | getRemoteMethodMap () const |
virtual MovingAverage * | deepCopy (CopiesMap &copies) const |
Static Public Member Functions | |
static string | _classname_ () |
Does the necessary operations to transform a shallow copy (this) into a deep copy by deep-copying all the members that need to be. | |
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 | window_length |
Array< string > | cost_funcs |
Static Public Attributes | |
static StaticInitializer | _static_initializer_ |
Static Protected Member Functions | |
static void | declareOptions (OptionList &ol) |
Declare this class' options. | |
Private Member Functions | |
void | build_ () |
This does the actual building. |
This SequentialLearner only takes the n previous target to predict the next one.
This is a memoryless learner!
Definition at line 49 of file MovingAverage.h.
Does the necessary operations to transform a shallow copy (this) into a deep copy by deep-copying all the members that need to be.
Reimplemented from PLearn::SequentialLearner.
Definition at line 87 of file MovingAverage.h.
PLearn::MovingAverage::MovingAverage | ( | ) |
string PLearn::MovingAverage::_classname_ | ( | ) | [static] |
Does the necessary operations to transform a shallow copy (this) into a deep copy by deep-copying all the members that need to be.
Reimplemented from PLearn::SequentialLearner.
Definition at line 46 of file MovingAverage.cc.
OptionList & PLearn::MovingAverage::_getOptionList_ | ( | ) | [static] |
Reimplemented from PLearn::SequentialLearner.
Definition at line 46 of file MovingAverage.cc.
RemoteMethodMap & PLearn::MovingAverage::_getRemoteMethodMap_ | ( | ) | [static] |
Reimplemented from PLearn::SequentialLearner.
Definition at line 46 of file MovingAverage.cc.
Reimplemented from PLearn::SequentialLearner.
Definition at line 46 of file MovingAverage.cc.
Object * PLearn::MovingAverage::_new_instance_for_typemap_ | ( | ) | [static] |
Reimplemented from PLearn::Object.
Definition at line 46 of file MovingAverage.cc.
StaticInitializer MovingAverage::_static_initializer_ & PLearn::MovingAverage::_static_initialize_ | ( | ) | [static] |
Reimplemented from PLearn::SequentialLearner.
Definition at line 46 of file MovingAverage.cc.
void PLearn::MovingAverage::build | ( | ) | [virtual] |
simply calls inherited::build() then build_()
Reimplemented from PLearn::SequentialLearner.
Definition at line 64 of file MovingAverage.cc.
References PLearn::SequentialLearner::build(), and build_().
{ inherited::build(); build_(); }
void PLearn::MovingAverage::build_ | ( | ) | [private] |
This does the actual building.
Reimplemented from PLearn::SequentialLearner.
Definition at line 52 of file MovingAverage.cc.
References cost_funcs, forget(), PLearn::SequentialLearner::max_train_len, PLERROR, PLearn::TVec< T >::size(), and window_length.
Referenced by build().
{ if(cost_funcs.size() < 1) PLERROR("In MovingAverage::build_() Empty cost_funcs : must at least specify one cost function!"); if (window_length < 1) PLERROR("In MovingAverage::build_() window_length has not been set!"); max_train_len = window_length; forget(); }
string PLearn::MovingAverage::classname | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 46 of file MovingAverage.cc.
void PLearn::MovingAverage::computeCostsFromOutputs | ( | const Vec & | input, |
const Vec & | output, | ||
const Vec & | target, | ||
Vec & | costs | ||
) | const [virtual] |
*** SUBCLASS WRITING: ***
This should be defined in subclasses to compute the weighted costs from already computed output. The costs should correspond to the cost names returned by getTestCostNames().
NOTE: In exotic cases, the cost may also depend on some info in the input, that's why the method also gets so see it.
Reimplemented from PLearn::SequentialLearner.
Definition at line 177 of file MovingAverage.cc.
References cost_funcs, i, PLERROR, PLearn::TVec< T >::size(), and PLearn::square().
Referenced by test(), and train().
{ for (int i=0; i<cost_funcs.size(); i++) { if (cost_funcs[i]=="mse" || cost_funcs[i]=="MSE") costs << square(outputs-targets); else PLERROR("This cost_funcs is not implemented."); } }
void PLearn::MovingAverage::declareOptions | ( | OptionList & | ol | ) | [static, protected] |
Declare this class' options.
Reimplemented from PLearn::SequentialLearner.
Definition at line 70 of file MovingAverage.cc.
References PLearn::OptionBase::buildoption, cost_funcs, PLearn::declareOption(), PLearn::SequentialLearner::declareOptions(), and window_length.
{ declareOption(ol, "window_length", &MovingAverage::window_length, OptionBase::buildoption, "the length of the moving average window \n"); declareOption(ol, "cost_funcs", &MovingAverage::cost_funcs, OptionBase::buildoption, "a list of cost functions to use \n"); inherited::declareOptions(ol); }
static const PPath& PLearn::MovingAverage::declaringFile | ( | ) | [inline, static] |
Reimplemented from PLearn::SequentialLearner.
Definition at line 88 of file MovingAverage.h.
MovingAverage * PLearn::MovingAverage::deepCopy | ( | CopiesMap & | copies | ) | const [virtual] |
Reimplemented from PLearn::SequentialLearner.
Definition at line 46 of file MovingAverage.cc.
void PLearn::MovingAverage::forget | ( | ) | [virtual] |
*** SUBCLASS WRITING: ***
(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!)
A typical forget() method should do the following:
This method is typically called by the build_() method, after it has finished setting up the parameters, and if it deemed useful to set or reset the learner in its fresh state. (remember build may be called after modifying options that do not necessarily require the learner to restart from a fresh state...) forget is also called by the setTrainingSet method, after calling build(), so it will generally be called TWICE during setTrainingSet!
Reimplemented from PLearn::SequentialLearner.
Definition at line 195 of file MovingAverage.cc.
References PLearn::SequentialLearner::forget().
Referenced by build_().
{ inherited::forget(); }
OptionList & PLearn::MovingAverage::getOptionList | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 46 of file MovingAverage.cc.
OptionMap & PLearn::MovingAverage::getOptionMap | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 46 of file MovingAverage.cc.
RemoteMethodMap & PLearn::MovingAverage::getRemoteMethodMap | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 46 of file MovingAverage.cc.
TVec< string > PLearn::MovingAverage::getTestCostNames | ( | ) | const [virtual] |
*** SUBCLASS WRITING: ***
This should return the names of the costs computed by computeCostsFromOutputs.
Implements PLearn::PLearner.
Definition at line 192 of file MovingAverage.cc.
References getTrainCostNames().
{ return getTrainCostNames(); }
TVec< string > PLearn::MovingAverage::getTrainCostNames | ( | ) | const [virtual] |
*** SUBCLASS WRITING: ***
This should return 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 189 of file MovingAverage.cc.
References cost_funcs.
Referenced by getTestCostNames().
{ return cost_funcs; }
void PLearn::MovingAverage::test | ( | VMat | testset, |
PP< VecStatsCollector > | test_stats, | ||
VMat | testoutputs = 0 , |
||
VMat | testcosts = 0 |
||
) | const [virtual] |
*** SUBCLASS WRITING: *** The method should:
Implements PLearn::SequentialLearner.
Definition at line 128 of file MovingAverage.cc.
References PLearn::columnMean(), computeCostsFromOutputs(), PLearn::endl(), PLearn::SequentialLearner::errors, PLearn::VMat::getSubRow(), PLearn::TVec< T >::hasMissing(), PLearn::SequentialLearner::horizon, PLearn::PLearner::inputsize(), PLearn::SequentialLearner::last_test_t, PLearn::SequentialLearner::last_train_t, PLearn::VMat::length(), MAX, PLearn::SequentialLearner::outputsize(), PLearn::SequentialLearner::predictions, PLearn::PLearner::report_progress, PLearn::VMat::subMat(), PLearn::PLearner::targetsize(), PLearn::VMat::toMat(), and window_length.
{ PP<ProgressBar> pb; static Vec input(0); static Vec target(targetsize()); static Vec output(outputsize()); static Vec cost(targetsize()); static Mat all_targets; int start = MAX(window_length-1, last_test_t+1); start = MAX(last_train_t+1,start); int target_pos = inputsize(); if (report_progress) pb = new ProgressBar("Testing MovingAverage learner", testset.length()-start); //test_stats->forget(); for (int t=start; t<testset.length(); t++) { #ifdef DEBUG cout << "MovingAverage::test -- t = " << t << endl; #endif all_targets = testset.subMat(t-window_length+1, target_pos, window_length, targetsize()).toMat(); columnMean(all_targets,output); predictions(t) << output; if (testoutputs) testoutputs->appendRow(output); if (t >= horizon) { Vec out = predictions(t-horizon); testset->getSubRow(t, target_pos, target); if (!target.hasMissing() && !out.hasMissing()) { computeCostsFromOutputs(input, out, target, cost); errors(t) << cost; if (testcosts) testcosts->appendRow(cost); test_stats->update(cost); #ifdef DEBUG cout << "MovingAverage::test update test_stats pour t = " << t << endl; #endif } } if (pb) pb->update(t-start); } last_test_t = MAX(testset.length()-1, last_test_t); #ifdef DEBUG cout << "MovingAverage.last_test_t = " << last_test_t << endl; #endif }
void PLearn::MovingAverage::train | ( | ) | [virtual] |
*** SUBCLASS WRITING: *** Does the actual training. Subclasses must implement this method. The method should upon entry, call setTrainingSet(training_set); The method should:
Implements PLearn::SequentialLearner.
Definition at line 81 of file MovingAverage.cc.
References PLearn::columnMean(), computeCostsFromOutputs(), PLearn::endl(), PLearn::SequentialLearner::errors, PLearn::VMat::getSubRow(), PLearn::TVec< T >::hasMissing(), PLearn::SequentialLearner::horizon, PLearn::PLearner::inputsize(), PLearn::SequentialLearner::last_train_t, PLearn::VMat::length(), MAX, PLearn::SequentialLearner::outputsize(), PLearn::SequentialLearner::predictions, PLearn::PLearner::report_progress, PLearn::VMat::subMat(), PLearn::PLearner::targetsize(), PLearn::PLearner::train_set, PLearn::PLearner::train_stats, and window_length.
{ PP<ProgressBar> pb; static Vec input(0); static Vec target(targetsize()); static Vec output(outputsize()); static Vec cost(targetsize()); static Mat all_targets; int target_pos = inputsize(); int start = MAX(window_length-1, last_train_t+1); if (report_progress) pb = new ProgressBar("Training MovingAverage learner", train_set.length()-start); //train_stats->forget(); for (int t=start; t<train_set.length(); t++) { #ifdef DEBUG cout << "MovingAverage::train -- t = " << t << endl; #endif all_targets = train_set.subMat(t-window_length+1, target_pos, window_length, targetsize()); columnMean(all_targets,output); predictions(t) << output; if (t >= horizon) { Vec out = predictions(t-horizon); train_set->getSubRow(t, target_pos, target); if (!target.hasMissing() && !out.hasMissing()) { computeCostsFromOutputs(input, out, target, cost); errors(t) << cost; train_stats->update(cost); #ifdef DEBUG cout << "MovingAverage::train update train_stats pour t = " << t << endl; #endif } } if (pb) pb->update(t-start); } last_train_t = MAX(train_set.length()-1, last_train_t); #ifdef DEBUG cout << "MovingAverage.last_train_t = " << last_train_t << endl; #endif train_stats->finalize(); }
Reimplemented from PLearn::SequentialLearner.
Definition at line 88 of file MovingAverage.h.
Definition at line 54 of file MovingAverage.h.
Referenced by build_(), computeCostsFromOutputs(), declareOptions(), and getTrainCostNames().
Definition at line 53 of file MovingAverage.h.
Referenced by build_(), declareOptions(), test(), and train().