PLearn 0.1
|
The first sentence should be a BRIEF DESCRIPTION of what the class does. More...
#include <PruningLinearRegressor.h>
Public Member Functions | |
PruningLinearRegressor () | |
Default constructor. | |
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 string | classname () const |
virtual OptionList & | getOptionList () const |
virtual OptionMap & | getOptionMap () const |
virtual RemoteMethodMap & | getRemoteMethodMap () const |
virtual PruningLinearRegressor * | deepCopy (CopiesMap &copies) const |
virtual void | build () |
simply calls inherited::build() then build_() | |
virtual void | makeDeepCopyFromShallowCopy (CopiesMap &copies) |
Transforms a shallow copy into a deep copy. | |
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 | |
string | pruning_method |
### declare public option fields (such as build options) here | |
real | min_t_ratio |
int | max_number |
real | max_fraction |
Static Public Attributes | |
static StaticInitializer | _static_initializer_ |
Protected Member Functions | |
void | computeTRatio () |
Utility function to compute the t-ratio for the estimator b (regression coefficients) | |
void | newDatasetIndices () |
Find the dataset indices corresponding to coefficients not pruned. | |
Static Protected Member Functions | |
static void | declareOptions (OptionList &ol) |
Declares the class options. | |
Protected Attributes | |
Vec | t_ratio |
t-ratio statistics for the estimator b (regression coefficients) Saved as a learned option to allow computing statistical significance of the weights when the model is reloaded and used in test mode. | |
TVec< int > | input_indices |
Indices of inputs kept for regression. | |
Private Types | |
typedef LinearRegressor | 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 PruningLinearRegressor.h.
typedef LinearRegressor PLearn::PruningLinearRegressor::inherited [private] |
Reimplemented from PLearn::LinearRegressor.
Definition at line 59 of file PruningLinearRegressor.h.
PLearn::PruningLinearRegressor::PruningLinearRegressor | ( | ) |
Default constructor.
Definition at line 55 of file PruningLinearRegressor.cc.
: pruning_method("max_number"), min_t_ratio(0.05), max_number(50), max_fraction(0.5) { }
string PLearn::PruningLinearRegressor::_classname_ | ( | ) | [static] |
Reimplemented from PLearn::LinearRegressor.
Definition at line 53 of file PruningLinearRegressor.cc.
OptionList & PLearn::PruningLinearRegressor::_getOptionList_ | ( | ) | [static] |
Reimplemented from PLearn::LinearRegressor.
Definition at line 53 of file PruningLinearRegressor.cc.
RemoteMethodMap & PLearn::PruningLinearRegressor::_getRemoteMethodMap_ | ( | ) | [static] |
Reimplemented from PLearn::LinearRegressor.
Definition at line 53 of file PruningLinearRegressor.cc.
Reimplemented from PLearn::LinearRegressor.
Definition at line 53 of file PruningLinearRegressor.cc.
Object * PLearn::PruningLinearRegressor::_new_instance_for_typemap_ | ( | ) | [static] |
Reimplemented from PLearn::LinearRegressor.
Definition at line 53 of file PruningLinearRegressor.cc.
StaticInitializer PruningLinearRegressor::_static_initializer_ & PLearn::PruningLinearRegressor::_static_initialize_ | ( | ) | [static] |
Reimplemented from PLearn::LinearRegressor.
Definition at line 53 of file PruningLinearRegressor.cc.
void PLearn::PruningLinearRegressor::build | ( | ) | [virtual] |
simply calls inherited::build() then build_()
Reimplemented from PLearn::LinearRegressor.
Definition at line 121 of file PruningLinearRegressor.cc.
References PLearn::LinearRegressor::build(), and build_().
{ inherited::build(); build_(); }
void PLearn::PruningLinearRegressor::build_ | ( | ) | [private] |
This does the actual building.
Reimplemented from PLearn::LinearRegressor.
Definition at line 100 of file PruningLinearRegressor.cc.
References max_number, min_t_ratio, PLERROR, and pruning_method.
Referenced by build().
{ if (pruning_method == "max_number") { if (max_number < 1) PLERROR("\"max_number\" should be strictly positive"); } else if (pruning_method == "max_fraction") { if (max_fraction <= 0.0 || max_fraction >= 1.0) PLERROR("\"max_fraction\" should be in range ]0,1["); } else if (pruning_method == "min_t_ratio") { if (min_t_ratio <= 0.0) PLERROR("\"min_t_ratio\" should be strictly positive"); } else PLERROR("Pruning method \"%s\" not supported", pruning_method.c_str()); }
string PLearn::PruningLinearRegressor::classname | ( | ) | const [virtual] |
Reimplemented from PLearn::LinearRegressor.
Definition at line 53 of file PruningLinearRegressor.cc.
void PLearn::PruningLinearRegressor::computeOutput | ( | const Vec & | input, |
Vec & | output | ||
) | const [virtual] |
Computes the output from the input.
Reimplemented from PLearn::LinearRegressor.
Definition at line 165 of file PruningLinearRegressor.cc.
References PLearn::LinearRegressor::computeOutput(), input_indices, PLearn::TVec< T >::length(), and PLearn::selectElements().
{ Vec actual_input(input_indices.length()); selectElements(input, input_indices, actual_input); inherited::computeOutput(actual_input, output); }
void PLearn::PruningLinearRegressor::computeTRatio | ( | ) | [protected] |
Utility function to compute the t-ratio for the estimator b (regression coefficients)
Definition at line 172 of file PruningLinearRegressor.cc.
References PLearn::abs(), PLearn::TMat< T >::copy(), i, PLearn::TMat< T >::length(), PLearn::TVec< T >::length(), PLearn::matInvert(), PLASSERT, PLearn::LinearRegressor::resid_variance, PLearn::TVec< T >::resize(), PLearn::sqrt(), t_ratio, PLearn::LinearRegressor::weights, PLearn::TMat< T >::width(), and PLearn::LinearRegressor::XtX.
Referenced by newDatasetIndices().
{ // We wish to compute the t-ratio of the estimator coefficients. // For that purpose, we use the following formula: // // t = |b| / sigma_b // // where b is the coefficients vector and sigma_b is the stderr matrix // of the estimator of b. The latter is computed as: // // sigma_b = s^2 * inverse(X'X) // // where s^2 is the residual variance (estimated using the // LinearRegressor::computeResidualsVariance method) // and X is the matrix of regressors.. const int ninputs = weights.length(); Mat sigma_b(ninputs, ninputs); t_ratio.resize(ninputs); // We compute the estimator PLASSERT(resid_variance.length() == 1); PLASSERT(weights.width() == 1); real residual_variance = resid_variance[0]; Mat XtX_copy = XtX.copy(); // matInvert overwrite the input matrix Mat XtX_inverse(XtX.length(), XtX.width()); matInvert(XtX_copy, XtX_inverse); for (int i=0; i<ninputs; i++) { real sigma_b = sqrt(residual_variance*XtX_inverse(i,i)); t_ratio[i] = abs(weights(i,0)) / sigma_b; } }
void PLearn::PruningLinearRegressor::declareOptions | ( | OptionList & | ol | ) | [static, protected] |
Declares the class options.
Reimplemented from PLearn::LinearRegressor.
Definition at line 62 of file PruningLinearRegressor.cc.
References PLearn::OptionBase::buildoption, PLearn::declareOption(), PLearn::LinearRegressor::declareOptions(), input_indices, PLearn::OptionBase::learntoption, max_fraction, max_number, min_t_ratio, pruning_method, and t_ratio.
{ //##### Build Options #################################################### declareOption(ol, "pruning_method", &PruningLinearRegressor::pruning_method, OptionBase::buildoption, "The pruning method:\n" " - \"max_number\" = keep only the weights with the k-best t-ratio\n" " - \"max_fraction\" = same as \"max_number\", but using a fraction rather than a hard threshold\n" " - \"min_t_ratio\" = keep only the weights with t-ratio > min_t_ratio"); declareOption(ol, "min_t_ratio", &PruningLinearRegressor::min_t_ratio, OptionBase::buildoption, "Minimum t-ratio for not pruning a coefficient"); declareOption(ol, "max_number", &PruningLinearRegressor::max_number, OptionBase::buildoption, "Maximum number of coefficients (the default)"); declareOption(ol, "max_fraction", &PruningLinearRegressor::max_fraction, OptionBase::buildoption, "Maximum fraction (in [0,1]) of coefficients"); //##### Learnt Options ################################################### declareOption(ol, "t_ratio", &PruningLinearRegressor::t_ratio, OptionBase::learntoption, "t-ratio statistics for the estimator b (regression coefficients)\n" "Saved as a learned option to allow computing statistical significance\n" "of the coefficients when the model is reloaded and used in test mode."); declareOption(ol, "input_indices", &PruningLinearRegressor::input_indices, OptionBase::learntoption, "Indices of inputs kept for regression"); inherited::declareOptions(ol); }
static const PPath& PLearn::PruningLinearRegressor::declaringFile | ( | ) | [inline, static] |
Reimplemented from PLearn::LinearRegressor.
Definition at line 92 of file PruningLinearRegressor.h.
:
//##### Protected Options ###############################################
PruningLinearRegressor * PLearn::PruningLinearRegressor::deepCopy | ( | CopiesMap & | copies | ) | const [virtual] |
Reimplemented from PLearn::LinearRegressor.
Definition at line 53 of file PruningLinearRegressor.cc.
OptionList & PLearn::PruningLinearRegressor::getOptionList | ( | ) | const [virtual] |
Reimplemented from PLearn::LinearRegressor.
Definition at line 53 of file PruningLinearRegressor.cc.
OptionMap & PLearn::PruningLinearRegressor::getOptionMap | ( | ) | const [virtual] |
Reimplemented from PLearn::LinearRegressor.
Definition at line 53 of file PruningLinearRegressor.cc.
RemoteMethodMap & PLearn::PruningLinearRegressor::getRemoteMethodMap | ( | ) | const [virtual] |
Reimplemented from PLearn::LinearRegressor.
Definition at line 53 of file PruningLinearRegressor.cc.
void PLearn::PruningLinearRegressor::makeDeepCopyFromShallowCopy | ( | CopiesMap & | copies | ) | [virtual] |
Transforms a shallow copy into a deep copy.
Reimplemented from PLearn::LinearRegressor.
Definition at line 127 of file PruningLinearRegressor.cc.
References PLearn::deepCopyField(), input_indices, PLearn::LinearRegressor::makeDeepCopyFromShallowCopy(), and t_ratio.
{ inherited::makeDeepCopyFromShallowCopy(copies); deepCopyField(t_ratio, copies); deepCopyField(input_indices, copies); }
void PLearn::PruningLinearRegressor::newDatasetIndices | ( | ) | [protected] |
Find the dataset indices corresponding to coefficients not pruned.
Definition at line 207 of file PruningLinearRegressor.cc.
References PLearn::TVec< T >::append(), computeTRatio(), PLearn::TVec< T >::copy(), i, PLearn::LinearRegressor::include_bias, input_indices, PLearn::TMat< T >::length(), PLearn::TVec< T >::length(), max_fraction, max_number, PLearn::min(), min_t_ratio, PLASSERT, pruning_method, PLearn::TVec< T >::resize(), PLearn::sortElements(), t_ratio, and PLearn::LinearRegressor::weights.
Referenced by train().
{ // Compute first the t-ratios computeTRatio(); // Sort all t-ratios int nb_weights = weights.length(); PLASSERT(nb_weights == t_ratio.length()); Vec t_ratio_sort = t_ratio.copy(); sortElements(t_ratio_sort, true); // Find the t-ratio threshold real t_ratio_threshold = 0.0; if (pruning_method == "max_number") { int keep_n_weights = min(max_number, nb_weights); // Add one coefficient if max is not yet reached if (include_bias && keep_n_weights<nb_weights) ++keep_n_weights; t_ratio_threshold = t_ratio_sort[keep_n_weights-1]; } else if (pruning_method == "max_fraction") { int keep_n_weights = max_fraction*nb_weights; t_ratio_threshold = t_ratio_sort[keep_n_weights-1]; } else if (pruning_method == "min_t_ratio") { t_ratio_threshold = min_t_ratio; } // Find kept (not pruned) coefficient indices input_indices.resize(0); int offset = include_bias ? 1 : 0; for (int i=0; i<nb_weights; i++) { if (t_ratio[i] >= t_ratio_threshold) { int data_index = i - offset; if (data_index >= 0) // = -1 for bias input_indices.append(data_index); } } }
void PLearn::PruningLinearRegressor::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 134 of file PruningLinearRegressor.cc.
References PLERROR, PLearn::PLearner::setTrainingSet(), and PLearn::PLearner::targetsize().
Referenced by train().
{ inherited::setTrainingSet(training_set, call_forget); if (targetsize() > 1) PLERROR("PruningLinearRegressor works only with single target problems"); }
void PLearn::PruningLinearRegressor::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.
Reimplemented from PLearn::LinearRegressor.
Definition at line 141 of file PruningLinearRegressor.cc.
References PLearn::TVec< T >::append(), PLearn::VMat::columns(), PLearn::TVec< T >::copy(), input_indices, PLearn::PLearner::inputsize(), PLearn::TVec< T >::length(), newDatasetIndices(), setTrainingSet(), PLearn::LinearRegressor::train(), PLearn::PLearner::train_set, and PLearn::PLearner::weightsize().
{ // train with all coefficients inherited::train(); // find the dataset indices corresponding to coefficients not pruned newDatasetIndices(); // Add target and (possibly) weight indices TVec<int> all_indices = input_indices.copy(); all_indices.append(inputsize()); // target index = inputsize if (weightsize()) all_indices.append(inputsize()+1); // weight index = target index + 1 // Build new training set VMat new_trainset = train_set.columns(all_indices); int new_inputsize = input_indices.length(); new_trainset->defineSizes(new_inputsize, 1, weightsize()); // Train with this new training set setTrainingSet(new_trainset); inherited::train(); }
Reimplemented from PLearn::LinearRegressor.
Definition at line 92 of file PruningLinearRegressor.h.
TVec<int> PLearn::PruningLinearRegressor::input_indices [protected] |
Indices of inputs kept for regression.
Definition at line 113 of file PruningLinearRegressor.h.
Referenced by computeOutput(), declareOptions(), makeDeepCopyFromShallowCopy(), newDatasetIndices(), and train().
Definition at line 68 of file PruningLinearRegressor.h.
Referenced by declareOptions(), and newDatasetIndices().
Definition at line 67 of file PruningLinearRegressor.h.
Referenced by build_(), declareOptions(), and newDatasetIndices().
Definition at line 66 of file PruningLinearRegressor.h.
Referenced by build_(), declareOptions(), and newDatasetIndices().
### declare public option fields (such as build options) here
Definition at line 65 of file PruningLinearRegressor.h.
Referenced by build_(), declareOptions(), and newDatasetIndices().
Vec PLearn::PruningLinearRegressor::t_ratio [protected] |
t-ratio statistics for the estimator b (regression coefficients) Saved as a learned option to allow computing statistical significance of the weights when the model is reloaded and used in test mode.
Definition at line 110 of file PruningLinearRegressor.h.
Referenced by computeTRatio(), declareOptions(), makeDeepCopyFromShallowCopy(), and newDatasetIndices().