PLearn 0.1
Public Member Functions | Static Public Member Functions | Public Attributes | Static Public Attributes | Static Protected Member Functions | Protected Attributes | Private Types
PLearn::DistanceKernel Class Reference

This class implements an Ln distance (defaults to L2 i.e. euclidean distance). More...

#include <DistanceKernel.h>

Inheritance diagram for PLearn::DistanceKernel:
Inheritance graph
[legend]
Collaboration diagram for PLearn::DistanceKernel:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 DistanceKernel (real the_Ln=2, bool powdist=false)
virtual string classname () const
virtual OptionListgetOptionList () const
virtual OptionMapgetOptionMap () const
virtual RemoteMethodMapgetRemoteMethodMap () const
virtual DistanceKerneldeepCopy (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 PPathdeclaringFile ()

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

Detailed Description

This class implements an Ln distance (defaults to L2 i.e. euclidean distance).

Definition at line 53 of file DistanceKernel.h.


Member Typedef Documentation

Reimplemented from PLearn::Kernel.

Reimplemented in PLearn::CosKernel.

Definition at line 58 of file DistanceKernel.h.


Constructor & Destructor Documentation

PLearn::DistanceKernel::DistanceKernel ( real  the_Ln = 2,
bool  powdist = false 
)

Definition at line 62 of file DistanceKernel.cc.

    : n(the_Ln),
      optimized(false),
      pow_distance(pd),
      ignore_missing(false)
{}

Member Function Documentation

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.

bool PLearn::DistanceKernel::_isa_ ( const Object o) [static]

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);
  }
Parameters:
olList 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);
}

Here is the call graph for this function:

static const PPath& PLearn::DistanceKernel::declaringFile ( ) [inline, static]

Reimplemented from PLearn::Kernel.

Reimplemented in PLearn::CosKernel.

Definition at line 74 of file DistanceKernel.h.

{ return "L"+tostring(n); }
DistanceKernel * PLearn::DistanceKernel::deepCopy ( CopiesMap copies) const [virtual]

Reimplemented from PLearn::Kernel.

Reimplemented in PLearn::CosKernel.

Definition at line 57 of file DistanceKernel.cc.

real PLearn::DistanceKernel::evaluate ( const Vec x1,
const Vec x2 
) const [virtual]

** 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);
    }
}

Here is the call graph for this function:

Here is the caller graph for this function:

real PLearn::DistanceKernel::evaluate_i_j ( int  i,
int  j 
) const [virtual]

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);
    }
}

Here is the call graph for this function:

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())

Returns:
Information about the object

Reimplemented from PLearn::Object.

Definition at line 76 of file DistanceKernel.h.

References n, and PLearn::tostring().

    { return "L"+tostring(n); }

Here is the call graph for this function:

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);
        }
    }
}

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Reimplemented from PLearn::Kernel.

Reimplemented in PLearn::CosKernel.

Definition at line 74 of file DistanceKernel.h.

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().

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().


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines