PLearn 0.1
|
#include <CorrelationKernel.h>
Public Member Functions | |
CorrelationKernel () | |
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 CorrelationKernel * | deepCopy (CopiesMap &copies) const |
virtual real | evaluate (const Vec &x1, const Vec &x2) const |
Compute K(x1,x2). | |
virtual void | setDataForKernelMatrix (VMat the_data) |
Overridden to precompute 'min_product_var' if necessary. | |
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 | correlation |
string | transform |
real | var_threshold |
Static Public Attributes | |
static StaticInitializer | _static_initializer_ |
Static Protected Member Functions | |
static void | declareOptions (OptionList &ol) |
Declares this class' options. | |
Protected Attributes | |
Mat | correl |
Used to store the correlations. | |
Mat | pvalues |
Used to store the pvalues. | |
Vec | mean_vec |
Used to store the mean of each example (variable). | |
Vec | var_vec |
Used to store the variance of each example (variable). | |
real | min_product_var |
Value used to threshold products of variances. | |
PP< VMatLanguage > | transform_prg |
VPL program that transforms the similarity measure. | |
TVec< string > | transform_prg_fields |
Fields of the VPL program. | |
Vec | result_vec |
Used to store the similarity measure. | |
Vec | result_transformed_vec |
Used to store the similarity measure tranformed by 'transform_prg'. | |
Private Types | |
typedef Kernel | inherited |
Private Member Functions | |
void | build_ () |
This does the actual building. |
Definition at line 52 of file CorrelationKernel.h.
typedef Kernel PLearn::CorrelationKernel::inherited [private] |
Reimplemented from PLearn::Kernel.
Definition at line 57 of file CorrelationKernel.h.
PLearn::CorrelationKernel::CorrelationKernel | ( | ) |
Default constructor.
Definition at line 55 of file CorrelationKernel.cc.
References transform_prg.
: correlation("linear"), transform(""), var_threshold(0) { transform_prg = new VMatLanguage(); }
string PLearn::CorrelationKernel::_classname_ | ( | ) | [static] |
Reimplemented from PLearn::Kernel.
Definition at line 72 of file CorrelationKernel.cc.
OptionList & PLearn::CorrelationKernel::_getOptionList_ | ( | ) | [static] |
Reimplemented from PLearn::Kernel.
Definition at line 72 of file CorrelationKernel.cc.
RemoteMethodMap & PLearn::CorrelationKernel::_getRemoteMethodMap_ | ( | ) | [static] |
Reimplemented from PLearn::Kernel.
Definition at line 72 of file CorrelationKernel.cc.
Reimplemented from PLearn::Kernel.
Definition at line 72 of file CorrelationKernel.cc.
Object * PLearn::CorrelationKernel::_new_instance_for_typemap_ | ( | ) | [static] |
Reimplemented from PLearn::Object.
Definition at line 72 of file CorrelationKernel.cc.
StaticInitializer CorrelationKernel::_static_initializer_ & PLearn::CorrelationKernel::_static_initialize_ | ( | ) | [static] |
Reimplemented from PLearn::Kernel.
Definition at line 72 of file CorrelationKernel.cc.
void PLearn::CorrelationKernel::build | ( | ) | [virtual] |
Simply calls inherited::build() then build_().
Reimplemented from PLearn::Kernel.
Definition at line 106 of file CorrelationKernel.cc.
References PLearn::Kernel::build(), and build_().
{ inherited::build(); build_(); }
void PLearn::CorrelationKernel::build_ | ( | ) | [private] |
This does the actual building.
Reimplemented from PLearn::Kernel.
Definition at line 115 of file CorrelationKernel.cc.
References PLearn::TVec< T >::resize(), result_transformed_vec, result_vec, transform, transform_prg, and transform_prg_fields.
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. transform_prg->setSourceFieldNames(TVec<string>(1)); // Dummy fieldnames. transform_prg->compileString(transform, transform_prg_fields); result_vec.resize(1); result_transformed_vec.resize(1); }
string PLearn::CorrelationKernel::classname | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 72 of file CorrelationKernel.cc.
void PLearn::CorrelationKernel::declareOptions | ( | OptionList & | ol | ) | [static, protected] |
Declares this class' options.
Reimplemented from PLearn::Kernel.
Definition at line 77 of file CorrelationKernel.cc.
References PLearn::OptionBase::buildoption, correlation, PLearn::declareOption(), PLearn::Kernel::declareOptions(), transform, and var_threshold.
{ // ### 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. Another possible flag to be combined with // ### is OptionBase::nosave declareOption(ol, "correlation", &CorrelationKernel::correlation, OptionBase::buildoption, "The correlation method used to compute the similarity, among:\n" "- 'linear' : linear correlation\n" "- 'mutual_information_norm' : normalized mutual information\n"); declareOption(ol, "transform", &CorrelationKernel::transform, OptionBase::buildoption, "An additional transformation applied on the similarity, in VPL language.\n"); declareOption(ol, "var_threshold", &CorrelationKernel::var_threshold, OptionBase::buildoption, "If set to a value > 0, denote by x_i the training point whose variance is the\n" "'var_threshold' quantile of all training variances. If v_i is its variance, then\n" "all pairs of points whose product of variances is less than v_i^2 will be given a\n" "similarity (correlation) of 1e-6.\n"); // Now call the parent class' declareOptions inherited::declareOptions(ol); }
static const PPath& PLearn::CorrelationKernel::declaringFile | ( | ) | [inline, static] |
Reimplemented from PLearn::Kernel.
Definition at line 132 of file CorrelationKernel.h.
CorrelationKernel * PLearn::CorrelationKernel::deepCopy | ( | CopiesMap & | copies | ) | const [virtual] |
Reimplemented from PLearn::Kernel.
Definition at line 72 of file CorrelationKernel.cc.
Compute K(x1,x2).
Implements PLearn::Kernel.
Definition at line 133 of file CorrelationKernel.cc.
References correl, correlation, PLearn::correlations(), PLearn::dot(), PLearn::exp(), PLearn::fast_is_equal(), PLearn::TVec< T >::length(), m, PLearn::mean(), min_product_var, n, PLearn::norm(), Pi, pl_log, PLERROR, pvalues, result_transformed_vec, result_vec, PLearn::sqrt(), PLearn::TVec< T >::toMat(), transform_prg, var_threshold, and PLearn::variance().
{ real result = 0; int n = x1.length(); #ifdef BOUNDCHECK if (x2.length() != n) PLERROR("In CorrelationKernel::evaluate - x1 and x2 must have same size"); #endif if (correlation == "linear") { VMat x1_(x1.toMat(n, 1)); VMat x2_(x2.toMat(n, 1)); x1_->defineSizes(n, 0, 0); x2_->defineSizes(n, 0, 0); correlations(x1_, x2_, correl, pvalues, true); result = correl(0,0); } else if (correlation == "mutual_information_norm") { Vec x1_c(n); Vec x2_c(n); x1_c << x1; x2_c << x2; x1_c -= mean(x1); x2_c -= mean(x2); real cos12 = dot(x1_c, x2_c) / (norm(x1_c) * norm(x2_c)); if (cos12 > 1) cos12 = 1; real sin12 = sqrt(1 - cos12 * cos12); // |sin(x1,x2)| if (fast_is_equal(sin12, 0.0, 1.0, 1e-5)) // The variables are considered equal. result = 1; else { real m = - pl_log(sin12); real epsilon = exp(0.5) / sqrt(2 * Pi); real sigma1 = sqrt(variance(x1_c.toMat(1,n), 0.0)); real sigma2 = sqrt(variance(x2_c.toMat(1,n), 0.0)); real h1 = 0.5 + pl_log(sqrt(2 * Pi) * (sigma1 + epsilon)); real h2 = 0.5 + pl_log(sqrt(2 * Pi) * (sigma2 + epsilon)); if (h1 <= 0 || h2 <= 0) PLERROR("In CorrelationKernel::evaluate - Entropy should always be > 0"); result = m / (sqrt(h1) * sqrt(h2)); } } else PLERROR("In CorrelationKernel::evaluate - Unknown value for 'correlation': " "%s", correlation.c_str()); if (var_threshold > 0) { real v_1 = variance(x1, mean(x1)); real v_2 = variance(x2, mean(x2)); if (v_1 * v_2 < min_product_var) result = 1e-6; } result_vec[0] = result; transform_prg->run(result_vec, result_transformed_vec); return result_transformed_vec[0]; /* if (transform.empty()) return result; else if (transform == "minus_log") return -log(result); else if (transform == "minus_log_abs") return -log(fabs(result)); else { PLERROR("In CorrelationKernel::evaluate - Unknown value for 'transform': " "%s", transform.c_str()); return 0; // To make the compiler happy. } */ }
OptionList & PLearn::CorrelationKernel::getOptionList | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 72 of file CorrelationKernel.cc.
OptionMap & PLearn::CorrelationKernel::getOptionMap | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 72 of file CorrelationKernel.cc.
RemoteMethodMap & PLearn::CorrelationKernel::getRemoteMethodMap | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 72 of file CorrelationKernel.cc.
void PLearn::CorrelationKernel::makeDeepCopyFromShallowCopy | ( | CopiesMap & | copies | ) | [virtual] |
Transforms a shallow copy into a deep copy.
Reimplemented from PLearn::Kernel.
Definition at line 211 of file CorrelationKernel.cc.
References correl, PLearn::deepCopyField(), PLearn::Kernel::makeDeepCopyFromShallowCopy(), mean_vec, pvalues, result_transformed_vec, result_vec, transform_prg, transform_prg_fields, and var_vec.
{ inherited::makeDeepCopyFromShallowCopy(copies); deepCopyField(correl, copies); deepCopyField(pvalues, copies); deepCopyField(mean_vec, copies); deepCopyField(var_vec, copies); deepCopyField(transform_prg, copies); deepCopyField(transform_prg_fields, copies); deepCopyField(result_vec, copies); deepCopyField(result_transformed_vec, copies); }
void PLearn::CorrelationKernel::setDataForKernelMatrix | ( | VMat | the_data | ) | [virtual] |
Overridden to precompute 'min_product_var' if necessary.
Reimplemented from PLearn::Kernel.
Definition at line 228 of file CorrelationKernel.cc.
References PLearn::computeMeanAndVariance(), PLearn::TVec< T >::length(), mean_vec, min_product_var, PLearn::Kernel::setDataForKernelMatrix(), PLearn::sortElements(), var_threshold, and var_vec.
{ inherited::setDataForKernelMatrix(the_data); if (var_threshold > 0) { // Compute variances. VMat transp = new TransposeVMatrix(the_data); computeMeanAndVariance(transp, mean_vec, var_vec); // Compute quantile. sortElements(var_vec); int q = (int) floor(var_threshold * (var_vec.length() - 1)); min_product_var = var_vec[q] * var_vec[q]; } }
Reimplemented from PLearn::Kernel.
Definition at line 132 of file CorrelationKernel.h.
Mat PLearn::CorrelationKernel::correl [mutable, protected] |
Used to store the correlations.
Definition at line 61 of file CorrelationKernel.h.
Referenced by evaluate(), and makeDeepCopyFromShallowCopy().
Definition at line 88 of file CorrelationKernel.h.
Referenced by declareOptions(), and evaluate().
Vec PLearn::CorrelationKernel::mean_vec [protected] |
Used to store the mean of each example (variable).
Definition at line 63 of file CorrelationKernel.h.
Referenced by makeDeepCopyFromShallowCopy(), and setDataForKernelMatrix().
real PLearn::CorrelationKernel::min_product_var [protected] |
Value used to threshold products of variances.
Definition at line 65 of file CorrelationKernel.h.
Referenced by evaluate(), and setDataForKernelMatrix().
Mat PLearn::CorrelationKernel::pvalues [mutable, protected] |
Used to store the pvalues.
Definition at line 62 of file CorrelationKernel.h.
Referenced by evaluate(), and makeDeepCopyFromShallowCopy().
Vec PLearn::CorrelationKernel::result_transformed_vec [protected] |
Used to store the similarity measure tranformed by 'transform_prg'.
Definition at line 73 of file CorrelationKernel.h.
Referenced by build_(), evaluate(), and makeDeepCopyFromShallowCopy().
Vec PLearn::CorrelationKernel::result_vec [protected] |
Used to store the similarity measure.
Definition at line 71 of file CorrelationKernel.h.
Referenced by build_(), evaluate(), and makeDeepCopyFromShallowCopy().
Definition at line 89 of file CorrelationKernel.h.
Referenced by build_(), and declareOptions().
PP<VMatLanguage> PLearn::CorrelationKernel::transform_prg [protected] |
VPL program that transforms the similarity measure.
Definition at line 67 of file CorrelationKernel.h.
Referenced by build_(), CorrelationKernel(), evaluate(), and makeDeepCopyFromShallowCopy().
TVec<string> PLearn::CorrelationKernel::transform_prg_fields [protected] |
Fields of the VPL program.
Definition at line 69 of file CorrelationKernel.h.
Referenced by build_(), and makeDeepCopyFromShallowCopy().
Definition at line 90 of file CorrelationKernel.h.
Referenced by declareOptions(), evaluate(), and setDataForKernelMatrix().
Vec PLearn::CorrelationKernel::var_vec [protected] |
Used to store the variance of each example (variable).
Definition at line 64 of file CorrelationKernel.h.
Referenced by makeDeepCopyFromShallowCopy(), and setDataForKernelMatrix().