| 
    PLearn 0.1 
   | 
 
#include <LLEKernel.h>


Public Member Functions | |
| LLEKernel () | |
| 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 LLEKernel * | deepCopy (CopiesMap &copies) const | 
| virtual void | computeGramMatrix (Mat K) const | 
| Overridden for efficiency purpose.   | |
| virtual real | evaluate (const Vec &x1, const Vec &x2) const | 
| Compute K(x1,x2).   | |
| virtual real | evaluate_i_j (int i, int j) const | 
| returns evaluate(data(i),data(j))   | |
| virtual real | evaluate_i_x (int i, const Vec &x, real squared_norm_of_x=-1) const | 
| Return evaluate(data(i),x).   | |
| virtual real | evaluate_i_x_again (int i, const Vec &x, real squared_norm_of_x=-1, bool first_time=false) const | 
| Return evaluate(data(i),x), where x is the same as in the precedent call to this same function (except if 'first_time' is true).   | |
| virtual void | setDataForKernelMatrix (VMat the_data) | 
| ** Subclasses may override these methods to provide efficient kernel matrix access **   | |
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 | |
| int | knn | 
| real | reconstruct_coeff | 
| real | regularizer | 
Static Public Attributes | |
| static StaticInitializer | _static_initializer_ | 
Static Protected Member Functions | |
| static void | declareOptions (OptionList &ol) | 
| Declares this class' options.   | |
Protected Attributes | |
| PP< ReconstructionWeightsKernel > | reconstruct_ker | 
| The kernel used to compute the reconstruction weights.   | |
Private Types | |
| typedef Kernel | inherited | 
Private Member Functions | |
| void | build_ () | 
| This does the actual building.   | |
Private Attributes | |
| bool | build_in_progress | 
| True iff build() has been called but build_() has not been called yet.   | |
| bool | x_is_training_point | 
| Used in 'evaluate_i_x_again' to remember whether x is a training point or not.   | |
| int | x_index | 
| Used in 'evaluate_i_x_again' to remember the index of x when it is a training point.   | |
Definition at line 53 of file LLEKernel.h.
typedef Kernel PLearn::LLEKernel::inherited [private] | 
        
Reimplemented from PLearn::Kernel.
Definition at line 58 of file LLEKernel.h.
| PLearn::LLEKernel::LLEKernel | ( | ) | 
Default constructor.
Definition at line 52 of file LLEKernel.cc.
: build_in_progress(false), reconstruct_ker(new ReconstructionWeightsKernel()), knn(5), reconstruct_coeff(-1), regularizer(1e-6) { }
| string PLearn::LLEKernel::_classname_ | ( | ) |  [static] | 
        
Reimplemented from PLearn::Kernel.
Definition at line 77 of file LLEKernel.cc.
| OptionList & PLearn::LLEKernel::_getOptionList_ | ( | ) |  [static] | 
        
Reimplemented from PLearn::Kernel.
Definition at line 77 of file LLEKernel.cc.
| RemoteMethodMap & PLearn::LLEKernel::_getRemoteMethodMap_ | ( | ) |  [static] | 
        
Reimplemented from PLearn::Kernel.
Definition at line 77 of file LLEKernel.cc.
Reimplemented from PLearn::Kernel.
Definition at line 77 of file LLEKernel.cc.
| Object * PLearn::LLEKernel::_new_instance_for_typemap_ | ( | ) |  [static] | 
        
Reimplemented from PLearn::Object.
Definition at line 77 of file LLEKernel.cc.
| StaticInitializer LLEKernel::_static_initializer_ & PLearn::LLEKernel::_static_initialize_ | ( | ) |  [static] | 
        
Reimplemented from PLearn::Kernel.
Definition at line 77 of file LLEKernel.cc.
| void PLearn::LLEKernel::build | ( | ) |  [virtual] | 
        
Simply calls inherited::build() then build_().
Reimplemented from PLearn::Kernel.
Definition at line 103 of file LLEKernel.cc.
References PLearn::Kernel::build(), build_(), and build_in_progress.
{
    build_in_progress = true;
    inherited::build();
    build_();
}

| void PLearn::LLEKernel::build_ | ( | ) |  [private] | 
        
This does the actual building.
Reimplemented from PLearn::Kernel.
Definition at line 113 of file LLEKernel.cc.
References build_in_progress, PLearn::fast_exact_is_equal(), knn, PLWARNING, reconstruct_coeff, reconstruct_ker, regularizer, PLearn::Kernel::report_progress, setDataForKernelMatrix(), and PLearn::Kernel::specify_dataset.
Referenced by build().
{
    // Let's make sure the value of 'reconstruct_coeff' is not set accidentally
    // to an unusual value.
    if (fast_exact_is_equal(reconstruct_coeff, 0)) {
        PLWARNING("In LLEKernel::build_ - 'reconstruct_coeff' is set to 0, you won't be able to apply this kernel out-of-sample");
    } else if (reconstruct_coeff > 0) {
        PLWARNING("In LLEKernel::build_ - 'reconstruct_coeff' is > 0, this may give weird results out-of-sample for small coefficients");
    } else if (fabs(reconstruct_coeff + 1) > 1e-6) {
        PLWARNING("In LLEKernel::build_ - 'reconstruct_coeff' is negative but not -1, this may give some very weird stuff out-of-sample");
    }
    reconstruct_ker->regularizer = this->regularizer;
    reconstruct_ker->knn = this->knn;
    reconstruct_ker->report_progress = this->report_progress;
    reconstruct_ker->build();
    build_in_progress = false;
    // This code, normally executed in Kernel::build_, can only be executed
    // now beause the kernel 'reconstruct_ker' has to be initialized.
    if (specify_dataset) {
        this->setDataForKernelMatrix(specify_dataset);
    }
}


| string PLearn::LLEKernel::classname | ( | ) |  const [virtual] | 
        
Reimplemented from PLearn::Object.
Definition at line 77 of file LLEKernel.cc.
| void PLearn::LLEKernel::computeGramMatrix | ( | Mat | K | ) |  const [virtual] | 
        
Overridden for efficiency purpose.
Reimplemented from PLearn::Kernel.
Definition at line 139 of file LLEKernel.cc.
References PLearn::fast_exact_is_equal(), i, PLearn::Kernel::n_examples, reconstruct_coeff, and reconstruct_ker.
                                             {
    reconstruct_ker->computeLLEMatrix(K);
    if (!fast_exact_is_equal(reconstruct_coeff, 0)) {
        for (int i = 0; i < n_examples; i++) {
            K(i, i) += fabs(reconstruct_coeff);
        }
    }
}

| void PLearn::LLEKernel::declareOptions | ( | OptionList & | ol | ) |  [static, protected] | 
        
Declares this class' options.
Reimplemented from PLearn::Kernel.
Definition at line 82 of file LLEKernel.cc.
References PLearn::OptionBase::buildoption, PLearn::declareOption(), PLearn::Kernel::declareOptions(), knn, reconstruct_coeff, and regularizer.
{
    declareOption(ol, "knn", &LLEKernel::knn, OptionBase::buildoption,
                  "The number of nearest neighbors considered.");
    declareOption(ol, "reconstruct_coeff", &LLEKernel::reconstruct_coeff, OptionBase::buildoption,
                  "The weight of K' in the weighted sum of K' and K''. If set to a negative\n"
                  "value, it will behave as its absolute value when evaluated on the training\n"
                  "points with the evaluate_i_j method, but will return only its absolute value\n"
                  "times K' when evaluated with the evaluate_i_x method.");
    declareOption(ol, "regularizer", &LLEKernel::regularizer, OptionBase::buildoption,
                  "The regularization factor used to make the linear systems stable.");
    // Now call the parent class' declareOptions
    inherited::declareOptions(ol);
}

| static const PPath& PLearn::LLEKernel::declaringFile | ( | ) |  [inline, static] | 
        
Reimplemented from PLearn::Kernel.
Definition at line 126 of file LLEKernel.h.
Reimplemented from PLearn::Kernel.
Definition at line 77 of file LLEKernel.cc.
Compute K(x1,x2).
Implements PLearn::Kernel.
Definition at line 151 of file LLEKernel.cc.
References evaluate_i_j(), evaluate_i_x(), and PLearn::Kernel::isInData().
                                                           {
    static int j1, j2;
    if (isInData(x1, &j1)) {
        if (isInData(x2, &j2)) {
            // Both points are in the training set.
            return evaluate_i_j(j1, j2);
        } else {
            // Only x1 is in the training set.
            return evaluate_i_x(j1, x2);
        }
    } else {
        if (isInData(x2, &j2)) {
            // Only x2 is in the training set.
            return evaluate_i_x(j2, x1);
        } else {
            // Neither x1 nor x2 is in the training set.
            return 0;
        }
    }
}

returns evaluate(data(i),data(j))
Reimplemented from PLearn::Kernel.
Definition at line 175 of file LLEKernel.cc.
References reconstruct_coeff, and reconstruct_ker.
Referenced by evaluate(), and evaluate_i_x_again().
                                               {
    // When evaluated on training points, we must ignore the nearest neighbor
    // (because it is the point itself).
    real result;
    int tmp = reconstruct_ker->ignore_nearest;
    reconstruct_ker->ignore_nearest = 1;
    if (i == j) {
        result =
            fabs(reconstruct_coeff) +
            2 * reconstruct_ker->evaluate_i_j(i,i) -
            reconstruct_ker->evaluate_sum_k_i_k_j(i,i);
        reconstruct_ker->ignore_nearest = tmp;
        return result;
    } else {
        result =
            reconstruct_ker->evaluate_i_j(j,i) +
            reconstruct_ker->evaluate_i_j(i,j) -
            reconstruct_ker->evaluate_sum_k_i_k_j(i,j);
        reconstruct_ker->ignore_nearest = tmp;
        return result;
    }
}

| real PLearn::LLEKernel::evaluate_i_x | ( | int | i, | 
| const Vec & | x, | ||
| real | squared_norm_of_x = -1  | 
        ||
| ) |  const [virtual] | 
        
Return evaluate(data(i),x).
[squared_norm_of_x is just a hint that may allow to speed up computation if it is already known, but it's optional]
Reimplemented from PLearn::Kernel.
Definition at line 201 of file LLEKernel.cc.
References evaluate_i_x_again().
Referenced by evaluate().
                                                                              {
    return evaluate_i_x_again(i, x, squared_norm_of_x, true);
}


| real PLearn::LLEKernel::evaluate_i_x_again | ( | int | i, | 
| const Vec & | x, | ||
| real | squared_norm_of_x = -1,  | 
        ||
| bool | first_time = false  | 
        ||
| ) |  const [virtual] | 
        
Return evaluate(data(i),x), where x is the same as in the precedent call to this same function (except if 'first_time' is true).
This can be used to speed up successive computations of K(x_i, x) (default version just calls evaluate_i_x).
Reimplemented from PLearn::Kernel.
Definition at line 208 of file LLEKernel.cc.
References evaluate_i_j(), PLearn::fast_exact_is_equal(), PLearn::Kernel::isInData(), reconstruct_coeff, reconstruct_ker, x_index, and x_is_training_point.
Referenced by evaluate_i_x().
                                                                                                     {
    if (fast_exact_is_equal(reconstruct_coeff, 0)) {
        // This kernel should only be evaluated on training points.
        if (first_time) {
            x_is_training_point = isInData(x, &x_index);
        }
        if (!x_is_training_point)
            return 0;
        return evaluate_i_j(i, x_index);
    } else if (reconstruct_coeff > 0) {
        if (first_time) {
            x_is_training_point = isInData(x, &x_index);
        }
        if (x_is_training_point) {
            return evaluate_i_j(i, x_index);
        } else {
            return reconstruct_coeff * reconstruct_ker->evaluate_x_i_again(x, i, squared_norm_of_x, first_time);
        }
    } else {
        // reconstruct_coeff < 0: we assume x is not a training point.
        return fabs(reconstruct_coeff) * reconstruct_ker->evaluate_x_i_again(x, i, squared_norm_of_x, first_time);
    }
}


| OptionList & PLearn::LLEKernel::getOptionList | ( | ) |  const [virtual] | 
        
Reimplemented from PLearn::Object.
Definition at line 77 of file LLEKernel.cc.
| OptionMap & PLearn::LLEKernel::getOptionMap | ( | ) |  const [virtual] | 
        
Reimplemented from PLearn::Object.
Definition at line 77 of file LLEKernel.cc.
| RemoteMethodMap & PLearn::LLEKernel::getRemoteMethodMap | ( | ) |  const [virtual] | 
        
Reimplemented from PLearn::Object.
Definition at line 77 of file LLEKernel.cc.
| void PLearn::LLEKernel::makeDeepCopyFromShallowCopy | ( | CopiesMap & | copies | ) |  [virtual] | 
        
Transforms a shallow copy into a deep copy.
Reimplemented from PLearn::Kernel.
Definition at line 235 of file LLEKernel.cc.
References PLearn::Kernel::makeDeepCopyFromShallowCopy(), and PLERROR.
{
    inherited::makeDeepCopyFromShallowCopy(copies);
    // ### 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("LLEKernel::makeDeepCopyFromShallowCopy not fully (correctly) implemented yet!");
}

| void PLearn::LLEKernel::setDataForKernelMatrix | ( | VMat | the_data | ) |  [virtual] | 
        
** Subclasses may override these methods to provide efficient kernel matrix access **
This method sets the data VMat that will be used to define the kernel matrix. It may precompute values from this that may later accelerate the evaluation of a kernel matrix element
Reimplemented from PLearn::Kernel.
Definition at line 252 of file LLEKernel.cc.
References build_in_progress, reconstruct_ker, and PLearn::Kernel::setDataForKernelMatrix().
Referenced by build_().
                                                    {
    if (build_in_progress)
        return;
    inherited::setDataForKernelMatrix(the_data);
    // We ignore the nearest neighbor when computing the reconstruction weights
    // on the training data.
    reconstruct_ker->ignore_nearest = 1;
    reconstruct_ker->setDataForKernelMatrix(the_data);
    // But we do not ignore it anymore when computing the reconstruction weights
    // on new samples.
    reconstruct_ker->ignore_nearest = 0;
}


Reimplemented from PLearn::Kernel.
Definition at line 126 of file LLEKernel.h.
bool PLearn::LLEKernel::build_in_progress [private] | 
        
True iff build() has been called but build_() has not been called yet.
Definition at line 61 of file LLEKernel.h.
Referenced by build(), build_(), and setDataForKernelMatrix().
Definition at line 88 of file LLEKernel.h.
Referenced by build_(), and declareOptions().
Definition at line 89 of file LLEKernel.h.
Referenced by build_(), computeGramMatrix(), declareOptions(), evaluate_i_j(), and evaluate_i_x_again().
The kernel used to compute the reconstruction weights.
Definition at line 80 of file LLEKernel.h.
Referenced by build_(), computeGramMatrix(), evaluate_i_j(), evaluate_i_x_again(), and setDataForKernelMatrix().
Definition at line 90 of file LLEKernel.h.
Referenced by build_(), and declareOptions().
int PLearn::LLEKernel::x_index [mutable, private] | 
        
Used in 'evaluate_i_x_again' to remember the index of x when it is a training point.
Definition at line 69 of file LLEKernel.h.
Referenced by evaluate_i_x_again().
bool PLearn::LLEKernel::x_is_training_point [mutable, private] | 
        
Used in 'evaluate_i_x_again' to remember whether x is a training point or not.
Definition at line 65 of file LLEKernel.h.
Referenced by evaluate_i_x_again().
 1.7.4