PLearn 0.1
|
This class implements an Ln distance (defaults to L2 i.e. euclidean distance). More...
#include <DistanceKernel.h>
Public Member Functions | |
DistanceKernel (real the_Ln=2, bool powdist=false) | |
virtual string | classname () const |
virtual OptionList & | getOptionList () const |
virtual OptionMap & | getOptionMap () const |
virtual RemoteMethodMap & | getRemoteMethodMap () const |
virtual DistanceKernel * | deepCopy (CopiesMap &copies) const |
virtual string | info () const |
Returns a bit more informative string about object (default returns classname()) | |
virtual real | evaluate (const Vec &x1, const Vec &x2) const |
** Subclasses must override this method ** | |
virtual real | evaluate_i_j (int i, int j) const |
returns evaluate(data(i),data(j)) | |
virtual void | setDataForKernelMatrix (VMat the_data) |
This method precomputes the squared norm for all the data to later speed up evaluate methods, if n == 2. | |
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 | optimized |
bool | pow_distance |
bool | ignore_missing |
Static Public Attributes | |
static StaticInitializer | _static_initializer_ |
Static Protected Member Functions | |
static void | declareOptions (OptionList &ol) |
Declare options (data fields) for the class. | |
Protected Attributes | |
real | n |
1 for L1, 2 for L2, etc... | |
Vec | squarednorms |
Used to store the squared norm of the input data. | |
Private Types | |
typedef Kernel | inherited |
This class implements an Ln distance (defaults to L2 i.e. euclidean distance).
Definition at line 53 of file DistanceKernel.h.
typedef Kernel PLearn::DistanceKernel::inherited [private] |
Reimplemented from PLearn::Kernel.
Reimplemented in PLearn::CosKernel.
Definition at line 58 of file DistanceKernel.h.
Definition at line 62 of file DistanceKernel.cc.
: n(the_Ln), optimized(false), pow_distance(pd), ignore_missing(false) {}
string PLearn::DistanceKernel::_classname_ | ( | ) | [static] |
Reimplemented from PLearn::Kernel.
Reimplemented in PLearn::CosKernel.
Definition at line 57 of file DistanceKernel.cc.
OptionList & PLearn::DistanceKernel::_getOptionList_ | ( | ) | [static] |
Reimplemented from PLearn::Kernel.
Reimplemented in PLearn::CosKernel.
Definition at line 57 of file DistanceKernel.cc.
RemoteMethodMap & PLearn::DistanceKernel::_getRemoteMethodMap_ | ( | ) | [static] |
Reimplemented from PLearn::Kernel.
Reimplemented in PLearn::CosKernel.
Definition at line 57 of file DistanceKernel.cc.
Reimplemented from PLearn::Kernel.
Reimplemented in PLearn::CosKernel.
Definition at line 57 of file DistanceKernel.cc.
Object * PLearn::DistanceKernel::_new_instance_for_typemap_ | ( | ) | [static] |
Reimplemented from PLearn::Object.
Reimplemented in PLearn::CosKernel.
Definition at line 57 of file DistanceKernel.cc.
StaticInitializer DistanceKernel::_static_initializer_ & PLearn::DistanceKernel::_static_initialize_ | ( | ) | [static] |
Reimplemented from PLearn::Kernel.
Reimplemented in PLearn::CosKernel.
Definition at line 57 of file DistanceKernel.cc.
string PLearn::DistanceKernel::classname | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Reimplemented in PLearn::CosKernel.
Definition at line 57 of file DistanceKernel.cc.
void PLearn::DistanceKernel::declareOptions | ( | OptionList & | ol | ) | [static, protected] |
Declare options (data fields) for the class.
Redefine this in subclasses: call declareOption
(...) for each option, and then call inherited::declareOptions(options)
. Please call the inherited
method AT THE END to get the options listed in a consistent order (from most recently defined to least recently defined).
static void MyDerivedClass::declareOptions(OptionList& ol) { declareOption(ol, "inputsize", &MyObject::inputsize_, OptionBase::buildoption, "The size of the input; it must be provided"); declareOption(ol, "weights", &MyObject::weights, OptionBase::learntoption, "The learned model weights"); inherited::declareOptions(ol); }
ol | List of options that is progressively being constructed for the current class. |
Reimplemented from PLearn::Kernel.
Reimplemented in PLearn::CosKernel.
Definition at line 72 of file DistanceKernel.cc.
References PLearn::OptionBase::buildoption, PLearn::declareOption(), PLearn::Kernel::declareOptions(), ignore_missing, n, optimized, and pow_distance.
{ declareOption(ol, "n", &DistanceKernel::n, OptionBase::buildoption, "This class implements a Ln distance (L2, the default is the usual euclidean distance)."); declareOption(ol, "pow_distance", &DistanceKernel::pow_distance, OptionBase::buildoption, "If set to 1, the distance computed will be elevated to power n."); declareOption(ol, "optimized", &DistanceKernel::optimized, OptionBase::buildoption, "If set to 1, the evaluate_i_j method will be faster, at the cost of potential\n" "approximations in the result."); declareOption(ol, "ignore_missing", &DistanceKernel::ignore_missing, OptionBase::buildoption, "If set to false, nan will be propagated.\n" "If set to true, if a value is missing in the matrix of some examples, we will ignore this value for the distance\n" "If set to true, work only if pow_distance is set to 1."); inherited::declareOptions(ol); }
static const PPath& PLearn::DistanceKernel::declaringFile | ( | ) | [inline, static] |
Reimplemented from PLearn::Kernel.
Reimplemented in PLearn::CosKernel.
Definition at line 74 of file DistanceKernel.h.
DistanceKernel * PLearn::DistanceKernel::deepCopy | ( | CopiesMap & | copies | ) | const [virtual] |
Reimplemented from PLearn::Kernel.
Reimplemented in PLearn::CosKernel.
Definition at line 57 of file DistanceKernel.cc.
** Subclasses must override this method **
returns K(x1,x2)
Implements PLearn::Kernel.
Reimplemented in PLearn::CosKernel.
Definition at line 96 of file DistanceKernel.cc.
References PLearn::dist(), ignore_missing, n, PLERROR, pow_distance, and PLearn::powdistance().
Referenced by PLearn::RemoveDuplicateVMatrix::build_().
{ if (ignore_missing && !pow_distance) PLERROR("In DistanceKernel::evaluate(int i, int j) - 'ignore_missing' " "implemented only if pow_distance is set"); if (pow_distance) { return powdistance(x1, x2, n, ignore_missing); } else { return dist(x1, x2, n); } }
returns evaluate(data(i),data(j))
Reimplemented from PLearn::Kernel.
Reimplemented in PLearn::CosKernel.
Definition at line 111 of file DistanceKernel.cc.
References d, PLearn::Kernel::data, PLearn::Kernel::data_inputsize, PLearn::Kernel::evaluate_i_j(), PLearn::fast_exact_is_equal(), i, ignore_missing, j, n, optimized, PLERROR, pow_distance, PLearn::sqrt(), and squarednorms.
{ static real d; if (ignore_missing) PLERROR("DistanceKernel::evaluate_i_j(int i, int j) not implemented for ignore_missing"); if (optimized && fast_exact_is_equal(n, 2.0)) { if (i == j) // The case 'i == j' can cause precision issues because of the optimized // formula below. Thus we make sure we always return 0. return 0; d = squarednorms[i] + squarednorms[j] - 2 * data->dot(i, j, data_inputsize); if (d < 0) { // This can happen (especially when compiled in -opt) if the two points // are the same, and the distance should be zero. if (d < -1e-2) // That should not happen. PLERROR("In DistanceKernel::evaluate_i_j - Found a (significantly) negative distance (%f), " "i = %d, j = %d, squarednorms[i] = %f, squarednorms[j] = %f, dot = %f", d, i, j, squarednorms[i], squarednorms[j], data->dot(i, j, data_inputsize)); d = 0; } if (pow_distance) return d; else return sqrt(d); } else { return inherited::evaluate_i_j(i,j); } }
OptionList & PLearn::DistanceKernel::getOptionList | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Reimplemented in PLearn::CosKernel.
Definition at line 57 of file DistanceKernel.cc.
OptionMap & PLearn::DistanceKernel::getOptionMap | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Reimplemented in PLearn::CosKernel.
Definition at line 57 of file DistanceKernel.cc.
RemoteMethodMap & PLearn::DistanceKernel::getRemoteMethodMap | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Reimplemented in PLearn::CosKernel.
Definition at line 57 of file DistanceKernel.cc.
virtual string PLearn::DistanceKernel::info | ( | ) | const [inline, virtual] |
Returns a bit more informative string about object (default returns classname())
Reimplemented from PLearn::Object.
Definition at line 76 of file DistanceKernel.h.
References n, and PLearn::tostring().
void PLearn::DistanceKernel::setDataForKernelMatrix | ( | VMat | the_data | ) | [virtual] |
This method precomputes the squared norm for all the data to later speed up evaluate methods, if n == 2.
Reimplemented from PLearn::Kernel.
Definition at line 144 of file DistanceKernel.cc.
References PLearn::Kernel::data, PLearn::Kernel::data_inputsize, PLearn::fast_exact_is_equal(), ignore_missing, PLearn::VMat::length(), n, PLWARNING, PLearn::TVec< T >::resize(), PLearn::Kernel::setDataForKernelMatrix(), and squarednorms.
Referenced by PLearn::RemoveDuplicateVMatrix::build_(), PLearn::KNNVMatrix::build_(), PLearn::NonLocalManifoldParzen::knn(), PLearn::GaussianContinuumDistribution::knn(), and PLearn::GaussianContinuum::knn().
{ if (ignore_missing) PLWARNING("DistanceKernel::setDataForKernelMatrix(VMat the_data) not tested for ignore_missing"); inherited::setDataForKernelMatrix(the_data); if (fast_exact_is_equal(n, 2.0)) { squarednorms.resize(data.length()); for(int index=0; index<data.length(); index++) { squarednorms[index] = data->dot(index, index, data_inputsize); } } }
Reimplemented from PLearn::Kernel.
Reimplemented in PLearn::CosKernel.
Definition at line 74 of file DistanceKernel.h.
Definition at line 70 of file DistanceKernel.h.
Referenced by declareOptions(), PLearn::CosKernel::declareOptions(), evaluate(), evaluate_i_j(), and setDataForKernelMatrix().
real PLearn::DistanceKernel::n [protected] |
1 for L1, 2 for L2, etc...
Definition at line 62 of file DistanceKernel.h.
Referenced by declareOptions(), PLearn::CosKernel::declareOptions(), evaluate(), evaluate_i_j(), and setDataForKernelMatrix().
Definition at line 68 of file DistanceKernel.h.
Referenced by declareOptions(), PLearn::CosKernel::declareOptions(), and evaluate_i_j().
Definition at line 69 of file DistanceKernel.h.
Referenced by PLearn::RemoveDuplicateVMatrix::build_(), declareOptions(), PLearn::CosKernel::declareOptions(), evaluate(), and evaluate_i_j().
Vec PLearn::DistanceKernel::squarednorms [protected] |
Used to store the squared norm of the input data.
Definition at line 64 of file DistanceKernel.h.
Referenced by evaluate_i_j(), and setDataForKernelMatrix().