PLearn 0.1
|
The first sentence should be a BRIEF DESCRIPTION of what the class does. More...
#include <AutoLinearRegressor.h>
Public Member Functions | |
AutoLinearRegressor () | |
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 string | classname () const |
virtual OptionList & | getOptionList () const |
virtual OptionMap & | getOptionMap () const |
virtual RemoteMethodMap & | getRemoteMethodMap () const |
virtual AutoLinearRegressor * | 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. | |
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 | |
bool | include_bias |
### declare public option fields (such as build options) here Start your comments with Doxygen-compatible comments such as //! Whether to include a bias term in the regression (true by default) | |
real | min_weight_decay |
real | weight_decay |
Mat | weights |
Vec | mean_target |
Static Public Attributes | |
static StaticInitializer | _static_initializer_ |
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. | |
Private Attributes | |
real | weights_norm |
Sum of squares of weights. | |
Vec | extendedinput |
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 AutoLinearRegressor.h.
typedef PLearner PLearn::AutoLinearRegressor::inherited [private] |
Reimplemented from PLearn::PLearner.
Definition at line 59 of file AutoLinearRegressor.h.
PLearn::AutoLinearRegressor::AutoLinearRegressor | ( | ) |
Default constructor.
Definition at line 54 of file AutoLinearRegressor.cc.
: include_bias(false), min_weight_decay(1e-6), weight_decay(0.0) { }
string PLearn::AutoLinearRegressor::_classname_ | ( | ) | [static] |
Reimplemented from PLearn::PLearner.
Definition at line 52 of file AutoLinearRegressor.cc.
OptionList & PLearn::AutoLinearRegressor::_getOptionList_ | ( | ) | [static] |
Reimplemented from PLearn::PLearner.
Definition at line 52 of file AutoLinearRegressor.cc.
RemoteMethodMap & PLearn::AutoLinearRegressor::_getRemoteMethodMap_ | ( | ) | [static] |
Reimplemented from PLearn::PLearner.
Definition at line 52 of file AutoLinearRegressor.cc.
Reimplemented from PLearn::PLearner.
Definition at line 52 of file AutoLinearRegressor.cc.
Object * PLearn::AutoLinearRegressor::_new_instance_for_typemap_ | ( | ) | [static] |
Reimplemented from PLearn::Object.
Definition at line 52 of file AutoLinearRegressor.cc.
StaticInitializer AutoLinearRegressor::_static_initializer_ & PLearn::AutoLinearRegressor::_static_initialize_ | ( | ) | [static] |
Reimplemented from PLearn::PLearner.
Definition at line 52 of file AutoLinearRegressor.cc.
void PLearn::AutoLinearRegressor::build | ( | ) | [virtual] |
Finish building the object; just call inherited::build followed by build_()
Reimplemented from PLearn::PLearner.
Definition at line 121 of file AutoLinearRegressor.cc.
References PLearn::PLearner::build(), and build_().
{ inherited::build(); build_(); }
void PLearn::AutoLinearRegressor::build_ | ( | ) | [private] |
This does the actual building.
Reimplemented from PLearn::PLearner.
Definition at line 106 of file AutoLinearRegressor.cc.
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. }
string PLearn::AutoLinearRegressor::classname | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 52 of file AutoLinearRegressor.cc.
void PLearn::AutoLinearRegressor::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 248 of file AutoLinearRegressor.cc.
References PLearn::powdistance(), PLearn::TVec< T >::resize(), weight_decay, and weights_norm.
{ // Compute the costs from *already* computed output. costs.resize(2); real squared_loss = powdistance(output,target); costs[0] = squared_loss + weight_decay*weights_norm; costs[1] = squared_loss; }
Computes the output from the input.
Reimplemented from PLearn::PLearner.
Definition at line 230 of file AutoLinearRegressor.cc.
References extendedinput, include_bias, PLearn::PLearner::inputsize(), mean_target, outputsize(), PLearn::product(), PLearn::TVec< T >::resize(), PLearn::TVec< T >::subVec(), and weights.
{ // Compute the output from the input int nout = outputsize(); output.resize(nout); if(!include_bias) product(output,weights,input); else { int nin = inputsize(); extendedinput.resize(1+nin); extendedinput.subVec(1,nin) << input; extendedinput[0] = 1.0; product(output,weights,extendedinput); } output += mean_target; }
void PLearn::AutoLinearRegressor::declareOptions | ( | OptionList & | ol | ) | [static, protected] |
Declares the class options.
Reimplemented from PLearn::PLearner.
Definition at line 61 of file AutoLinearRegressor.cc.
References PLearn::OptionBase::buildoption, PLearn::declareOption(), PLearn::PLearner::declareOptions(), include_bias, PLearn::OptionBase::learntoption, mean_target, min_weight_decay, weight_decay, and weights.
{ // ### Declare all of this object's options here. // ### For the "flags" of each option, you should typically specify // ### one of OptionBase::buildoption, OptionBase::learntoption or // ### OptionBase::tuningoption. If you don't provide one of these three, // ### this option will be ignored when loading values from a script. // ### You can also combine flags, for example with OptionBase::nosave: // ### (OptionBase::buildoption | OptionBase::nosave) // ### ex: // declareOption(ol, "myoption", &AutoLinearRegressor::myoption, // OptionBase::buildoption, // "Help text describing this option"); // ... declareOption(ol, "include_bias", &AutoLinearRegressor::include_bias, OptionBase::buildoption, "Whether to include a bias term in the regression \n" "Note: this is currently ignored.\n"); declareOption(ol, "min_weight_decay", &AutoLinearRegressor::min_weight_decay, OptionBase::buildoption, "The minimum weight decay to try."); declareOption(ol, "weight_decay", &AutoLinearRegressor::weight_decay, OptionBase::learntoption, "The weight decay is the factor that multiplies the \n" "squared norm of the parameters in the loss function.\n" "It is automatically tuned by the algorithm \n"); declareOption(ol, "weights", &AutoLinearRegressor::weights, OptionBase::learntoption, "The weight matrix, which are the parameters computed by " "training the regressor.\n"); declareOption(ol, "mean_target", &AutoLinearRegressor::mean_target, OptionBase::learntoption, "The mean of the target (used as a default bias)."); // Now call the parent class' declareOptions inherited::declareOptions(ol); }
static const PPath& PLearn::AutoLinearRegressor::declaringFile | ( | ) | [inline, static] |
Reimplemented from PLearn::PLearner.
Definition at line 145 of file AutoLinearRegressor.h.
:
//##### Protected Options ###############################################
AutoLinearRegressor * PLearn::AutoLinearRegressor::deepCopy | ( | CopiesMap & | copies | ) | const [virtual] |
Reimplemented from PLearn::PLearner.
Definition at line 52 of file AutoLinearRegressor.cc.
void PLearn::AutoLinearRegressor::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 152 of file AutoLinearRegressor.cc.
References PLearn::PLearner::forget(), PLearn::TMat< T >::resize(), PLearn::PLearner::stage, and weights.
{ weights.resize(0,0); stage = 0; inherited::forget(); }
OptionList & PLearn::AutoLinearRegressor::getOptionList | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 52 of file AutoLinearRegressor.cc.
OptionMap & PLearn::AutoLinearRegressor::getOptionMap | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 52 of file AutoLinearRegressor.cc.
RemoteMethodMap & PLearn::AutoLinearRegressor::getRemoteMethodMap | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 52 of file AutoLinearRegressor.cc.
TVec< string > PLearn::AutoLinearRegressor::getTestCostNames | ( | ) | const [virtual] |
Returns the names of the costs computed by computeCostsFromOutpus (and thus the test method).
Implements PLearn::PLearner.
Definition at line 258 of file AutoLinearRegressor.cc.
References PLearn::TVec< T >::push_back().
{ TVec<string> names; names.push_back("mse+penalty"); names.push_back("mse"); return names; }
TVec< string > PLearn::AutoLinearRegressor::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 266 of file AutoLinearRegressor.cc.
References PLearn::TVec< T >::push_back().
{ TVec<string> names; names.push_back("GCV_mse"); names.push_back("mse"); return names; }
void PLearn::AutoLinearRegressor::makeDeepCopyFromShallowCopy | ( | CopiesMap & | copies | ) | [virtual] |
Transforms a shallow copy into a deep copy.
Reimplemented from PLearn::PLearner.
Definition at line 128 of file AutoLinearRegressor.cc.
References PLearn::deepCopyField(), extendedinput, PLearn::PLearner::makeDeepCopyFromShallowCopy(), mean_target, and weights.
{ // ### 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("AutoLinearRegressor::makeDeepCopyFromShallowCopy not fully (correctly) implemented yet!"); inherited::makeDeepCopyFromShallowCopy(copies); deepCopyField(weights, copies); deepCopyField(mean_target, copies); deepCopyField(extendedinput, copies); }
int PLearn::AutoLinearRegressor::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 147 of file AutoLinearRegressor.cc.
References PLearn::PLearner::targetsize().
Referenced by computeOutput().
{ return targetsize(); }
void PLearn::AutoLinearRegressor::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 159 of file AutoLinearRegressor.cc.
References PLearn::TMat< T >::column(), PLearn::columnMean(), PLearn::dot(), PLearn::TVec< T >::fill(), PLearn::hconcat(), i, include_bias, PLearn::PLearner::initTrain(), PLearn::TMat< T >::length(), mean_target, min_weight_decay, PLearn::multiplyAcc(), PLearn::TMat< T >::resize(), PLearn::TVec< T >::resize(), PLearn::PLearner::stage, PLearn::TMat< T >::subMatColumns(), PLearn::sum(), PLearn::TMat< T >::toVecCopy(), PLearn::PLearner::train_set, PLearn::PLearner::train_stats, weight_decay, PLearn::weightedRidgeRegressionByGCV(), weights, and weights_norm.
{ // The role of the train method is to bring the learner up to // stage==nstages, updating train_stats with training costs measured // on-line in the process. // This generic PLearner method does a number of standard stuff useful for // (almost) any learner, and return 'false' if no training should take // place. See PLearner.h for more details. if (!initTrain()) return; if(stage<1) { // clear statistics of previous epoch train_stats->forget(); int ninputs = train_set->inputsize(); int ntargets = train_set->targetsize(); int nweights = train_set->weightsize(); Mat tset = train_set->toMatCopy(); int l = tset.length(); Mat X = tset.subMatColumns(0,ninputs); Mat Y = tset.subMatColumns(ninputs, ntargets); Vec gamma; // the weights if (include_bias) { Mat col_ones = Mat(l, 1, 1.0); X = hconcat(col_ones, X); } mean_target.resize(ntargets); mean_target.fill(0); if(nweights!=0) { gamma = tset.column(ninputs+ntargets).toVecCopy(); for(int i=0; i<l; i++) multiplyAcc(mean_target, Y(i), gamma[i]); mean_target /= sum(gamma); } else columnMean(Y, mean_target); Y -= mean_target; int insize = ninputs + (include_bias ? 1 : 0); //weights.resize(insize, ntargets); weights.resize(ntargets, insize); real best_GCV; weight_decay = weightedRidgeRegressionByGCV(X, Y, gamma, weights, best_GCV, min_weight_decay); //Mat weights_excluding_biases = weights.subMatRows(include_bias? 1 : 0, ninputs); Mat weights_excluding_biases = weights.subMatColumns(include_bias? 1 : 0, ninputs); weights_norm = dot(weights_excluding_biases,weights_excluding_biases); //Vec trcosts(1); Vec trcosts(2); trcosts[0] = best_GCV; trcosts[1] = best_GCV; train_stats->update(trcosts); ++stage; train_stats->finalize(); // finalize statistics for this epoch } }
Reimplemented from PLearn::PLearner.
Definition at line 145 of file AutoLinearRegressor.h.
Vec PLearn::AutoLinearRegressor::extendedinput [mutable, private] |
Definition at line 181 of file AutoLinearRegressor.h.
Referenced by computeOutput(), and makeDeepCopyFromShallowCopy().
### declare public option fields (such as build options) here Start your comments with Doxygen-compatible comments such as //! Whether to include a bias term in the regression (true by default)
Definition at line 67 of file AutoLinearRegressor.h.
Referenced by computeOutput(), declareOptions(), and train().
Definition at line 75 of file AutoLinearRegressor.h.
Referenced by computeOutput(), declareOptions(), makeDeepCopyFromShallowCopy(), and train().
Definition at line 69 of file AutoLinearRegressor.h.
Referenced by declareOptions(), and train().
Definition at line 71 of file AutoLinearRegressor.h.
Referenced by computeCostsFromOutputs(), declareOptions(), and train().
Definition at line 73 of file AutoLinearRegressor.h.
Referenced by computeOutput(), declareOptions(), forget(), makeDeepCopyFromShallowCopy(), and train().
Sum of squares of weights.
Definition at line 180 of file AutoLinearRegressor.h.
Referenced by computeCostsFromOutputs(), and train().